Home > NoiseTools > nt_pca.m

# nt_pca

## PURPOSE

[z,idx]=nt_pca(x,shifts,nkeep,threshold,w) - time-shift pca

## SYNOPSIS

function [z,idx]=nt_pca(x,shifts,nkeep,threshold,w)

## DESCRIPTION

    [z,idx]=nt_pca(x,shifts,nkeep,threshold,w) - time-shift pca

      z: pcs
      idx: x(idx) maps to z

      x: data matrix
      shifts: array of shifts to apply
      keep: number of components shifted regressor PCs to keep (default: all)
      threshold: discard PCs with eigenvalues below this (default: 0)
      w: weights

     Beware: mean is NOT removed prior to processing.

## CROSS-REFERENCE INFORMATION

This function calls:

- nt_cov [c,tw]=nt_cov(x,shifts,w) - time shift covariance
- nt_greetings nt_greetings - display message the first time the toolbox is used
- nt_multishift z=nt_multishift(x,shifts,amplitudes) - apply multiple shifts to matrix
- nt_pcarot [topcs,eigenvalues]=pcarot(cov,nkeep,threshold,N) - PCA matrix from covariance

This function is called by:

- nt_fixsign y=nt_fixsign(x) - flip signs to maximize inter-component correlation
- nt_sns1 y=nt_sns1(x,nneigbors,skip,w,threshold) - sensor noise suppression
- nt_split_jd [idx,score_vector,todss]=nt_split_dss(x,thresh,depth) - segmentation based on joint diagonalization
- nt_tsr [y,idx,w]=nt_tsr(x,ref,shifts,wx,wref,keep,thresh) - time-shift regression (TSPCA)
- nt_zapline [y,yy]=nt_zapline(x,fline,nremove,p,plotflag) - remove power line artifact

## SOURCE CODE

    0001 function [z,idx]=nt_pca(x,shifts,nkeep,threshold,w)
    0002 %[z,idx]=nt_pca(x,shifts,nkeep,threshold,w) - time-shift pca
    0003 %
    0004 %  z: pcs
    0005 %  idx: x(idx) maps to z
    0006 %
    0007 %  x: data matrix
    0008 %  shifts: array of shifts to apply
    0009 %  keep: number of components shifted regressor PCs to keep (default: all)
    0010 %  threshold: discard PCs with eigenvalues below this (default: 0)
    0011 %  w: weights
    0012 %
    0013 % Beware: mean is NOT removed prior to processing.
    0014
    0015 % TODO: reimplement using nt_pca0
    0016 nt_greetings;
    0017
    0018 if nargin<1; error('!'); end
    0019 if nargin<2||isempty(shifts); shifts=[0]; end
    0020 if nargin<3; nkeep=[]; end
    0021 if nargin<4; threshold=[]; end
    0022 if nargin<5; w=[]; end
    0023
    0024 if isnumeric(x)
    0025     [m,n,o]=size(x);
    0026 else
    0027     [m,n]=size(x{1});
    0028     o=length(x);
    0029 end
    0030
    0031
    0032 % offset of z relative to x
    0033 offset=max(0,-min(shifts));
    0034 shifts=shifts+offset;           % adjust shifts to make them nonnegative
    0035 idx=offset+(1:m-max(shifts));   % x(idx) maps to z
    0036
    0037 % % remove mean
    0038 % x=nt_fold(nt_demean(nt_unfold(x),w),m);
    0039
    0040 % covariance
    0041 c=nt_cov(x,shifts,w);
    0042
    0043 % PCA matrix
    0044 [topcs,evs]=nt_pcarot(c,nkeep,threshold);
    0045
    0046 %clf; plot(evs); set(gca,'yscale','log'); pause
    0047
    0048 % apply PCA matrix to time-shifted data
    0049 if isnumeric(x)
    0050     z=zeros(numel(idx),size(topcs,2),o);
    0051     for k=1:o
    0052         z(:,:,k)=nt_multishift(x(:,:,k),shifts)*topcs;
    0053     end
    0054 else
    0055     z=[];
    0056     for k=1:o
    0057         z{k}(:,:)=nt_multishift(x{k}(:,:),shifts)*topcs;
    0058     end
    0059 end
    0060
    0061