Home > NoiseTools > nt_cca_mm_old.m

nt_cca_mm_old

PURPOSE ^

[D,E,R]=nt_cca_mm(x,y,ssize) - calculate metrics for match-mismatch task

SYNOPSIS ^

function [D,E,R]=nt_cca_mm(x,y,ssize,flipflag)

DESCRIPTION ^

[D,E,R]=nt_cca_mm(x,y,ssize) - calculate metrics for match-mismatch task

  D: d-prime 
  E: error rate
  R: correlation coefficient over entire trial

  x,y: data as trial arrays
  ssize: samples, segment size [default: all]
  flipflag: if true flip mismatched segments timewise [default false]

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [D,E,R]=nt_cca_mm(x,y,ssize,flipflag)
0002 %[D,E,R]=nt_cca_mm(x,y,ssize) - calculate metrics for match-mismatch task
0003 %
0004 %  D: d-prime
0005 %  E: error rate
0006 %  R: correlation coefficient over entire trial
0007 %
0008 %  x,y: data as trial arrays
0009 %  ssize: samples, segment size [default: all]
0010 %  flipflag: if true flip mismatched segments timewise [default false]
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 % clip all trials to same size multiple of ssize
0019 n=size(x{1},1); % min size?
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); % reduce to multiple of wsize
0026 if n<1; error('!'); end
0027 for iTrial=1:numel(x)
0028     x{iTrial}=nt_demean(x{iTrial}(1:n,:)); % clip trials to new length
0029     y{iTrial}=nt_demean(y{iTrial}(1:n,:));
0030 end
0031 nsegments=n/ssize;
0032 ntrials=numel(x);
0033 
0034 if 0 % scramble (sanity check, should yield approx d-prime==0 and error == 50%)
0035     for iTrial=1:ntrials
0036         y{iTrial}=y{1+mod(iTrial+5,ntrials)};
0037         %disp([iTrial, 1+mod(iTrial+5,ntrials)]);
0038     end
0039 end
0040 
0041 % CCA
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     % calculate model on data excluding this trial
0052     others=setdiff(1:ntrials,iTrial);
0053     
0054     % CCs
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     % cut into segments
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),:))); % all mean 0 norm 1
0066             Y(:,:,iTrial2,iSegment)=nt_normcol(nt_demean(cc_y{iTrial2}(start+(1:ssize),:)));
0067         end
0068     end
0069     
0070     % We calculate the Euclidean distance between EEG and envelope segments
0071     % for both matched and mismatched segment pairs.
0072     
0073     % match
0074     D_match=sqrt(mean((X-Y).^2));
0075     sz=size(D_match); D_match=reshape(D_match,sz(2:end)); % squeeze first dim (singleton)
0076     D_match=D_match(:,:)'; % trials X comps
0077     
0078     % mismatch
0079     D_mismatch=sqrt(mean((X-circshift(Y,1,3)).^2));
0080     sz=size(D_mismatch); D_mismatch=reshape(D_mismatch,sz(2:end)); % squeeze first dim (singleton)
0081     D_mismatch=D_mismatch(:,:)'; % trials X comps
0082     
0083     % CCA to contrast match and mismatch --> LDA
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 %     DD_match=D_match*todss(:,1);
0090 %     DD_mismatch=D_mismatch*todss(:,1);
0091 %     dprime(iTrial)=abs(mean(DD_match)-mean(DD_mismatch)) /...
0092 %         std([DD_match-mean(DD_match); DD_mismatch-mean(DD_mismatch)]);
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     % apply same CCA transform:
0102     cc_x=nt_mmat(x{iTrial},AA{iTrial});
0103     cc_y=nt_mmat(y{iTrial},BB{iTrial});
0104     % yy_x=nt_mmat(y{1+mod(iTrial,ntrials)},BB{iTrial}); % scramble
0105     
0106     %figure(1); plot([xx_x,yy_x]); pause
0107     
0108     % cut CCs into segments
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     % Euclidean distance for matched segments
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     % Euclidean distance for mismatched segments
0124     D_mismatch=zeros(nsegments,size(X,3)*size(X,4),ncomp);
0125     for iSegment=1:nsegments
0126         X_all_others=X(:,:,:); % all segments of all other trials
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]); %mean(d,3);
0133     end
0134     
0135     D_mismatch=reshape(D_mismatch,size(D_mismatch,1)*size(D_mismatch,2),size(D_mismatch,3));
0136     
0137 %      figure(1); clf;
0138 %      for k=1:6; subplot (3,2,k); plot([D_match_x(:,k),D_mismatch_x(:,k)]); end
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 %      figure(2); clf;
0154 %      plot([D_match_x,D_mismatch_x])
0155 %      pause
0156     %disp(err(iTrial))
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

Generated on Thu 23-Jul-2020 16:52:47 by m2html © 2005