Home > NoiseTools > nt_cov2.m

# nt_cov2

## PURPOSE

[c,tw]=nt_cov2(x,w) - weighted covariance

## SYNOPSIS

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

## DESCRIPTION

    [c,tw]=nt_cov2(x,w) - weighted covariance

      c: covariance matrix
      tw: total weight

      x: data
      w: weights

     X can be 1D, 2D or 3D.
     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_unfold y=nt_fold(x) - unfold 3D to 2D

This function is called by:

- nt_tsregress [z,idx]=nt_tsregress(x,y,shifts,xw,yw,keep,threshold) - time-shift regression

## SOURCE CODE

    0001 function [c,tw]=nt_cov2(x,w);
    0002 %[c,tw]=nt_cov2(x,w) - weighted covariance
    0003 %
    0004 %  c: covariance matrix
    0005 %  tw: total weight
    0006 %
    0007 %  x: data
    0008 %  w: weights
    0009 %
    0010 % X can be 1D, 2D or 3D.
    0011 % W can be 1D (if X is 1D or 2D) or 2D (if X is 3D). The same weight is
    0012 % applied to each column.
    0013 %
    0014 % Output is a 2D matrix with dimensions (ncols(X)*numel(SHIFTS))^2.
    0015 % It is made up of an ncols(X)*ncols(X) matrix of submatrices, each of
    0016 % dimensions numel(SHIFTS)*numel(SHIFTS).
    0017 %
    0018 % NoiseTools
    0019
    0020 if nargin<2; w=[]; end;
    0021 if prod(size(x))==0; error('data empty'); end
    0022
    0023 x=nt_unfold(x);
    0024 w=nt_unfold(w);
    0025
    0026 if isempty(w); w=ones(size(x)); end
    0027 if size(w,1)~=size(x,1); error ('!'); end
    0028 if size(w,2)==1;
    0029     w=repmat(w,[1,size(x,2)]);
    0030 elseif size(w,2)~=size(x,2);
    0031     error('!');
    0032 end
    0033
    0034 c=zeros(size(x,2));
    0035 if isempty(w)
    0036     % no weights
    0037
    0038     c=x'*x;
    0039     tw=size(x,1)*ones(size(c));
    0040
    0041 else
    0042     % weights
    0043     x=x.*w;
    0044     c=x'*x;
    0045     tw=w'*w;
    0046 end
    0047