0001 function [D,E,R]=nt_cca_mm(x,y,ssize,flipflag)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 if nargin<2; error('!'); end
0013 if nargin<3; ssize=[]; end
0014 if nargin<4||isempty(flipflag); flipflag=0; end
0015
0016 if ssize ~= round(ssize); error('!'); end
0017
0018
0019 n=size(x{1},1);
0020 for iTrial=1:numel(x)
0021 if size(x{iTrial}) ~= size(y{iTrial}); error('!'); end
0022 n=min(n,size(x{iTrial},1));
0023 end
0024 if isempty(ssize); ssize=n; end
0025 n=ssize*floor(n/ssize);
0026 if n<1; error('!'); end
0027 for iTrial=1:numel(x)
0028 x{iTrial}=nt_demean(x{iTrial}(1:n,:));
0029 y{iTrial}=nt_demean(y{iTrial}(1:n,:));
0030 end
0031 nsegments=n/ssize;
0032 ntrials=numel(x);
0033
0034 if 0
0035 for iTrial=1:ntrials
0036 y{iTrial}=y{1+mod(iTrial+5,ntrials)};
0037
0038 end
0039 end
0040
0041
0042 shifts=[0];
0043 [AA,BB,RR]=nt_cca_crossvalidate(x,y,shifts);
0044 R=mean(RR,3);
0045
0046 DD_match=[];
0047 DD_mismatch=[];
0048
0049 for iTrial=1:ntrials
0050
0051
0052 others=setdiff(1:ntrials,iTrial);
0053
0054
0055 cc_x=nt_mmat(x(others),AA{iTrial});
0056 cc_y=nt_mmat(y(others),BB{iTrial});
0057 ncomp=size(cc_x{1},2);
0058
0059
0060 X=zeros(ssize,ncomp,numel(others),nsegments);
0061 Y=zeros(ssize,ncomp,numel(others),nsegments);
0062 for iTrial2=1:numel(others)
0063 for iSegment=1:nsegments
0064 start=(iSegment-1)*ssize;
0065 X(:,:,iTrial2,iSegment)=nt_normcol(nt_demean(cc_x{iTrial2}(start+(1:ssize),:)));
0066 Y(:,:,iTrial2,iSegment)=nt_normcol(nt_demean(cc_y{iTrial2}(start+(1:ssize),:)));
0067 end
0068 end
0069
0070
0071
0072
0073
0074 D_match=sqrt(mean((X-Y).^2));
0075 sz=size(D_match); D_match=reshape(D_match,sz(2:end));
0076 D_match=D_match(:,:)';
0077
0078
0079 D_mismatch=sqrt(mean((X-circshift(Y,1,3)).^2));
0080 sz=size(D_mismatch); D_mismatch=reshape(D_mismatch,sz(2:end));
0081 D_mismatch=D_mismatch(:,:)';
0082
0083
0084 c0=nt_cov(D_match)/size(D_mismatch,1);
0085 c1=nt_cov(D_mismatch)/size(D_match,1);
0086 [todss,pwr0,pwr1]=nt_dss0(c0,c1);
0087 if mean(D_match*todss(:,1))<0; todss=-todss; end
0088
0089
0090
0091
0092
0093
0094
0095 We now have a CCA solution (AA, BB), and a JD transform (todss)
0096 calculated entirely on the basis of other trials.
0097
0098 We apply them to segments of this trial.
0099
0100
0101
0102 cc_x=nt_mmat(x{iTrial},AA{iTrial});
0103 cc_y=nt_mmat(y{iTrial},BB{iTrial});
0104
0105
0106
0107
0108
0109 S_x=zeros(ssize,ncomp,nsegments);
0110 S_y=zeros(ssize,ncomp,nsegments);
0111 for iSegment=1:nsegments
0112 start=(iSegment-1)*ssize;
0113 S_x(:,:,iSegment)=nt_normcol(nt_demean(cc_x(start+(1:ssize),:)));
0114 S_y(:,:,iSegment)=nt_normcol(nt_demean(cc_y(start+(1:ssize),:)));
0115 end
0116
0117
0118 D_match=zeros(nsegments,ncomp);
0119 for iSegment=1:nsegments
0120 D_match(iSegment,:)=sqrt( mean((S_x(:,:,iSegment)-S_y(:,:,iSegment)).^2) );
0121 end
0122
0123
0124 D_mismatch=zeros(nsegments,size(X,3)*size(X,4),ncomp);
0125 for iSegment=1:nsegments
0126 X_all_others=X(:,:,:);
0127 if flipflag;
0128 X_all_others=X_all_others(end:-1:1,:,:);
0129 end
0130 tmp=bsxfun(@minus,X_all_others,S_y(:,:,iSegment));
0131 d = sqrt(mean((tmp).^2));
0132 D_mismatch(iSegment,:,:)=permute(d,[1,3,2]);
0133 end
0134
0135 D_mismatch=reshape(D_mismatch,size(D_mismatch,1)*size(D_mismatch,2),size(D_mismatch,3));
0136
0137
0138
0139 if 0
0140 D_match=D_match*todss(:,1);
0141 D_mismatch=D_mismatch*todss(:,1);
0142 else
0143 D_match=D_match(:,1);
0144 D_mismatch=D_mismatch(:,1);
0145 end
0146
0147
0148 DD_match=[DD_match; D_match(:)];
0149 DD_mismatch=[DD_mismatch; D_mismatch(:)];
0150
0151 err(iTrial)=numel(find(D_match>mean(D_mismatch)))/nsegments;
0152
0153
0154
0155
0156
0157 end
0158
0159 D=abs(mean(DD_match)-mean(DD_mismatch)) /...
0160 std([DD_match-mean(DD_match); DD_mismatch-mean(DD_mismatch)]);
0161
0162 figure(100); clf
0163 histogram(DD_match, 0:.2:10); hold on; histogram(DD_mismatch,0:.2:10); drawnow
0164
0165
0166 E=mean(err);
0167
0168