Home > NoiseTools > nt_qca0.m

# nt_qca0

## PURPOSE

[tosquares,quads,D]=nt_qca0(x,npcs,nsmooth,nquads) - maximize induced power using quadratic component analysis

## SYNOPSIS

function [tosquares,quads,D]=nt_qca0(x,npcs,nsmooth,nquads)

## DESCRIPTION

    [tosquares,quads,D]=nt_qca0(x,npcs,nsmooth,nquads) - maximize induced power using quadratic component analysis

      tosquares: matrix to most reproducible induced component
      quads: most reproducible quadratic component(s)
      D: eigenvalues sorted by absolute value

      x: data (time*channel*trial)
      npcs: maximum number of data PCs to use [default: all]
      nsmooth: square smoothing window to apply to xproducts [default: 1]
      nquads: number of quadratic components to return [default: 1]

     NoiseTools.

## CROSS-REFERENCE INFORMATION

This function calls:

- 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_pca0 [topcs,pwr,y]=nt_pca0(x,shifts,nkeep,threshold,w) - time-shift pca
- nt_quad2square [tosquare,D]=nt_quad2square(toquad,order) - quadratic to squared linear component

This function is called by:

- nt_qca [squares,quads,D]=nt_qca(x,npcs,nsmooth,nquads) - maximize induced power using quadratic component analysis

## SOURCE CODE

    0001 function [tosquares,quads,D]=nt_qca0(x,npcs,nsmooth,nquads)
    0002 %[tosquares,quads,D]=nt_qca0(x,npcs,nsmooth,nquads) - maximize induced power using quadratic component analysis
    0003 %
    0004 %  tosquares: matrix to most reproducible induced component
    0005 %  quads: most reproducible quadratic component(s)
    0006 %  D: eigenvalues sorted by absolute value
    0007 %
    0008 %  x: data (time*channel*trial)
    0009 %  npcs: maximum number of data PCs to use [default: all]
    0010 %  nsmooth: square smoothing window to apply to xproducts [default: 1]
    0011 %  nquads: number of quadratic components to return [default: 1]
    0012 %
    0013 % NoiseTools.
    0014
    0015
    0016 if nargin<4; nquads=1; end
    0017 if nargin<3; nsmooth=1; end
    0018 if nargin<2; npcs=[]; end
    0019 [nsamples,nchans,ntrials]=size(x);
    0020
    0021
    0022 % PCA & normalize, select PCs to save space
    0023 THRESH=10^-12;
    0024 [topcs,pwr]=nt_pca0(x,[],[],THRESH);
    0025 if isempty(npcs) || npcs>size(topcs,2); npcs=size(topcs,2); end
    0026 topcs=topcs(:,1:npcs);
    0027 x=nt_mmat(x,topcs);
    0028
    0029 %{
    0030 Cross-products are formed trial by trial to save space.
    0031 %}
    0032
    0033 % covariance of cross-products
    0034 c0=zeros(npcs*(npcs+1)/2);
    0035 xxx=zeros(nsamples-nsmooth+1,npcs*(npcs+1)/2);
    0036 for k=1:ntrials
    0037     xx=zeros(nsamples,npcs*(npcs+1)/2);
    0038     ii=1;
    0039     for jj=1:npcs
    0040         for kk=1:jj
    0041             xx(:,ii)=x(:,kk,k).*x(:,jj,k);
    0042             ii=ii+1;
    0043         end
    0044     end
    0045     xx=filter(ones(nsmooth,1)/nsmooth,1,xx);                      % lowpass (demodulate)
    0046     xx=xx(nsmooth:end,:,:);
    0047     xxx=xxx+xx;
    0048     c0=c0+xx'*xx;
    0049 end
    0050 c1=xxx'*xxx;
    0051
    0052 % DSS to find most repeatable quadratic form
    0053 [todss,pwr0,pwr1]=nt_dss0(c0,c1,[],0);
    0054 tobest=todss(:,2); % first is DC
    0055
    0056 % find linear component with square closest to optimal quad form
    0057 [tosquares,D]=nt_quad2square(tobest,'colwise');
    0058 tosquares=topcs*tosquares;
    0059
    0060 % on request, answer best quadratic component(s)
    0061 if nargout>1;
    0062     nquads=min(nquads,npcs*(npcs+1)/2-1);
    0063     quads=zeros(nsamples-nsmooth+1,nquads+1,ntrials);
    0064     for k=1:ntrials
    0065         xx=zeros(nsamples,npcs*(npcs+1)/2);
    0066         ii=1;
    0067         for jj=1:npcs
    0068             for kk=1:jj
    0069                 xx(:,ii)=x(:,kk,k).*x(:,jj,k);
    0070                 ii=ii+1;
    0071             end
    0072         end
    0073         xx=filter(ones(nsmooth,1)/nsmooth,1,xx);                      % lowpass (demodulate)
    0074         xx=xx(nsmooth:end,:,:);                                       % chop off onset artifact
    0075         quads(:,:,k)=nt_mmat(xx,todss(:,1:nquads+1));
    0076     end
    0077 end
    0078