Home > NoiseTools > nt_cca_aad.m

# nt_cca_aad

## PURPOSE

[D,E,R]=nt_cca_match_aad(xA,xB,y,ssize) - calculate metrics for match-mismatch task

## SYNOPSIS

function [D,E,R]=nt_cca_aad(xA,xB,y,ssize)

## DESCRIPTION

    [D,E,R]=nt_cca_match_aad(xA,xB,y,ssize) - calculate metrics for match-mismatch task

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

      xA,xB: stimulus streams
      y: response
      ssize: samples, segment size [default: all]

## 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_aad(xA,xB,y,ssize)
    0002 %[D,E,R]=nt_cca_match_aad(xA,xB,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 %  xA,xB: stimulus streams
    0009 %  y: response
    0010 %  ssize: samples, segment size [default: all]
    0011
    0012 if nargin<3; error('!'); end
    0013 if nargin<4; ssize=[]; end
    0014
    0015 if ssize ~= round(ssize); error('!'); end
    0016
    0017 % clip all trials to same size multiple of wsize
    0018 n=size(xA{1},1); % min size?
    0019 for iTrial=1:numel(xA)
    0020     if size(xA{iTrial}) ~= size(xB{iTrial}); error('!'); end
    0021     if size(xA{iTrial}) ~= size(y{iTrial}); error('!'); end
    0022     n=min(n,size(xA{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(xA)
    0028     xA{iTrial}=nt_demean(xA{iTrial}(1:n,:)); % clip trials to new length
    0029     xB{iTrial}=nt_demean(xB{iTrial}(1:n,:)); % clip trials to new length
    0030     y{iTrial}=nt_demean(y{iTrial}(1:n,:));
    0031 end
    0032 nsegments=n/ssize;
    0033 ntrials=numel(xA);
    0034
    0035 % merge stimulus streams
    0036 x={};
    0037 for iTrial=1:numel(xA)
    0038     x{iTrial}=[xA{iTrial},xB{iTrial}];
    0039 end
    0040
    0041 % CCA
    0042 shifts=[0];
    0043 [AA,BB,RR]=nt_cca_crossvalidate(xA,y,shifts);
    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     xA_CC=nt_mmat(xA(others),AA{iTrial});
    0053     xB_CC=nt_mmat(xB(others),AA{iTrial});
    0054     yCC=nt_mmat(y(others),BB{iTrial});
    0055     ncomp=size(xA_CC{1},2);
    0056
    0057     % cut into segments
    0058     XA=zeros(ssize,ncomp,numel(others),nsegments);
    0059     XB=zeros(ssize,ncomp,numel(others),nsegments);
    0060     Y=zeros(ssize,ncomp,numel(others),nsegments);
    0061     for iTrial2=1:numel(others)
    0062         for iWindow=1:nsegments
    0063             start=(iWindow-1)*ssize;
    0064             XA(:,:,iTrial2,iWindow)=nt_normcol(nt_demean(xA_CC{iTrial2}(start+(1:ssize),:))); % all mean 0 norm 1
    0065             XB(:,:,iTrial2,iWindow)=nt_normcol(nt_demean(xB_CC{iTrial2}(start+(1:ssize),:)));
    0066             Y(:,:,iTrial2,iWindow)=nt_normcol(nt_demean(yCC{iTrial2}(start+(1:ssize),:)));
    0067         end
    0068     end
    0069
    0070     % Euclidean distance between EEG and envelope segments
    0071
    0072 %     figure(1); clf;
    0073 %     subplot 211;
    0074 %     plot([XA(:,1,1,1),Y(:,1,1,1)]); title(corr(XA(:,1,1,1),Y(:,1,1,1)))
    0075 %     subplot 212;
    0076 %     plot([XB(:,1,1,1),Y(:,1,1,1)]); title(corr(XB(:,1,1,1),Y(:,1,1,1))); pause
    0077
    0078     % match
    0079     D_match=sqrt(mean((XA-Y).^2));
    0080     sz=size(D_match); D_match=reshape(D_match,sz(2:end)); % remove initial singleton
    0081     D_match=D_match(:,:)'; % trials X comps
    0082
    0083     % mismatch
    0084     D_mismatch=sqrt(mean((XB-Y).^2));
    0085     sz=size(D_mismatch); D_mismatch=reshape(D_mismatch,sz(2:end));
    0086     D_mismatch=D_mismatch(:,:)'; % trials X comps
    0087
    0088 %     figure(1); clf;
    0089 %     plot([D_match(:,1),D_mismatch(:,1)]); pause
    0090
    0091     c0=nt_cov(D_mismatch)/size(D_mismatch,1);
    0092     c1=nt_cov(D_match)/size(D_match,1);
    0093     [todss,pwr0,pwr1]=nt_dss0(c1,c0);
    0094     if mean(D_match*todss(:,1))<0; todss=-todss; end
    0095
    0096     %figure(1); clf; plot(pwr1./pwr0,'.-'); pause
    0097     if 1
    0098     DD_match=D_match*todss(:,1);
    0099     DD_mismatch=D_mismatch*todss(:,1);
    0100     else
    0101     DD_match=D_match(:,1);
    0102     DD_mismatch=D_mismatch(:,1);
    0103     end
    0104
    0105 %     figure(1); clf;
    0106 %     plot([DD_match(:,1),DD_mismatch(:,1)]); pause
    0107
    0108
    0109     dprime(iTrial)=abs(mean(DD_match)-mean(DD_mismatch)) / std([DD_match-mean(DD_match); DD_mismatch-mean(DD_mismatch)]);
    0110 %     disp(dprime(iTrial));
    0111 %     DD_match=D_match(:,1);
    0112 %     DD_mismatch=D_mismatch(:,1);
    0113 %     dprime(iTrial)=abs(mean(DD_match)-mean(DD_mismatch)) / std([DD_match-mean(DD_match); DD_mismatch-mean(DD_mismatch)]);
    0114 %     disp(dprime(iTrial));
    0115
    0116     %{
    0117     We now have a CCA solution and a JD transform calculated
    0118     on other trials.
    0119
    0120     We apply them to segments of this trial.
    0121     %}
    0122
    0123     % apply same CCA transform:
    0124     xA_x=nt_mmat(xA{iTrial},AA{iTrial});
    0125     xB_x=nt_mmat(xB{iTrial},AA{iTrial});
    0126     y_x=nt_mmat(y{iTrial},BB{iTrial});
    0127
    0128     % cut CCs into segments
    0129     XA_x=zeros(ssize,ncomp,nsegments);
    0130     XB_x=zeros(ssize,ncomp,nsegments);
    0131     Y_x=zeros(ssize,ncomp,nsegments);
    0132     for iWindow=1:nsegments
    0133         start=(iWindow-1)*ssize;
    0134         XA_x(:,:,iWindow)=nt_normcol(nt_demean(xA_x(start+(1:ssize),:)));
    0135         XB_x(:,:,iWindow)=nt_normcol(nt_demean(xB_x(start+(1:ssize),:)));
    0136         Y_x(:,:,iWindow)=nt_normcol(nt_demean(y_x(start+(1:ssize),:)));
    0137     end
    0138
    0139 %     figure(1); clf
    0140 %     subplot 211;
    0141 %     plot([XA_x(:,1,iWindow), Y_x(:,1,iWindow)]);title(corr(XA_x(:,1,iWindow), Y_x(:,1,iWindow)));
    0142 %     subplot 212;
    0143 %     plot([XB_x(:,1,iWindow), Y_x(:,1,iWindow)]);title(corr(XB_x(:,1,iWindow), Y_x(:,1,iWindow)));
    0144 %
    0145 %     figure(2); clf
    0146 %     nt_spect_plot(XA_x(:,1,iWindow), 1024,[],[],64); hold on;
    0147 %     nt_spect_plot(Y_x(:,1,iWindow), 1024,[],[],64);
    0148 %     pause
    0149
    0150     % Euclidean distance for matched and mismatched segments
    0151     D_match_x=zeros(nsegments,ncomp);
    0152     D_mismatch_x=zeros(nsegments,ncomp);
    0153     for iWindow=1:nsegments
    0154         D_match_x(iWindow,:)=sqrt( mean((XA_x(:,:,iWindow)-Y_x(:,:,iWindow)).^2) );
    0155         D_mismatch_x(iWindow,:)=sqrt( mean((XB_x(:,:,iWindow)-Y_x(:,:,iWindow)).^2) );
    0156     end
    0157
    0158     if 1
    0159         D_match_x=D_match_x*todss(:,1);
    0160         D_mismatch_x=D_mismatch_x*todss(:,1);
    0161     else
    0162         D_match_x=D_match_x(:,1);
    0163         D_mismatch_x=D_mismatch_x(:,1);
    0164     end
    0165 %       figure(2); clf;
    0166 %       plot([D_match_x,D_mismatch_x])
    0167 %      pause
    0168
    0169     err(iTrial)=numel(find(D_mismatch_x<D_match_x))/nsegments;
    0170     %disp(err(iTrial))
    0171 end
    0172
    0173 D=mean(dprime);
    0174 E=mean(err);
    0175
    0176