function experiments = analyse_results_control(bPrintPDF)

if nargin < 1
    bPrintPDF = false;
end

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

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).rmse = zeros(nSubjects, nAzimuths);
    experiments(ii).nSubjects = nSubjects;
    
    for ss = 1:nSubjects
    
        [allRspn, allRef] = deal(zeros(nTrials, 1));
        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};
        responses = data{3};
        cnt = 0;
        for rr = 1:length(responses)
            [runType, targetPos] = parse_file_name(names{rr});
            if strcmp(runType, 'control')
                cnt = cnt + 1;
                allRspn(cnt) = responses(rr);
                allRef(cnt) = targetPos;
            end
        end
        fprintf('%d responses recorded\n', cnt);
        
        % work out the error at each azimith
        for aa = 1:nAzimuths
            ref = azimuths(aa);
            responses = azimuths(allRspn(allRef==aa));
            experiments(ii).rmse(ss,aa) = sqrt(mean((responses-ref).^2));
            %experiments(ii).rmse(ss,aa) = mean(abs(responses-ref));
        end
    end 
end

% plot it
plotPath = './graphics';
if ~exist(plotPath, 'dir')
    mkdir(plotPath);
end
plotFile = fullfile(plotPath, 'kopco_rmse_control');
open(strcat(plotFile, '.fig'));

FontSize = 14;

hold off;
faceColourList = {[0 0 0], [1 1 1]};

for ii = 1:nExperiments
    
    AVG = mean(experiments(ii).rmse);
    SEM = std(experiments(ii).rmse) ./ sqrt(experiments(ii).nSubjects);

    errorbar(azimuths, AVG, SEM, 'MarkerSize',8,'Marker','o',...
        'LineStyle','-','Color',[0 0 0],'MarkerFaceColor', faceColourList{ii});
    hold on;
end

axis([-60 60 0 27]);
set(gca,'XTick',[-50,0,50],'XTickLabel',{'-50','0','50'},'FontSize', FontSize);
set(gca,'YTick',5:5:25,'YTickLabel',5:5:25,'FontSize', FontSize);
ylabel('RMS Error [\circ]', 'FontSize', FontSize);
xlabel('Target Azimuth [\circ]', 'FontSize', FontSize);
%title('RMS Error in Responses', 'FontSize', FontSize, 'FontWeight', 'Normal');
h = legend('Control for dry condition', 'Control for reverb condition', ...
    'Location', 'SouthEast');
set(h, 'FontSize', FontSize);


% 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

