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: This function is called by:

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     
0025     % x and y are matrices
0026     if size(y,1)~=size(x,1); error('!'); end
0027     if size(y,3)~=size(x,3); error('!'); end
0028     n=size(x,2)+size(y,2);
0029     C=zeros(n,n,length(shifts));
0030     for iPage=1:size(x,3)
0031         for iLag=1:length(shifts)
0032             [xx,yy]=nt_relshift(x(:,:,iPage),y(:,:,iPage),shifts(iLag));
0033             if ~numel(xx); error('xx empty after shifting'); end
0034             if ~numel(yy); error('yy empty after shifting'); end
0035             if demeanflag
0036                 xx=nt_demean(xx); yy=nt_demean(yy);
0037             end
0038             C(:,:,iLag)=C(:,:,iLag) +[xx,yy]'*[xx,yy];
0039         end
0040     end    
0041     m=size(x,2);
0042     tw=size(x,1)*size(x,3);
0043     
0044 else
0045     
0046     if isnumeric(y); error('!'); end
0047     
0048     % x and y are cell arrays
0049     n=size(x{1},2)+size(y{1},2);
0050     C=zeros(n,n,length(shifts));
0051     tw=0;
0052     for iCell=1:length(x);
0053          %disp(iCell)
0054          C=C+nt_cov_lags(x{iCell},y{iCell},shifts);
0055          tw=tw+size(x{iCell},1);
0056     end
0057     m=size(x{1},2);
0058 end
0059

Generated on Sat 29-Apr-2023 17:15:46 by m2html © 2005