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

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

Generated on Mon 01-Oct-2018 16:21:39 by m2html © 2005