function [chord_matrix,f_feature] = compute_chordAnnotation(filename, parameter)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Name: evaluate_matrix1_matrix2
% Programmer: Meinard Müller, Verena Konz
%
%
% Input: filename
%        parameter
%        parameter.shiftFB = 0; Simulates Audio Tuning Downwards by Half Semitone
%        parameter.circShift =  1; Simulates Audio Tuning Upwards by number of semitones
%        Tuning_upwards = parameter.circShift - 0.5* parameter.plusQuarter 
%
% Output: chord_matrix
%         recall
%         f_measure
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


if nargin<2
   parameter=[]; 
end


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

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

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

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

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

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

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

if isfield(parameter,'vec_energy')==0
   parameter.vec_energy = [40 20 10 5] / 100;
end

if isfield(parameter,'vec_weight')==0
   parameter.vec_weight = [ 1 1 1 1]/4;
end

if isfield(parameter,'templateMajor')==0
   parameter.templateMajor = [1 0 0 0 1 0 0 1 0 0 0 0];
end

if isfield(parameter,'templateMinor')==0
   parameter.templateMinor = [1 0 0 1 0 0 0 1 0 0 0 0];
end


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

dirname = parameter.dirname;
thresholdDistance = parameter.chordThresholdDistance;
if strcmp(filename(end-2:end),'wav') ==1
    parameter.win_len = 4410;
    featureRate = 10;
    if parameter.shiftFB ==0
        filename_STMSP = strcat(filename(1:length(filename)-4),'_pitchSTMSP_',num2str(parameter.win_len));
    elseif parameter.shiftFB ==1
        filename_STMSP = strcat(filename(1:length(filename)-4),'_pitchSTMSP_',num2str(parameter.win_len),'_minusQuarter');
    elseif parameter.shiftFB ==2
        filename_STMSP = strcat(filename(1:length(filename)-4),'_pitchSTMSP_',num2str(parameter.win_len),'_minusThird');
    elseif parameter.shiftFB ==3
        filename_STMSP = strcat(filename(1:length(filename)-4),'_pitchSTMSP_',num2str(parameter.win_len),'_minusHalf');
    elseif parameter.shiftFB ==4
        filename_STMSP = strcat(filename(1:length(filename)-4),'_pitchSTMSP_',num2str(parameter.win_len),'_minusTwoThird');
    elseif parameter.shiftFB ==5
        filename_STMSP = strcat(filename(1:length(filename)-4),'_pitchSTMSP_',num2str(parameter.win_len),'_minusThreeQuarter');
    end
    load(strcat(dirname,filename_STMSP)); % load f_pitch and sideinfo;
    wavDuration = sideinfo.wav.duration;    
else
    featureRate = 10;    
    parameter.stepsize_ms = 1/featureRate*1000;
    [midi,info,ext,tempoList] = midird4([dirname,filename]);
    f_pitch = midird4_to_pitchSTMSP(midi, parameter);
end

f_CENS = STMSP_to_CENS(f_pitch,parameter);
f_CENS = circshift(f_CENS,[0 parameter.circShift]);

featureRate  = featureRate/parameter.stat_downsample_array; 
f_feature = f_CENS';
N = size(f_feature,2);

templateChords = chordTemplates(parameter);

 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Analysis
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%select = [6 7 9 18 21];
select = [1:24];
templateChordsSelected = templateChords;
nonSelected = setdiff([1:24],select);
for n=1:size(nonSelected,2)
    templateChordsSelected(:,nonSelected(n)) = ones(12,1)*NaN;
end

numSamples = size(f_feature,2);
numChords =  size(templateChordsSelected,2);

f_distance = 1 - templateChordsSelected' * f_feature;

if parameter.strategy == 0
    [f_min,f_chordIndex] = min(f_distance,[],1);
    chord_matrix = zeros(numChords,numSamples);
    for n=1:numSamples
        chord_matrix(f_chordIndex(n),n)=1;
    end
elseif parameter.strategy == 1
    chord_matrix = f_distance;
    chord_matrix = (chord_matrix  < thresholdDistance);
else
    error('No strategy available!');
end

