function [P,R,F] = evaluateMIREXOnset(output,reference,exclude,parameter)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%
%
% evaluates according to MIREX
%   INPUT: output: array with detected onset positions in sec
%          reference: annotated onset positions, array or cell
%   OUTPUT: performance precision, recall, Fmeasure
%          P: precision
%          R: recall
%          F: F1-measure
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if nargin < 4
    parameter = [];
end

if nargin < 3
    exclude = zeros(size(reference));
end


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

parameter.window = 0.05; %s
window = parameter.window;

% counter
Ocd     =     0;      % number of correctly detected onsets (CD)
Ofn     =     0;      % number of missed onsets (FN)

% for each reference onset

for refOnset =  1:length(reference)
    if exclude(refOnset)>0
        continue;
    end
    start    =  reference(refOnset) - window;
    ende     =  reference(refOnset) + window;
     
    % find beats in tolerance window
    tmp      =  double(output>=start & output<=ende);
    
    Ocd      =  Ocd + (sum(tmp)>0);
    Ofn      =  Ofn + (sum(tmp)==0);


end


P = Ocd / length(output);
R = Ocd / (length(reference) - sum(exclude>0));
F = 2*P*R/(P+R+eps);            




