function [] = try_eventdetection(inputFile,outputFile)
% Perform event detection and produce collective results
% for all different configurations

% Suggested name for outputFile: 'Outputs_(sid/bdm)/XX/scriptXX.txt'

% This script assumes that the system outputs are text files saved in a 
% preexisting folder "Outputs_<annotator>" and their names are of the form:
% 'Outputs_(bdm/sid)/XX/scriptXX<...>.txt
% where <...> is: _Bases_X-XX_thres_XXXX_filt_X

% All possible runs:
%{
 try_eventdetection('Development_Set/stereo/script01.wav','Outputs_sid/01/script01.txt');
 try_eventdetection('Development_Set/stereo/script02.wav','Outputs_sid/02/script02.txt');
 try_eventdetection('Development_Set/stereo/script03.wav','Outputs_sid/03/script03.txt');
 try_eventdetection('Development_Set/stereo/script01.wav','Outputs_bdm/01/script01.txt');
 try_eventdetection('Development_Set/stereo/script02.wav','Outputs_bdm/02/script02.txt');
 try_eventdetection('Development_Set/stereo/script03.wav','Outputs_bdm/03/script03.txt');
%}

% If normalised use thresholds: [200:50:900] and let line 53 on
% If unnormalised use thresholds: [0:0.1:2.4] and comment out line 53

%Choose Annotation
Annotators = {'_bdm', '_sid'};
if isequal(outputFile(8:11),Annotators{1})
ANOT_FLAG = 1;
elseif isequal(outputFile(8:11),Annotators{2})
ANOT_FLAG = 2;
else
    error('Incorrect outputFile, please see description again.');
end


% Different Combinations
AllnumBases = [ 5 8 10 12 15 20 20]; % Last number(20) corresponds to Dict20individual
AllFiltering = [ 1 3 5 ];
AllThres = [200:50:900];  

for i = 1 : length(AllnumBases)
    % Initialize
    if (i == length(AllnumBases))
        loadfile = ['Dictionaries' Annotators{ANOT_FLAG} '/Dict20individual' '.mat'];
    else
        loadfile = ['Dictionaries' Annotators{ANOT_FLAG} '/Dict' num2str(AllnumBases(i)) '.mat'];
    end
    load(loadfile);
    Classes = {'alert','clearthroat','cough','doorslam','drawer','keyboard','keyes',...
        'knock','laughter','mouse','pageturn','pendrop','phone','printer',...
        'speech','switch'};
    
    
    % Read the audio for the Event, making sure no overflow occurs
    [x,fs] = wavread(inputFile);
    
    
    % Normalize wav files
    xnow = sum(x,2)/2;
    % If choosing not to normalize, the thershold parameter above has to be
    % adjusted: i.e. [0:0.1:2.2];
    xnow = xnow./std(xnow); 
    
    
    % Calculate CQT
    [intCQT] = computeCQT(xnow);
    cqt_rep = intCQT(:,round(1:7.1128:size(intCQT,2)));
    
    
    % Perform NMF
    [W,H,errs,vout] = nmf_beta(cqt_rep,size(Dict,2),'beta',1,'W',Dict,'W0',Dict);
    
    
    % Reshape H
    Hnew = reshape(H,AllnumBases(i),16,size(H,2));
    Hnew2 = squeeze(sum(Hnew,1));
    
    for j = 1 : length(AllThres)
        for k = 1 : length(AllFiltering)
            % Perform thresolding on Hnew2
            thr = AllThres(j);
            Hnew2 = medfilt1(Hnew2',AllFiltering(k))';
            eventRoll = double(Hnew2 > thr);
            
            
            % Create matrix of events (onset-offset-classNum)
            auxEventRoll = diff([zeros(1,16); eventRoll'; zeros(1,16);],1); p=0;
            nmat=[];
            for m=1:16
                onsets = find(auxEventRoll(:,m)==1);
                offsets = find(auxEventRoll(:,m)==-1);
                for n=1:length(onsets)
                    if((offsets(n)/100-0.01) - (onsets(n)/100) > 0.05) % not output really short events
                        p=p+1;
                        nmat(p,1) = onsets(n)/100;
                        nmat(p,2) = offsets(n)/100-0.01;
                        nmat(p,3) = m;
                    end;
                end;
            end;
            nmat = sortrows(nmat,1);
            
            
            % Print output
            if (i == length(AllnumBases))
            	outputFileName = [outputFile(1:end-4) '_Bases_'  num2str(AllnumBases(i)) ...
                'individual_thres_' num2str(AllThres(j)) '_filt_' num2str(AllFiltering(k)) '.txt'];
            else
                outputFileName = [outputFile(1:end-4) '_Bases_'  num2str(AllnumBases(i)) ...
                '_thres_' num2str(AllThres(j)) '_filt_' num2str(AllFiltering(k)) '.txt'];
            end	    
            fid=fopen(outputFileName,'w');
            for m=1:size(nmat,1)
                fprintf(fid,'%.2f\t%.2f\t%s\n',nmat(m,1),nmat(m,2),Classes{nmat(m,3)});
            end;
            fclose(fid);
            
        end
    end
    
end

