%% leave-one-trial-out

%data(normal): (ch*time*trial)
%excludeTrials: template should not include these trials
%data2(sick): (ch*time*trial)
%excludeTrials2: template should not include these trials

% output: correlation of each frequency

function [ out ,out2] = myFBCCA4_twoDataSet_predict( data ,excludeTrials, data2, excludeTrials2, harmonics)

numTrials = (1:size(data,3));
template = squeeze(mean(data(:,:,excludeTrials),3))';
newData = squeeze(mean(data(:,:,numTrials(~ismember(numTrials,numTrials(excludeTrials)))),3))';

newData2 = squeeze(mean(data2(:,:,excludeTrials2),3))';
template2 = squeeze(mean(data2(:,:,numTrials(~ismember(numTrials,numTrials(excludeTrials2)))),3))';


if(isempty(numTrials(~ismember(numTrials,numTrials(excludeTrials)))))
    template = newData;
    template2 = newData2;
end
[c, wx, wy] = myFBCCA(template,5,8:0.2:11.8,harmonics);
freq = (8:0.2:11.8);
debug = zeros(20,harmonics*2,2);
W = [1:10].^(-1.25)+0.25;
for subBand = 1:harmonics
    for i=1:20
        pNewData{i,subBand} = newData*wx{i,subBand};
        pTemplateData{i,subBand} = template*wx{i,subBand};
        %for rr = 1:size(pNewData{i},2)
        %[A,B,r,U,V]  = canoncorr(pNewData{i}(:,1:size(pNewData{i},2)),pTemplateData{i}(:,1:size(pNewData{i},2)));
        %[A,B,r,U,V]  = canoncorr(pNewData{i},pTemplateData{i});
        powerPnewdata = 10*log(abs(fft(pNewData{i,subBand}(:,1))));
        newR = powerPnewdata(round(freq(i)*5+1),:);
        %newR(rr) = r(1);
        %end
        out(i,subBand) = mean(newR(1));
        %     out(i) = r(1);
        %debug(i,:,1) = newR;
        
        pNewData2{i,subBand} = newData2*wx{i,subBand};
        pTemplateData2{i,subBand} = template2*wx{i,subBand};
        
        powerPnewdata2 = 10*log(abs(fft(pNewData2{i,subBand}(:,1))));
        newR2 = powerPnewdata2(round(freq(i)*5+1),:);
        %for rr = 1:size(pNewData2{i},2)
        %     [A,B,r2,U,V]  = canoncorr(pNewData2{i}(:,1:size(pNewData2{i},2)),pTemplateData{i}(:,1:size(pNewData2{i},2)));
        %[A,B,r,U,V]  = canoncorr(pNewData2{i},pTemplateData{i});
        %    newR2(rr) = r(1);
        %end
        out2(i,subBand) = mean(newR2(1));
        %     out2(i) = r2(1);
        %debug(i,:,2) = newR2;
        if(length(excludeTrials)>=51)
            %         figure;subplot(2,1,1);plot(freq,powerPnewdata(round(freq(:)*5+1),:));
            %         hold on;plot(freq,mean(powerPnewdata(round(freq(:)*5+1),:),2),'r','LineWidth',2);hold off;
            %         subplot(2,1,2);plot(freq,powerPnewdata2(round(freq(:)*5+1),:))
            %         hold on;plot(freq,mean(powerPnewdata2(round(freq(:)*5+1),:),2),'r','LineWidth',2);hold off;
            %         title([num2str(freq(i)) ' hz']);
        end
    end
    %figure;plot(freq,out,'b',freq,out2,'r');
end
 out = W(1:harmonics)*(out.^2)';
 out2 = W(1:harmonics)*(out2.^2)';
end

% function [corrVector, Wx, Wy] = myCCA2__(input, resolution, frequency, harmonics)
% %column major
% template = zeros(size(input,1),harmonics*2);
% time = size(input,1);
% T = 1/time * resolution;                     % Sample time
% L = size(input,1);                     % Length of signal
% t = (0:L-1)*T;                % Time vector
% index=1;
% corrVector = zeros(1,length(frequency));
%
% for f = 1:length(frequency)
%     for harm=1:(harmonics)
%         template(:,index) = sin(2*harm*pi*frequency(f)*t);
%         template(:,index+1) = cos(2*harm*pi*frequency(f)*t);
%         index= index+2;
%     end
%     [A,B,r,U,V] = canoncorr(input,template);
%     corrVector(f) = r(1);
%     Wx{f} = A;
%     Wy{f} = B;
%     index=1;
%     template=[];
% end
% end

