0001 function [z,idx]=nt_pca(x,shifts,nkeep,threshold,w)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
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
0027 offset=max(0,-min(shifts));
0028 shifts=shifts+offset;
0029 idx=offset+(1:m-max(shifts));
0030
0031
0032 x=nt_fold(nt_demean(nt_unfold(x),w),m);
0033
0034
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
0043 [topcs,evs]=nt_pcarot(c,nkeep);
0044
0045
0046
0047
0048
0049
0050 if ~isempty (threshold)
0051 ii=find(evs/evs(1)>threshold);
0052 topcs=topcs(:,ii);
0053 evs=evs(ii);
0054 end
0055
0056
0057
0058
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