%%
% condition 1: block 8.8hz
% condition 2: block 9.4hz
%%
function [o1,o2]= plot2D( D1, D2,name, condition, algo )
D1 = squeeze(mean(D1(:,11:size(D1,2),:),3));
D2 = squeeze(mean(D2(:,11:size(D2,2),:),3));
o1 = zeros(size(D1,1),2);
o2 = zeros(size(D2,1),2);
[x1,x2] = getArea(D1,D2);

o1(:,1) = abs(x1)-abs(x2);
o2(:,1) = abs(x1)-abs(x2);

%
% m = {'8hz','8.2hz','8.4hz','8.6hz','8.8hz','9hz','9.2hz','9.4hz','9.6hz','9.8hz','10hz','10.2hz','10.4hz','10.6hz','10.8hz','11hz','11.2hz','11.4hz','11.6hz','11.8hz'}; %the markers to be plotted ('j' is 1 and 'r' is 9
% figure('Color', 'w');%x:power, y:power difference
% text(x1, o1(:,2), m, ...
%     'FontSize', 15, 'FontWeight', 'bold', ...
%     'HorizontalAlignment', 'center','color','k' );
% text(x2, o2(:,2), m, ...
%     'FontSize', 15, 'FontWeight', 'bold', ...
%     'HorizontalAlignment', 'center','color','r' );
% set(gca, 'XLim', [min([o1(:,1);o2(:,1)]) max([o1(:,1);o2(:,1)])], 'YLim', [min([o1(:,2);o2(:,2)]) max([o1(:,2);o2(:,2)])]);
% title(name);

stdo1 =  double(std(D1,0,2));
stdo2 =  double(std(D2,0,2));
if(condition==1)
    blueIndex = [2 4 6 8 10 12 13 15 17 19];%non-flickering targets
    blackIndex = [1 3 5 7 9 11 14 16 18 20];%flickering targets
    redIndex = 7;%suppose to be depressed target
elseif (condition ==2)
    blueIndex = [1 3 5 7 9 11 14 16 18 20];%non-flickering targets
    blackIndex = [2 4 6 10 12 13 15 17 19];%flickering targets
    redIndex = 7;%suppose to be depressed target
elseif (condition ==3)
    blueIndex = [1 3 5 7 9 11 14 16 18 20];%non-flickering targets
    blackIndex = [1 3 5 7 9 11 14 16 18 20 2 4 6 10 12 13 15 17 19];%flickering targets
    redIndex = 6;%suppose to be depressed target
end
m = {'8hz','8.2hz','8.4hz','8.6hz','8.8hz','9hz','9.2hz','9.4hz','9.6hz','9.8hz','10hz','10.2hz','10.4hz','10.6hz','10.8hz','11hz','11.2hz','11.4hz','11.6hz','11.8hz'}; %the markers to be plotted ('j' is 1 and 'r' is 9
figure('Color', 'w');%x:power, y:std
text(double(x1), stdo1, m, ...
    'FontSize', 15, 'FontWeight', 'bold', ...
    'HorizontalAlignment', 'center','color','k' );
text(double(x2), stdo2, m, ...
    'FontSize', 15, 'FontWeight', 'bold', ...
    'HorizontalAlignment', 'center','color','r' );
set(gca, 'XLim', [min([x1;x2]) max([x1;x2])], 'YLim', [min([stdo1;stdo2]) max([stdo1;stdo2])]);
title(name);

figure('Color', 'w');%x:power, y:std
text(o2(blueIndex,1), stdo2(blueIndex), m(blueIndex), ...
    'FontSize', 15, 'FontWeight', 'bold', ...
    'HorizontalAlignment', 'center','color','b' );
text(o2(blackIndex,1), stdo2(blackIndex), m(blackIndex), ...
    'FontSize', 15, 'FontWeight', 'bold', ...
    'HorizontalAlignment', 'center','color','k' );
text(o2(redIndex,1), stdo2(redIndex), m(redIndex), ...
    'FontSize', 15, 'FontWeight', 'bold', ...
    'HorizontalAlignment', 'center','color','r' );
set(gca, 'XLim', [min([o2(:,1)]) max([o2(:,1)])], 'YLim', [min([stdo2]) max([stdo2])]);
title([algo ', ' name]);
end

function [a1, a2] = getArea(data1,data2)
a1 = sum(abs(data1-repmat(mean(data1,2),1,size(data1,2))),2);
a2 = sum(abs(data2-repmat(mean(data2,2),1,size(data2,2))),2);
[a1,i1] = sort(a1,2,'descend');
[a2,i2] = sort(a2,2,'descend');
end


% %%
% x = 1:20;
% y = rand(1,6);
% m = {'8hz','8.2hz','8.4hz','8.6hz','8.8hz','9hz','9.2hz','9.4hz','9.6hz','9.8hz','10hz','10.2hz','10.4hz','10.6hz','10.8hz','11hz','11.2hz','11.4hz','11.6hz','11.8hz'}; %the markers to be plotted ('j' is 1 and 'r' is 9
%
% figure('Color', 'w');
%
% text(x, y, m, ...
%     'FontSize', 20, 'FontWeight', 'bold', ...
%     'HorizontalAlignment', 'center','color','r' )
