Home > NoiseTools > nt_cov_lags.m

# nt_cov_lags

## PURPOSE

[C,tw,m]=nt_cov_lags(x,y,shifts,nodemeanflag) - covariance of [x,y] with lags

## SYNOPSIS

function [C,tw,m]=nt_cov_lags(x,y,shifts,demeanflag)

## DESCRIPTION

    [C,tw,m]=nt_cov_lags(x,y,shifts,nodemeanflag) - covariance of [x,y] with lags

      C: covariance matrix (3D if length(lags)>1)
      tw: total weight
      m: number of columns in x

      x,y: data matrices
      shifts: positive shift means y is delayed relative to x
      demeanflag: if true remove means [default: 1]

      x and y can be time X channels or time X channels X trials.  They can
      also be cell arrays of time X channels matrices.

     See also nt_relshift.

      NoiseTools

## CROSS-REFERENCE INFORMATION

This function calls:

- nt_cov_lags [C,tw,m]=nt_cov_lags(x,y,shifts,nodemeanflag) - covariance of [x,y] with lags
- nt_demean [y,mn]=nt_demean(x,w) - remove weighted mean over cols
- nt_relshift [xx,yy]=nt_relshift(x,y,shift,flag) - delay x relative to y

This function is called by:

- nt_cca [A,B,R]=nt_cca(x,y,shifts,C,m,thresh,demeanflag) - canonical correlation
- nt_cca_crossvalidate [AA,BB,RR,iBest]=nt_cca_crossvalidate(xx,yy,shifts,ncomp,A0,B0) - CCA with cross-validation
- nt_cca_crossvalidate2 [A,B,RR]=nt_cca_crossvalidate2(xx,yy,shifts) - CCA with cross-validation
- nt_cov_lags [C,tw,m]=nt_cov_lags(x,y,shifts,nodemeanflag) - covariance of [x,y] with lags

## SOURCE CODE

    0001 function [C,tw,m]=nt_cov_lags(x,y,shifts,demeanflag)
    0002 %[C,tw,m]=nt_cov_lags(x,y,shifts,nodemeanflag) - covariance of [x,y] with lags
    0003 %
    0004 %  C: covariance matrix (3D if length(lags)>1)
    0005 %  tw: total weight
    0006 %  m: number of columns in x
    0007 %
    0008 %  x,y: data matrices
    0009 %  shifts: positive shift means y is delayed relative to x
    0010 %  demeanflag: if true remove means [default: 1]
    0011 %
    0012 %  x and y can be time X channels or time X channels X trials.  They can
    0013 %  also be cell arrays of time X channels matrices.
    0014 %
    0015 % See also nt_relshift.
    0016 %
    0017 %  NoiseTools
    0018
    0019 if nargin<2; error('!'); end
    0020 if nargin<3 || isempty(shifts); shifts=[0]; end
    0021 if nargin<4 || isempty(demeanflag); demeanflag=1; end
    0022
    0023 if isnumeric(x)
    0024     % x and y are matrices
    0025     if size(y,1)~=size(x,1); error('!'); end
    0026     if size(y,3)~=size(x,3); error('!'); end
    0027     n=size(x,2)+size(y,2);
    0028     C=zeros(n,n,length(shifts));
    0029     for iPage=size(x,3)
    0030         for iLag=1:length(shifts)
    0031             [xx,yy]=nt_relshift(x(:,:,iPage),y(:,:,iPage),shifts(iLag));
    0032             if demeanflag
    0033                 xx=nt_demean(xx); yy=nt_demean(yy);
    0034             end
    0035             C(:,:,iLag)=C(:,:,iLag) +[xx,yy]'*[xx,yy];
    0036         end
    0037     end
    0038     m=size(x,2);
    0039     tw=size(x,1)*size(x,3);
    0040 else
    0041     if isnumeric(y); error('!'); end
    0042
    0043     % x and y are cell arrays
    0044     n=size(x{1},2)+size(y{1},2);
    0045     C=zeros(n,n,length(shifts));
    0046     tw=0;
    0047     for iCell=1:length(x);
    0048          %disp(iCell)
    0049          C=C+nt_cov_lags(x{iCell},y{iCell},shifts);
    0050          tw=tw+size(x{iCell},1);
    0051     end
    0052     m=size(x{1},2);
    0053 end
    0054