subjects = {'sbj03','sbj13'};%patient/normal
%myPath = '../raw/Draft';
myPath = 'data/';
%shift1 = 0; shift2 = 5;
shift1 = 0.14; shift2 = 5.14;
sbjs = 1%:size(subjects,2)
sbjName = subjects{1};
name = {[sbjName '_R_A_raw_asred.set']};
%name = { [sbjName '_L_cali_raw_asred.set'],[sbjName '_R_cali_raw_asred.set']};
EEG2L = pop_loadset('filename',name{1},'filepath',myPath);
EEG2L = pop_select( EEG2L,'time',[shift1 shift2] );
sbjName = subjects{2};
name = {[sbjName '_R_A_raw_asred.set']};
%name = { [sbjName '_L_cali_raw_asred.set'],[sbjName '_R_cali_raw_asred.set']};
EEG10L = pop_loadset('filename',name{1},'filepath',myPath);
EEG10L = pop_select( EEG10L,'time',[shift1 shift2] );
data2L = squeeze(mean(EEG2L.data,3));
data10L = squeeze(mean(EEG10L.data,3));

colors = [0, 51, 204; ...
               255,  0, 102]/255;

%% plot each sector's time/frequency domain data after applying CCA
close all
xRange = 8:0.2:20;
s02AfterCCA=[];
s10AfterCCA=[];
for freq = 1:length(xRange)
    [cool, s02AfterCCA(:,freq)] = myCCA2(data2L(3:8,:)',5,xRange(freq),2);
    [cool, s10AfterCCA(:,freq)] = myCCA2(data10L(3:8,:)',5,xRange(freq),2);
end
% time domain
figure;
subplot(3,2,1);plot((1:length(s10AfterCCA))/500,mean(data2L(3:8,:),1)','Color', colors(1,:),'linewidth',2);hold on;
ylabel('Amplitude (\uV)');
xlabel('Time (s)');
%legend(' Normal subject',' Glaucoma subject');
%legend boxoff
%ylim([-14 25]);
ylim([-10 10]);
set(gca,'fontsize',20);
box off;
hold off;
subplot(3,2,3);plot((1:length(s02AfterCCA))/500,mean(data10L(3:8,:),1)','Color', colors(2,:), 'linewidth',2); hold on;
ylabel('Amplitude (\uV)');
xlabel('Time (s)');
%legend(' Normal subject',' Glaucoma subject');
%legend boxoff
%ylim([-14 25]);
ylim([-10 10]);
set(gca,'fontsize',20);
box off;
hold off;
%frequency domain
freq = [7,18,19];%9.2Hz, 11.4Hz, 11.6Hz
for ff = 1%9.2Hz
    subplot(3,2,2);plot((0:length(s10AfterCCA)-1)/5,abs(fft(s10AfterCCA(:,freq(ff))')/500),'Color', colors(1,:),'linewidth',2);hold on;
    subplot(3,2,2);plot((0:length(s02AfterCCA)-1)/5,abs(fft(s02AfterCCA(:,freq(ff))')/500),'Color', colors(2,:),'linewidth',2);
    plot([9.2, 9.2], [0 4], '--k', 'LineWidth', 1)
    ylabel('Amplitude (uV)');
    xlabel('Frequency (Hz)');
    legend('Control','Glaucoma');
    %legend(' Normal subject',' Glaucoma subject');
    legend boxoff
    set(gca,'fontsize',20);
    xlim([8 20]);
    ylim([0 2]);
    box off;
    hold off;
end
for ff = 2%11.4Hz
    subplot(3,2,4);plot((0:length(s10AfterCCA)-1)/5,abs(fft(s10AfterCCA(:,freq(ff))')/500),'Color', colors(1,:),'linewidth',2);hold on;
    subplot(3,2,4);plot((0:length(s02AfterCCA)-1)/5,abs(fft(s02AfterCCA(:,freq(ff))')/500),'Color', colors(2,:),'linewidth',2);
    plot([11.4, 11.4], [0 4], '--k', 'LineWidth', 1)
    ylabel('Amplitude (uV)');
    xlabel('Frequency (Hz)');
    %legend(' Normal subject',' Glaucoma subject');
    %legend boxoff
    set(gca,'fontsize',20);
    xlim([8 20]);
    ylim([0 2]);
    box off;
    hold off;
end
% 11.6Hz, but we need to reload the data
sbjName = subjects{1};
name = {[sbjName '_R_B_raw_asred.set']};
EEG2L = pop_loadset('filename',name{1},'filepath',myPath);
EEG2L = pop_select( EEG2L,'time',[shift1 shift2] );
sbjName = subjects{2};
name = {[sbjName '_R_B_raw_asred.set']};
EEG10L = pop_loadset('filename',name{1},'filepath',myPath);
EEG10L = pop_select( EEG10L,'time',[shift1 shift2] );
data2L = squeeze(mean(EEG2L.data,3));
data10L = squeeze(mean(EEG10L.data,3));
s02AfterCCA=[];
s10AfterCCA=[];
for frequency = 1:length(xRange)
    [cool, s02AfterCCA(:,frequency)] = myCCA2(data2L(3:8,:)',5,xRange(frequency),2);
    [cool, s10AfterCCA(:,frequency)] = myCCA2(data10L(3:8,:)',5,xRange(frequency),2);
end
for ff = 3%11.6hz
    subplot(3,2,6);plot((0:length(s10AfterCCA)-1)/5,abs(fft(s10AfterCCA(:,freq(ff))')/500),'Color', colors(1,:),'linewidth',2);hold on;
    subplot(3,2,6);plot((0:length(s02AfterCCA)-1)/5,abs(fft(s02AfterCCA(:,freq(ff))')/500),'Color', colors(2,:),'linewidth',2);
    plot([11.6, 11.6], [0 4], '--k', 'LineWidth', 1)
    ylabel('Amplitude (uV)');
    xlabel('Frequency (Hz)');
    %legend('Control','Glaucoma');
    %legend boxoff
    set(gca,'fontsize',20);
    xlim([8 20]);
    ylim([0 2]);
    box off;
    hold off;
end
%title('Frequency domain');
% %%
% % close all
% % figure('name','sbj02');plot((0:size(data2L,2)-1)/5,abs(fft(data2L(3:8,:)')/length(data2L)));
% % figure('name','sbj10');plot((0:size(data10L,2)-1)/5,abs(fft(data10L(3:8,:)')/length(data10L)));
%
% figure('name',subjects{1});plot((0:size(data2L,2)-1)/5,abs(fft(mean(data2L(3:8,:),1))/length(data2L)));
% figure('name',subjects{2});plot((0:size(data10L,2)-1)/5,abs(fft(mean(data10L(3:8,:),1))/length(data10L)));
% figure;
% plot((0:size(data10L,2)-1)/5,abs(fft(mean(data10L(3:8,:),1))/length(data10L)),'b','linewidth',2);hold on;
% plot((0:size(data2L,2)-1)/5,abs(fft(mean(data2L(3:8,:),1))/length(data2L)),'r','linewidth',2);
%
% title('Amplitude of single target SSVEP');
%
% ylabel('Amplutude (uV)');
% xlabel('Frequency (Hz)');
% legend(' Normal subject',' Glaucoma subject');
% set(gca,'fontsize',20);
% % xlim([8 20]);
% % set(gca,'XTick',(8:0.2:20));
% box off;
% hold off;
% % figure('name','sbj02');plot((0:size(data2L,2)-1)/5,10*log10(abs(fft(data2L(3:8,:)'))));
% % figure('name','sbj10');plot((0:size(data10L,2)-1)/5,10*log10(abs(fft(data10L(3:8,:)'))));
% %
% % figure('name','sbj02');pwelch((data2L(3:8,:)'),500,0,length(data2L),500);
% % figure('name','sbj10');pwelch((data10L(3:8,:)'),500,0,length(data10L),500);
% %
% % figure('name','sbj02');pwelch(mean(data2L(3:8,:),1),500,0,length(data2L),500);
% % figure('name','sbj10');pwelch(mean(data10L(3:8,:),1),500,0,length(data10L),500);
% %% the same data but applying CCA, then FFT
% % close all
% s02AfterCCA = myCCA(data2L(3:8,:)',5,11.6,2);
% s10AfterCCA = myCCA(data10L(3:8,:)',5,11.6,2);
%
% figure('name','After CCA- sbj patient');plot((0:size(s02AfterCCA,1)-1)/5,abs(fft(s02AfterCCA(:,1))/length(s02AfterCCA)));
% figure('name','After CCA- sbj normal');plot((0:size(s10AfterCCA,1)-1)/5,abs(fft(s10AfterCCA(:,1))/length(s10AfterCCA)));
% figure;
% plot((0:size(s10AfterCCA,1)-1)/5,abs(fft(s10AfterCCA(:,1))/length(s10AfterCCA)),'b','linewidth',2);hold on;
% plot((0:size(s02AfterCCA,1)-1)/5,abs(fft(s02AfterCCA(:,1))/length(s02AfterCCA)),'r','linewidth',2);
%
% title('Amplitude of single target SSVEP');
%
% ylabel('Amplutude (uV)');
% xlabel('Frequency (Hz)');
% legend(' Normal subject',' Glaucoma subject');
% set(gca,'fontsize',20);
% box off;
% hold off;
% %% the same data but applying CCA only
% % close all
% xRange = 8:0.2:20;
% s02AfterCCA = myCCA2(data2L(3:8,:)',5,xRange,2);
% s10AfterCCA = myCCA2(data10L(3:8,:)',5,xRange,2);
%
% figure('name','After CCA- sbj patient');plot(xRange,s02AfterCCA);
% figure('name','After CCA- sbj normal');plot(xRange,s10AfterCCA);
% figure;
% plot(xRange,s10AfterCCA,'b','linewidth',2);hold on;
% plot(xRange,s02AfterCCA,'r','linewidth',2);
%
% title('Amplitude of single target SSVEP');
%
% ylabel('Correlation');
% xlabel('Frequency (Hz)');
% legend(' Normal subject',' Glaucoma subject');
% set(gca,'fontsize',20);
% xlim([8 20]);
% box off;
% hold off;

%%
%% plot each sector's time domain and correlation data after applying CCA
%close all
% xRange = 8:0.2:11.8;
% s02AfterCCA=[];
% s10AfterCCA=[];
% for freq = 1:length(xRange)
%     [s02AfterCCA_corre(freq), s02AfterCCA(:,freq)] = myCCA2(data2L(3:8,:)',5,xRange(freq),2);
%     [s10AfterCCA_corre(freq), s10AfterCCA(:,freq)] = myCCA2(data10L(3:8,:)',5,xRange(freq),2);
% end
% % for ff = 19
% % time domain
% figure;
% subplot(2,1,1);plot((1:length(s10AfterCCA))/500,s10AfterCCA(:,ff)','b','linewidth',2);hold on;
% subplot(2,1,1);plot((1:length(s02AfterCCA))/500,s02AfterCCA(:,ff)','r','linewidth',2);
% title('Time domain');
% ylabel('Amplitude (uV)');
% xlabel('Time (seccond)');
% legend(' Normal subject',' Glaucoma subject');
% set(gca,'fontsize',20);
% box off;
% hold off;
% % correlation
% %figure;
% subplot(2,1,2);plot(xRange,s10AfterCCA_corre,'b','linewidth',2);hold on;
% subplot(2,1,2);plot(xRange,s02AfterCCA_corre,'r','linewidth',2);
% %title('Correlation');
% ylabel('Correlation');
% xlabel('Frequency (Hz)');
% legend(' Normal subject',' Glaucoma subject');
% set(gca,'fontsize',20);
% xlim([8 11.8]);
% %ylim([0 850]);
% box off;
% hold off;
% end
%%