-
Notifications
You must be signed in to change notification settings - Fork 9
/
nt_pcarot.m
40 lines (35 loc) · 917 Bytes
/
nt_pcarot.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function [topcs,eigenvalues]=nt_pcarot(cov,nkeep,threshold,N)
% [topcs,eigenvalues]=pcarot(cov,nkeep,threshold,N) - PCA matrix from covariance
%
% topcs: PCA rotation matrix
% eigenvalues: PCA eigenvalues
%
% cov: covariance matrix
% nkeep: number of component to keep
% thresholds: discard components below this threshold
% N: eigs' K parameter (if absent: use eig)
%
% NoiseTools
if nargin<4; N=[]; end
if nargin<3; threshold=[]; end
if nargin<2; nkeep=[]; end
if ~isempty(N);
[V, S] = eigs(cov,N) ;
else
[V, S] = eig(cov) ;
end
V=real(V);
S=real(S);
[eigenvalues, idx] = sort(diag(S)', 'descend') ;
topcs = V(:,idx);
% truncate
if ~isempty (threshold)
ii=find(eigenvalues/eigenvalues(1)>threshold);
topcs=topcs(:,ii);
eigenvalues=eigenvalues(ii);
end
if ~isempty(nkeep)
nkeep=min(nkeep,size(topcs,2));
topcs=topcs(:,1:nkeep);
eigenvalues=eigenvalues(1:nkeep);
end