% Analysis for results WITH visual feedback
%

% VP files
files = { ...
    '2016-03-02_16:31:57_GUI_VP_2.csv'; ...
    '2016-03-07_15:40:12_GUI_VP_3.csv'; ...
    '2016-03-09_15:42:15_GUI_VP_4.csv'; ...
    '2016-03-08_15:24:03_GUI_VP_5.csv'; ...
    '2016-03-08_16:31:57_GUI_VP_6.csv'; ...
    '2016-03-08_18:35:00_GUI_VP_7.csv'; ...
    '2016-03-10_16:25:03_GUI_VP_9.csv'; ...
    '2016-03-07_16:37:05_GUI_VP_10.csv'; ...
    '2016-03-07_17:21:32_GUI_VP_11.csv'; ...
    '2016-03-07_18:46:13_GUI_VP_12.csv'; ...
    '2016-03-07_18:18:57_GUI_VP_13.csv'; ...
    '2016-03-07_19:17:21_GUI_VP_14.csv'; ...
    '2016-03-08_14:24:54_GUI_VP_15.csv'; ...
    '2016-03-08_16:52:37_GUI_VP_16.csv'; ...
    '2016-03-08_17:45:55_GUI_VP_17.csv'; ...
    '2016-03-09_13:59:16_GUI_VP_18.csv'; ...
    '2016-03-09_14:57:28_GUI_VP_19.csv'; ...
    '2016-03-09_19:12:04_GUI_VP_21.csv'; ...
    '2016-03-09_17:33:08_GUI_VP_22.csv'; ...
    '2016-03-09_18:15:23_GUI_VP_23.csv'; ...
    '2016-03-10_14:16:52_GUI_VP_24.csv'; ...
    '2016-03-10_15:14:20_GUI_VP_25.csv'; ...
    '2016-03-11_14:18:28_GUI_VP_26.csv'; ...
    '2016-03-11_15:17:17_GUI_VP_27.csv'; ...
    '2016-03-11_16:09:30_GUI_VP_28.csv'; ...
    '2016-03-11_17:31:10_GUI_VP_29.csv'; ...
    };

% Condition matching
% NOTE: the position are numbered after the documentation of the BRIR
% measurement, see https://zenodo.org/record/49691/files/doc.zip
pos(1) = 9; %position 1 => condition 9
pos(2) = 6;
pos(3) = 3;
pos(4) = 7;
pos(5) = 4;
pos(6) = 1;
pos(7) = 8;
pos(8) = 5;
pos(9) = 2;
X = [-1.0 -0.5 +0.0 -1.0 -0.5 +0.0 -1.0 -0.5 +0.0];
Y = [+0.5 +0.5 +0.5 +0.0 +0.0 +0.0 -0.5 -0.5 -0.5];

% Get results
M = [];
for ii = 1:length(files)
    tmp = readtable(['../results/visual_feedback/' files{ii}]);
    VP{ii} = sortrows(tmp,4);     % sort after recording technique
    M = [M VP{ii}.Kondition];
end
%figure;
%histogram(M(:));

% Calculate mean result
n = zeros(9,8); % (pos, recordings)
edges = [0.5 1.5 2.5 3.5 4.5 5.5 6.5 7.5 8.5 9.5];
n(:,8) = histcounts(M(:),edges);
for ii = 1:7
    n(:,ii) = histcounts(M(ii,:),edges);
end
fid = fopen('./data/preference_visual_feedback.csv','w');
fprintf(fid,'# Listener position, X/m, Y/m, rec.80, rec.81, rec.82, rec.83, rec.84, rec.85, rec.86, average\n');
for ii = 1:length(n)
    fprintf(fid,'%2.f, %4.1f, %4.1f, %2.f, %2.f, %2.f, %2.f, %2.f, %2.f, %2.f, %2.f\n',ii,X(ii),Y(ii),n(pos(ii),:));
end
fclose(fid);

