Home > NoiseTools > nt_cca_crossvalidate2.m

nt_cca_crossvalidate2

PURPOSE ^

[A,B,RR]=nt_cca_crossvalidate2(xx,yy,shifts) - CCA with cross-validation

SYNOPSIS ^

function [A,B,RR]=nt_cca_crossvalidate2(xx,yy,shifts)

DESCRIPTION ^

[A,B,RR]=nt_cca_crossvalidate2(xx,yy,shifts) - CCA with cross-validation

  A,B: transform matrices calculated on first half of trials
  RR: r scores (3D) for second half of trials

  xx,yy: cell arrays of column matrices
  shifts: array of shifts to apply to y relative to x (can be negative)

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [A,B,RR]=nt_cca_crossvalidate2(xx,yy,shifts)
0002 %[A,B,RR]=nt_cca_crossvalidate2(xx,yy,shifts) - CCA with cross-validation
0003 %
0004 %  A,B: transform matrices calculated on first half of trials
0005 %  RR: r scores (3D) for second half of trials
0006 %
0007 %  xx,yy: cell arrays of column matrices
0008 %  shifts: array of shifts to apply to y relative to x (can be negative)
0009 
0010 if nargin<3 || isempty (shifts); shifts=[0]; end
0011 if nargin<2; error('!'); end
0012 if ~iscell(xx) || ~iscell(yy); error('!'); end
0013 if length(xx) ~= length (yy); error('!'); end
0014 if size(xx{1},1) ~= size(yy{1},1); error('!'); end
0015 
0016 iFirstHalf=1:round(numel(xx)/2);
0017 iSecondHalf=round(numel(xx)/2)+1:numel(xx);
0018 
0019 nTrials=length(xx);
0020 n=size(xx{1},2)+size(yy{1},2);
0021 
0022 % calculate covariance matrix on first half
0023 C=zeros(n,n,length(shifts));
0024 disp('Calculate all covariances...'); tic;
0025 nt_whoss;
0026 for iTrial=iFirstHalf
0027     C=C+nt_cov_lags(xx{iTrial}, yy{iTrial},shifts);
0028 end
0029 toc;
0030 
0031 % calculate CCA on first half
0032 [A,B,R]=nt_cca([],[],[],C,size(xx{1},2));  % CCA to apply to that trial (trained on others)clear C CC
0033 toc;
0034 
0035 %%
0036 % calculate correlation coefficients on second half
0037 disp('Calculate cross-correlations...'); tic;
0038 for iShift=1:length(shifts)
0039     xxx={}; yyy={};
0040     % shift, trim to same length, convert to CCs, normalize
0041     for iTrial=1:nTrials
0042         [xxx{iTrial},yyy{iTrial}]=nt_relshift(xx{iTrial},yy{iTrial},shifts(iShift));
0043         xxx{iTrial}=nt_normcol( nt_demean( nt_mmat(xxx{iTrial},A(:,:,iShift)) ) );
0044         yyy{iTrial}=nt_normcol( nt_demean( nt_mmat(yyy{iTrial},B(:,:,iShift)) ) );
0045     end
0046     for iTrial=1:numel(iSecondHalf)
0047         x=xxx{iSecondHalf(iTrial)};
0048         y=yyy{iSecondHalf(iTrial)};
0049         RR(:,iShift,iTrial)=diag(x'*y) / size(x,1);
0050     end
0051 end
0052 toc;
0053 
0054 disp('done');
0055

Generated on Sat 29-Apr-2023 17:15:46 by m2html © 2005