-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_klip_basis.pro
90 lines (52 loc) · 1.7 KB
/
get_klip_basis.pro
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
FUNCTION GET_KLIP_BASIS, image_arr, k_klip
;Function that returns the KL basis of input matrix
;IMAGE_ARR - Data matrix : N_obj (columns) X M_attrib (rows)
;K_KLIP - Integer number of KL Basis Vectors to keep
;Data Matix "R"
R = image_arr
;Get Dimensions
R_Dimen = (size(image_arr))(0)
n_col = (size(R))(1)
n_row = (size(R))(2)
;Make sure we read the correct dimensions!
if R_Dimen eq 1 then begin
n_col = (size(R))(1) ; size now returns col as arg. (1)
n_row = 1.0
endif
; Constants
TOLERANCE = 5.0E-3 ; Close enough to zero?
;Subtract the mean of each object from itself
for ii = 0,n_col-1 do begin
R[ii,*] = R[ii,*] - total(R[ii,*])/n_row
endfor
;We want the eigendecomposition of RR'
A = R # transpose(R)
eigenval = eigenql(A, EIGENVECTORS = eigenvect, /double)
n_eig = n_elements(eigenval)
eig_val_arr = fltarr(n_eig,n_eig)
for jj = 0, n_elements(eigenval)-1 do begin
if abs(eigenval[jj]) lt TOLERANCE then eigenval[jj] = 0.0
eig_val_arr[jj,jj] = eigenval[jj]
if sqrt(eigenval[jj]) ne 0 then begin
eigenvect[*,jj] = eigenvect[*,jj]/sqrt(abs(eigenval[jj])) ; normalize
endif
endfor
new_basis = matrix_multiply(eigenvect,R,/atranspose) ; kth column is kth principle component of R
;counter = 0
;percent = 0.0
;while percent lt max_percent do begin
; percent = percent+eigenval[counter]/total(eigenval)*100
; counter = counter+1
;endwhile
;k_klip = counter
n_rows = (size(new_basis))(2)
;check if 1d representation
new_basis_Dimen = (size(new_basis))(0)
if new_basis_Dimen eq 1 then n_rows = 1
kl_basis = fltarr(k_klip,n_rows)
kl_basis[*] = !values.f_nan
for jj = 0,k_klip-1 do begin
kl_basis[jj,*] = new_basis[jj,*]
endfor
return, kl_basis
END