function [f_pitch,sideinfo] = STFT_to_pitchSTMSP(f_magstft,freqinfo,featureRate,parameter,sideinfo)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Name: STFT_to_pitchSTMSP
% Version: 1
% Date: 2009
% Programmer: Sebastian Ewert
%
% - pools DFT bins and forms pitch features
%
% Input: f_magstft  (double FxT)    % could also be a be power-STFT but should be real valued
%        parameter.vis = 0;
%        parameter.save = 0;
%        parameter.save_dir;
%        parameter.save_filename;
%        parameter.midi_min = 21;
%        parameter.midi_max = 108;
%        parameter.refTuning = 440;
%        parameter.sampleRate = 22050;
%        parameter.bandradius = 0.4
%        parameter.save_dir = '';
%        parameter.save_filename = '';
%        parameter.peakPickInBands = 1  % otherwise it will sum the energy in a bands
%
%
% Output: f_pitch
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Check parameters
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if nargin<3
    sideinfo=[];
end

if nargin<2
    parameter=[];
end

if nargin<1
    error('Please specify input data f_audio');
end

if isfield(parameter,'save')==0
    parameter.save = 0;
end

if isfield(parameter,'vis')==0
    parameter.vis = 0;
end

if isfield(parameter,'midi_min')==0
   parameter.midi_min = 21;
end

if isfield(parameter,'midi_max')==0
   parameter.midi_max = 108;
end

if isfield(parameter,'refTuning')==0
   parameter.refTuning = 440;
end

if isfield(parameter,'sampleRate')==0
   parameter.sampleRate = 22050;
end

if isfield(parameter,'bandradius')==0
   parameter.bandradius = 0.4;
end
if isfield(parameter,'peakPickInBands')==0
   parameter.peakPickInBands = 1;
end

if isfield(parameter,'save_dir')==0
    parameter.save_dir = '';
end
if isfield(parameter,'save_filename')==0
    parameter.save_filename = '';
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% some pre calculations
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

f_pitch = zeros(size(f_magstft,2),120);
for m=parameter.midi_min:parameter.midi_max
    lowerLimitForBand_hz = parameter.refTuning * ( 2^((m-69-parameter.bandradius)/12));
    upperLimitForBand_hz = parameter.refTuning * ( 2^((m-69+parameter.bandradius)/12));
    [ignoreMe indLower] = min(abs(freqinfo-lowerLimitForBand_hz));
    [ignoreMe indUpper] = min(abs(freqinfo-upperLimitForBand_hz));
    
    if parameter.peakPickInBands
        f_pitch(:,m) = max(f_magstft(indLower:indUpper,:),[],1);
    else
        f_pitch(:,m) = sum(f_magstft(indLower:indUpper,:),1);
    end
end

sideinfo.STFTpitchSTMSP.featureRate = featureRate;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Save f_pitch_energy for each window size separately as f_pitch
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    

if parameter.save == 1
    filename = strcat(parameter.save_filename,'_FBpitchSTMSP_',num2str(parameter.sampleRate * 2/featureRate));
    save(strcat(parameter.save_dir,filename),'f_pitch','sideinfo');
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Visualization
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if parameter.vis == 1
    figure;
    hot2 = hot;
    hot2 = [hot2; hot2(32:64,:); hot2(32:64,:)];
    hot2 = sort(hot2);

    t_axis = (0:size(f_pitch,1)-1)/featureRate;
    p_axis = (parameter.midi_min:parameter.midi_max);
    f_image = f_pitch(:,p_axis)';
    imagesc(t_axis,p_axis,f_image);

    set(gca,'YTick',[parameter.midi_min:parameter.midi_max]);
    set(gca,'YDir','normal');
    colormap(hot2);
    h = colorbar;
end

end
