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]; xvalidate=1;
0043 [AA,BB,RR]=nt_cca_crossvalidate(x,y,shifts,xvalidate);
0044 R=mean(RR,3);
0045
0046 for iTrial=1:ntrials
0047
0048
0049 others=setdiff(1:ntrials,iTrial);
0050
0051
0052 xx=nt_mmat(x(others),AA{iTrial});
0053 yy=nt_mmat(y(others),BB{iTrial});
0054 ncomp=size(xx{1},2);
0055
0056
0057 X=zeros(ssize,ncomp,numel(others),nsegments);
0058 Y=zeros(ssize,ncomp,numel(others),nsegments);
0059 for iTrial2=1:numel(others)
0060 for iWindow=1:nsegments
0061 start=(iWindow-1)*ssize;
0062 X(:,:,iTrial2,iWindow)=nt_normcol(nt_demean(xx{iTrial2}(start+(1:ssize),:)));
0063 Y(:,:,iTrial2,iWindow)=nt_normcol(nt_demean(yy{iTrial2}(start+(1:ssize),:)));
0064 end
0065 end
0066
0067
0068
0069
0070 D_match=sqrt(mean((X-Y).^2));
0071 sz=size(D_match); D_match=reshape(D_match,sz(2:end));
0072 D_match=D_match(:,:)';
0073
0074
0075 D_mismatch=sqrt(mean((X-circshift(Y,1,3)).^2));
0076 sz=size(D_mismatch); D_mismatch=reshape(D_mismatch,sz(2:end));
0077 D_mismatch=D_mismatch(:,:)';
0078
0079 c0=nt_cov(D_match)/size(D_mismatch,1);
0080 c1=nt_cov(D_mismatch)/size(D_match,1);
0081 [todss,pwr0,pwr1]=nt_dss0(c0,c1);
0082 if mean(D_match*todss(:,1))<0; todss=-todss; end
0083
0084 DD_match=D_match*todss(:,1);
0085 DD_mismatch=D_mismatch*todss(:,1);
0086
0087 dprime(iTrial)=abs(mean(DD_match)-mean(DD_mismatch)) / std([DD_match-mean(DD_match); DD_mismatch-mean(DD_mismatch)]);
0088
0089
0090 We now have a CCA solution and a JD transform calculated
0091 on other trials.
0092
0093 We apply them to segments of this trial.
0094
0095
0096
0097 xx_x=nt_mmat(x{iTrial},AA{iTrial});
0098 yy_x=nt_mmat(y{iTrial},BB{iTrial});
0099
0100
0101
0102
0103
0104 X_x=zeros(ssize,ncomp,nsegments);
0105 Y_x=zeros(ssize,ncomp,nsegments);
0106 for iWindow=1:nsegments
0107 start=(iWindow-1)*ssize;
0108 X_x(:,:,iWindow)=nt_normcol(nt_demean(xx_x(start+(1:ssize),:)));
0109 Y_x(:,:,iWindow)=nt_normcol(nt_demean(yy_x(start+(1:ssize),:)));
0110 end
0111
0112
0113 D_match_x=zeros(nsegments,ncomp);
0114 for iWindow=1:nsegments
0115 D_match_x(iWindow,:)=sqrt( mean((X_x(:,:,iWindow)-Y_x(:,:,iWindow)).^2) );
0116 end
0117
0118
0119 D_mismatch_x=zeros(nsegments,ncomp);
0120 for iWindow=1:nsegments
0121 X_all_others=X(:,:,:);
0122 if flipflag;
0123 X_all_others=X_all_others(end:-1:1,:,:);
0124 end
0125 tmp=bsxfun(@minus,X_all_others,Y_x(:,:,iWindow));
0126 d = sqrt(mean((tmp).^2));
0127 D_mismatch_x(iWindow,:)=mean(d,3);
0128 end
0129
0130
0131
0132 if 1
0133 D_match_x=D_match_x*todss(:,1);
0134 D_mismatch_x=D_mismatch_x*todss(:,1);
0135 else
0136 D_match_x=D_match_x(:,1);
0137 D_mismatch_x=D_mismatch_x(:,1);
0138 end
0139
0140
0141
0142
0143
0144 err(iTrial)=numel(find(D_mismatch_x<D_match_x))/nsegments;
0145
0146 end
0147
0148 D=mean(dprime);
0149 E=mean(err);
0150
0151