Home > NoiseTools > nt_cca_mm.m

# nt_cca_mm

## PURPOSE

[D,E,R]=nt_cca_match_mm3(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_match_mm3(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:

- nt_cca_crossvalidate [AA,BB,RR,iBest]=nt_cca_crossvalidate(xx,yy,shifts,ncomp,A0,B0) - CCA with cross-validation
- nt_cov [c,tw]=nt_cov(x,shifts,w) - time shift covariance
- nt_demean [y,mn]=nt_demean(x,w) - remove weighted mean over cols
- nt_dss0 [todss,pwr1,pwr2]=nt_dss0(c0,c1,keep1,keep2) - dss from covariance
- nt_mmat y=nt_mmat(x,m) - matrix multiplication (with convolution)
- nt_normcol [y,norm]=nt_normcol(x,w) - normalize each column so its weighted msq is 1

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_match_mm3(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 wsize
    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]; 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     % calculate model on data excluding this trial
    0049     others=setdiff(1:ntrials,iTrial);
    0050
    0051     % CCs
    0052     xx=nt_mmat(x(others),AA{iTrial});
    0053     yy=nt_mmat(y(others),BB{iTrial});
    0054     ncomp=size(xx{1},2);
    0055
    0056     % cut into segments
    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),:))); % all mean 0 norm 1
    0063             Y(:,:,iTrial2,iWindow)=nt_normcol(nt_demean(yy{iTrial2}(start+(1:ssize),:)));
    0064         end
    0065     end
    0066
    0067     % Euclidean distance between EEG and envelope segments
    0068
    0069     % match
    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(:,:)'; % trials X comps
    0073
    0074     % mismatch
    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(:,:)'; % trials X comps
    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     % apply same CCA transform:
    0097     xx_x=nt_mmat(x{iTrial},AA{iTrial});
    0098     yy_x=nt_mmat(y{iTrial},BB{iTrial});
    0099     % yy_x=nt_mmat(y{1+mod(iTrial,ntrials)},BB{iTrial}); % scramble
    0100
    0101     %figure(1); plot([xx_x,yy_x]); pause
    0102
    0103     % cut CCs into segments
    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     % Euclidean distance for matched segments
    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     % average Euclidean distance for mismatched segments
    0119     D_mismatch_x=zeros(nsegments,ncomp);
    0120     for iWindow=1:nsegments
    0121         X_all_others=X(:,:,:); % all segments of all other trials
    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 %      figure(1); clf;
    0131 %      for k=1:6; subplot (3,2,k); plot([D_match_x(:,k),D_mismatch_x(:,k)]); end
    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 %      figure(2); clf;
    0141 %      plot([D_match_x,D_mismatch_x])
    0142 %      pause
    0143
    0144     err(iTrial)=numel(find(D_mismatch_x<D_match_x))/nsegments;
    0145     %disp(err(iTrial))
    0146 end
    0147
    0148 D=mean(dprime);
    0149 E=mean(err);
    0150
    0151