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

 mean is removed prior to processing

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

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 % mean is 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 [m,n,o]=size(x);
0025 
0026 % offset of z relative to x
0027 offset=max(0,-min(shifts));
0028 shifts=shifts+offset;           % adjust shifts to make them nonnegative
0029 idx=offset+(1:m-max(shifts));   % x(idx) maps to z
0030 
0031 % remove mean
0032 x=nt_fold(nt_demean(nt_unfold(x),w),m);
0033 
0034 % covariance
0035 if isempty(w);
0036     c=nt_cov(x,shifts);
0037 else
0038     if sum(w(:))==0; error('weights are all zero!'); end
0039     c=nt_cov(x,shifts,w);
0040 end
0041 
0042 % PCA matrix
0043 [topcs,evs]=nt_pcarot(c,nkeep);
0044 
0045 % truncate
0046 % if ~isempty(nkeep)
0047 %     topcs=topcs(:,1:nkeep);
0048 %     evs=evs(1:nkeep);
0049 % end
0050 if ~isempty (threshold)
0051     ii=find(evs/evs(1)>threshold);
0052     topcs=topcs(:,ii);
0053     evs=evs(ii);
0054 end
0055 
0056 %clf; plot(evs); set(gca,'yscale','log'); pause
0057 
0058 % apply PCA matrix to time-shifted data
0059 z=zeros(numel(idx),size(topcs,2),o);
0060 for k=1:o
0061     z(:,:,k)=nt_multishift(x(:,:,k),shifts)*topcs;
0062 end
0063 
0064

Generated on Mon 10-Nov-2014 14:40:42 by m2html © 2005