function [pitch,sad,dc_idx,pf0] = genPitchLabel(f0s,algs,pcf,dispflag,gthf)
% function [pitch,sad,dc_idx,pf0] = genPitchLabel(f0s,algs,pcf,dispflag)
%
% Generates pitch label using agreement of multiple pitch tracks using 
% following the following method: 
%  Generate SAD and pitch label
%     1. certain pitch when all pitch estimation agrees (voiced)
%     2. uncertain pitch when pitch estimates disagree (voiced)
%     3. uncertain voiced when voicing decisions disagree
%     4. certain unvoiced when unvoicing decisions disagree
%
% Inputs:
%         f0s: a cell containing pitch track (f0 in Hz)
%         algs: the names of algorithms
%         pcf: frequencies of pitch candidates
%         dispflag: flag for plotting (filename should be changed
%         accordingly)
%
% Outputs:
%         pitch: generated pitch label using agreement (pitch index)
%         sad: SAD (1/0) + disagreement code (-1: voicing, -2: f0)
%         dc_idx: discard decision (when sad = -1 or -2.)
%         pf0: generated pitch in f0 (Hz)
%
% [2012-05-01] Byung Suk Lee bsl@ee.columbia.edu

% if ~exist('dispflag','var'), dispflag = 0; end;
if nargin < 4, dispflag = 0; end;

%%
pd = 6;
npcf = length(pcf);
nalgs = length(algs);
ps = cell(nalgs,1);

for alg = 1:nalgs
    ps{alg} = freq2pitch(f0s{alg},pcf);
    if size(ps{alg},2) > size(ps{alg},1)
        ps{alg} = ps{alg}';
    end
    pso{alg} = ps{alg};
end

%%
sad = - ones(size(ps{alg}));
label = zeros(size(ps{alg}));
for alg = 1:nalgs
label = label + (ps{alg} > 0);
end
sad(label < eps) = 0;
sad(label == nalgs) = 1;
for alg = 1:nalgs
ps{alg}(label < nalgs) = 0;
end
tmpp = zeros(length(ps{alg}),nalgs);
for alg = 1:nalgs
tmpp(:,alg) = ps{alg};
end
pitch = mean(tmpp,2);

%% allow 20% deviation from the mean pitch 
ucp_idx = zeros(size(ps{nalgs}));
for alg = 1:nalgs
    ucp_idx = ucp_idx + (abs(pitch - ps{alg}) > pd);
end
ucp_idx = ucp_idx > 0;
sad(ucp_idx) = -2;
dc_idx = sad < 0;
pitch(dc_idx) = npcf + 1;
pf0 = pitch2freq(pitch,pcf);
pitch = round(pitch);

if dispflag
% if isempty(strfind(path,'../export_fig')), addpath('../export_fig/'); end;

%%
colors = {'r+','bo','gx','md','y','c','k'};
figure,
ax(1) = subplot(411);
hold off
for alg = 1:nalgs
    plot(pso{alg},colors{alg})
    if alg == 1
        hold on
    end
end
grid on
legend(algs,'Location','NorthEastOutside')
ylabel({'(a)','Pitch Index'})
ax(2) = subplot(412);
plot(sad,'r')
legend('SAD','Location','NorthEastOutside')
ylabel({'(b)','SAD'})
ylim([-2 2])
ax(3) = subplot(413);
plot(pitch,'bx-')
hold on;
dc_show = dc_idx * (npcf + 1);
dc_show(~dc_idx) = inf;
plot(dc_show,'og')
ylabel({'(c)','Pitch Index'})
xlabel('Frame')
legend('Label','Reject','Location','NorthEastOutside')
grid on
if nargin > 4
ax(4) = subplot(414);
plot(gthf,'rx-')
hold on;
plot(pf0,'b+-')
ylabel({'(d)','f0 (Hz)'})
legend('ground truth','GPL output','Location','NorthEastOutside')
end
linkaxes(ax,'x')
% xlim([0 1000]+100*6)

% imgpath = 'images/gpl/';
% if ~exist(imgpath,'dir'), mkdir(imgpath); end;
% outfile = [imgpath,'rats_sample_label'];
% set(gcf, 'Color', 'w');
% print('-dpng',[outfile,'.png']);
% export_fig([outfile,'.eps']);
% export_fig([outfile,'.pdf']);

end
