Home > NoiseTools > nt_cov.m

# nt_cov

## PURPOSE

[c,tw]=nt_cov(x,shifts,w) - time shift covariance

## SYNOPSIS

function [c,tw]=nt_cov(x,shifts,w);

## DESCRIPTION

    [c,tw]=nt_cov(x,shifts,w) - time shift covariance

      c: covariance matrix
      tw: total weight (c/tw is normalized covariance)

      x: data
      shifts: array of time shifts (must be non-negative)
      w: weights

     If SHIFTS==[0], this function calculates the covariance matrix of X,
     weighted by W.  In the general case it calculates the covariance matrix
     of time-shifted X.

     X can be 1D, 2D or 3D.  It can also be a cell array.
     W can be 1D (if X is 1D or 2D) or 2D (if X is 3D). The same weight is
     applied to each column.

     Output is a 2D matrix with dimensions (ncols(X)*numel(SHIFTS))^2.
     It is made up of an ncols(X)*ncols(X) matrix of submatrices, each of
     dimensions numel(SHIFTS)*numel(SHIFTS).

     NoiseTools

## CROSS-REFERENCE INFORMATION

This function calls:

- nt_multishift z=nt_multishift(x,shifts,amplitudes) - apply multiple shifts to matrix
- nt_vecmult y=nt_vecmult(x,v) - multiply all rows or columns of matrix by vector

This function is called by:

- nt_bias_cluster [c0,c1,A,todss,pwr0,pwr1]=nt_bias_cluster(x,dsr,flags) - cluster covariance
- nt_bias_fft [c0,c1]=nt_bias_fft(x,freq,nfft) - covariance with and w/o filter bias
- nt_bias_filter [c0,c1]=nt_bias_filter(x,B,A) - covariance with and w/o filter bias
- nt_cca_aad [D,E,R]=nt_cca_match_aad(xA,xB,y,ssize) - calculate metrics for match-mismatch task
- nt_cca_mm [D,E,R]=nt_cca_match_mm3(x,y,ssize) - calculate metrics for match-mismatch task
- nt_cluster_jd [IDX,todss,SCORE,COVS]=nt_cluster_jd(x,dsr,smooth,flags,init,verbose) - cluster with joint diagonalization
- nt_dprime [d,e]=nt_dprime(x,y,jd_flag) - calculate d' (discriminability) of two distributions
- nt_dprime_old [d,err_rate,perm]=nt_dprime(x,y,jd_flag) - calculate d' (discriminability) of two distributions
- nt_dss1 [todss,pwr0,pwr1]=nt_dss1(x,w,keep1,keep2) - evoked-biased DSS denoising
- nt_pca [z,idx]=nt_pca(x,shifts,nkeep,threshold,w) - time-shift pca
- nt_pca0 [topcs,pwr,y]=nt_pca0(x,shifts,nkeep,threshold,w) - time-shift pca
- nt_sns y=nt_sns(x,nneigbors,skip,w) - sensor noise suppression
- nt_split_jd [idx,score_vector,todss]=nt_split_dss(x,thresh,depth) - segmentation based on joint diagonalization
- nt_star [y,w,ww]=nt_star(x,thresh,closest,depth) - sensor noise suppression
- nt_star2 [y,w,ww]=nt_star2(x,thresh,closest,w) - sensor noise suppression
- nt_tsr [y,idx,w]=nt_tsr(x,ref,shifts,wx,wref,keep,thresh) - time-shift regression (TSPCA)
- nt_tsr_nodemean [y,idx,w]=nt_tsr_nodemean(x,ref,shifts,wx,wref,keep,thresh) - time-shift regression (TSPCA)
- nt_tsregress [z,idx]=nt_tsregress(x,y,shifts,xw,yw,keep,threshold) - time-shift regression

## SOURCE CODE

    0001 function [c,tw]=nt_cov(x,shifts,w);
    0002 %[c,tw]=nt_cov(x,shifts,w) - time shift covariance
    0003 %
    0004 %  c: covariance matrix
    0005 %  tw: total weight (c/tw is normalized covariance)
    0006 %
    0007 %  x: data
    0008 %  shifts: array of time shifts (must be non-negative)
    0009 %  w: weights
    0010 %
    0011 % If SHIFTS==[0], this function calculates the covariance matrix of X,
    0012 % weighted by W.  In the general case it calculates the covariance matrix
    0013 % of time-shifted X.
    0014 %
    0015 % X can be 1D, 2D or 3D.  It can also be a cell array.
    0016 % W can be 1D (if X is 1D or 2D) or 2D (if X is 3D). The same weight is
    0017 % applied to each column.
    0018 %
    0019 % Output is a 2D matrix with dimensions (ncols(X)*numel(SHIFTS))^2.
    0020 % It is made up of an ncols(X)*ncols(X) matrix of submatrices, each of
    0021 % dimensions numel(SHIFTS)*numel(SHIFTS).
    0022 %
    0023 % NoiseTools
    0024
    0025 %% arguments
    0026 if nargin<3; w=[]; end;
    0027 if nargin<2||isempty(shifts); shifts=0; end;
    0028 if prod(size(x))==0; error('data empty'); end
    0029 shifts=shifts(:);           % --> column vector
    0030 nshifts=numel(shifts);
    0031
    0032
    0033 if isempty(w)
    0034     %% no weights
    0035     if isnumeric(x)
    0036         %% matrix
    0037         [m,n,o]=size(x);
    0038         c=zeros(n*nshifts);
    0039         for k=1:o
    0040             xx=nt_multishift(x(:,:,k),shifts);
    0041             c=c+xx'*xx;
    0042         end
    0043         tw=size(xx,1)*o;
    0044     elseif iscell(x)
    0045         %% cell array
    0046         [m,n]=size(x{1});
    0047         o=length(x);
    0048         c=zeros(n*nshifts);
    0049         for k=1:o;
    0050             if size(x{k},2)~=n;  disp([size(x{k}),n, k]); error('!'); end
    0051             xx=nt_multishift(x{k}(:,:),shifts);
    0052             c=c+xx'*xx;
    0053         end
    0054         tw=size(xx,1)*o;
    0055     else error('!'); end
    0056 else
    0057     %% weights
    0058     if isnumeric(x)
    0059         %% matrix
    0060         [m,n,o]=size(x);
    0061         c=zeros(n*nshifts);
    0062         for k=1:o
    0063             if ~all(shifts == [0]);
    0064                 xx=nt_multishift(x(:,:,k),shifts);
    0065                 ww=nt_multishift(w(:,:,k),shifts);
    0066                 ww=min(ww,[],2);
    0067             else
    0068                 xx=x(:,:,k); ww=w(:,:,k);
    0069             end
    0070             xx=nt_vecmult(xx,ww);
    0071             c=c+xx'*xx;
    0072         end
    0073         tw=sum(w(:));
    0074     else
    0075         %% cell array
    0076         c=zeros(n*nshifts);
    0077         [m,n]=size(x{1});
    0078         o=length(x);
    0079         for k=1:o
    0080             if ~all(shifts == [0]);
    0081                 xx=nt_multishift(x{k}(:,:),shifts);
    0082                 ww=nt_multishift(w{k}(:,:),shifts);
    0083                 ww=min(ww,[],2);
    0084             else
    0085                 xx=x{k}(:,:); ww=w{k}(:,:);
    0086             end
    0087             xx=nt_vecmult(xx,ww);
    0088             c=c+xx'*xx;
    0089         end
    0090         tw=sum(w(:));
    0091     end
    0092 end
    0093