function experiments = analyse_results_missrate(bPrintPDF)

if nargin < 1
    bPrintPDF = false;
end

resultPath = './../results';
snr = 0;
azimuths = -50:10:50;
experiments = struct('brir', {'KEMAR', 'Surrey'}, ...
                     'snr', snr, ...
                     'azimuths', azimuths);
nExperiments = numel(experiments);
nAzimuths = length(azimuths);
nMaskerPatterns = 5;
nIDs = 5;
nTrials = nIDs * nMaskerPatterns * nAzimuths;


for ii = 1:nExperiments
    
    fprintf('-- Processing responses for BRIR = %s , SNR = %d dB\n', experiments(ii).brir, experiments(ii).snr);
    allFiles = dir(fullfile(resultPath, sprintf('results_%s_*_snr_%d.txt', experiments(ii).brir, experiments(ii).snr)));
    nSubjects = length(allFiles);
    experiments(ii).nSubjects = nSubjects;
    [experiments(ii).missRateFixed, experiments(ii).missRateMixed] = ...
        deal(zeros(nSubjects, nAzimuths, nMaskerPatterns));
    
    for ss = 1:nSubjects

        % Extract responses for both fixed and mixed masker conditions
        % nTrials x 3: [resp ref maskerPat]
        [rspnFixed, rspnMixed] = deal(zeros(nTrials, 3));
        
        fprintf('   %d/%d: ', ss, nSubjects);
        filename = fullfile(resultPath, allFiles(ss).name);
        fprintf('%s ', filename);
        % read the data
        ifp = fopen(filename,'r');
        data = textscan(ifp,'%d%s%d');
        fclose(ifp);
        names = data{2};
        rspn = data{3};
        cntFixed = 0; cntMixed = 0;
        for rr = 1:length(rspn)
            [runType, tPos, mPat] = parse_file_name(names{rr});
            switch runType
                case 'fixed'
                    cntFixed = cntFixed + 1;
                    rspnFixed(cntFixed,:) = [rspn(rr), tPos, mPat];
                case 'mixed'
                    cntMixed = cntMixed + 1;
                    rspnMixed(cntMixed,:) = [rspn(rr), tPos, mPat];
                case 'fixed_catch'
                case 'mixed_catch'
            end
        end
        fprintf('%d fixed responses and %d mixed responses\n', cntFixed, cntMixed);
        
        % Work out miss rate at each azimith for each masker pattern
        for mPat = 1:nMaskerPatterns
            for tPos = 1:nAzimuths

                % Fixed maskers
                idx = (rspnFixed(:,2) == tPos) & (rspnFixed(:,3) == mPat);
                rspn = rspnFixed(idx,1);
                experiments(ii).missRateFixed(ss,tPos,mPat) = ...
                    100 * sum(rspn==0) / length(rspn);

                % Mixed maskers
                idx = (rspnMixed(:,2) == tPos) & (rspnMixed(:,3) == mPat);
                rspn = rspnMixed(idx,1);
                experiments(ii).missRateMixed(ss,tPos,mPat) = ...
                	100 * sum(rspn==0) / length(rspn);

            end
        end
        
    end
end

% plot it
plotPath = './graphics';
if ~exist(plotPath, 'dir')
    mkdir(plotPath);
end
faceColourList = {[0 0 0], [1 1 1]};
FontSize = 16;
xticks = [-50 0 50];
yticks = 0:10:30;
xmax = 70;
ymax = 38;

% Plot miss rates
plotFile = fullfile(plotPath, 'kopco_missrate');
open(strcat(plotFile, '.fig'));

% Plot masker positions
maskerMarkerPos = -7;
subplotList = zeros(nMaskerPatterns, 1);
for mPat = 1:nMaskerPatterns
    switch (mPat)
        case 1
            maskerPositions = [1 2 3 4];
        case 2
            maskerPositions = [8 9 10 11];
        case 3
            maskerPositions = [4 5 7 8];
        case 4
            maskerPositions = [1 2 10 11];
        case 5
            maskerPositions = [2 5 7 10];
        otherwise
            maskerPositions = [];
    end
    subplotList(mPat) = subplot(1, nMaskerPatterns, mPat);
    hold off
    plot(azimuths(maskerPositions), maskerMarkerPos+1, 'MarkerSize',6,'Marker',...
        '^','LineStyle','none','Color',[0 0 0],'MarkerFaceColor',[0 0 0]);
    title(sprintf('Pattern %d', mPat), 'FontSize', FontSize, 'FontWeight', 'Normal');

    hold on;
    plot([-xmax xmax], [0 0], '--k');
    axis([-xmax xmax maskerMarkerPos ymax]);
    set(gca,'XTick',xticks,'XTickLabel',{'-50','0','50'},'FontSize', FontSize);

    if mPat == 1
        set(gca,'YTick',yticks,'YTickLabel',yticks,'FontSize', FontSize);
        ylabel('Miss Rate [%]', 'FontSize', FontSize);
    else
        set(gca,'YTick',yticks,'YTickLabel',{},'FontSize', FontSize);
    end
    if mPat == 3
        xlabel('Target Azimuth [\circ]', 'FontSize', FontSize);
    end
end

% Plot miss rates
for ii = 1:nExperiments
    
    for mPat = 1:nMaskerPatterns

        AVG = mean(experiments(ii).missRateMixed(:,:,mPat));
        plot(subplotList(mPat), azimuths, AVG, 'MarkerSize',8,'Marker','d','LineStyle',...
            '-','Color',[0 0 0],'MarkerFaceColor', faceColourList{ii});
    
        AVG = mean(experiments(ii).missRateFixed(:,:,mPat));
        plot(subplotList(mPat), azimuths, AVG, 'MarkerSize',8,'Marker','s','LineStyle',...
            '-','Color',[0 0 0],'MarkerFaceColor', faceColourList{ii});

    end
end

% Plot legend grid
%subplot(1, nMaskerPatterns, 1);
x1 = -40; x2 = 0; x3 = 35;
y1 = ymax - 4;
%text(x1,y1,'TMR','FontSize', FontSize)
text(x2,y1,'Mixed','FontSize', FontSize)
text(x3,y1,'Fixed','FontSize', FontSize)

x2 = [0 26]; x3 = [36 62];
y2 = ymax - 9;
text(x1,y2,'Dry','FontSize', FontSize)
ii = 1;
plot(x2, [y2 y2], 'k')
plot(mean(x2), y2, 'MarkerSize',8,'Marker','d','LineStyle','-','Color',[0 0 0],'MarkerFaceColor',faceColourList{ii});
plot(x3, [y2 y2], 'k')
plot(mean(x3), y2, 'MarkerSize',8,'Marker','s','LineStyle','-','Color',[0 0 0],'MarkerFaceColor',faceColourList{ii});

y3 = ymax - 14;
text(x1,y3,'Reverb','FontSize', FontSize)
ii = 2;
plot(x2, [y3 y3], 'k')
plot(mean(x2), y3, 'MarkerSize',8,'Marker','d','LineStyle','-','Color',[0 0 0],'MarkerFaceColor',faceColourList{ii});
plot(x3, [y3 y3], 'k')
plot(mean(x3), y3, 'MarkerSize',8,'Marker','s','LineStyle','-','Color',[0 0 0],'MarkerFaceColor',faceColourList{ii});

% Resize subplots
pos1 = get(subplotList(1), 'Position');
pos2 = get(subplotList(2), 'Position');
% p = [pos1(2) pos2(1)-pos1(1) pos1(4)];
p = [0.1841 pos2(1)-pos1(1) 0.7108];
for m = 1:nMaskerPatterns
    pos = get(subplotList(m), 'Position');
    pos(2:4) = p;
    set(subplotList(m), 'Position', pos);
end


% Print PDF file
if bPrintPDF
    set(gcf,'Units','inches');
    pos = get(gcf,'Position');
    set(gcf, 'PaperPosition',[0 0 pos(3:4)],'PaperSize', pos(3:4));
    print('-dpdf', strcat(plotFile, '.pdf'));
end

