-
Notifications
You must be signed in to change notification settings - Fork 5
/
fdavpca.m
173 lines (161 loc) · 5.91 KB
/
fdavpca.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
classdef fdavpca
%fdavpca A class to provide vertical fPCA
% -------------------------------------------------------------------------
% This class provides vertical fPCA using the
% SRVF framework
%
% Usage: obj = fdavpca(warp_data)
%
% where:
% warp_data - fdawarp object of aligned data
%
%
% fdavpca Properties:
% warp_data - fdawarp class with alignment data
% q_pca - srvf principal directions
% f_pca - f principal directions
% latent - latent values
% coef - prinicapl coefficients
% id - point used for f(0)
% mqn - mean srvf
% U - eigenvectors
% stds - geodesic directions
%
%
% fdavpca Methods:
% fdavpca - class constructor
% calc_fpca - perform vertical fPCA
% plot - plot results and functions in object
%
%
% Author : J. D. Tucker (JDT) <jdtuck AT sandia.gov>
% Date : 15-Mar-2018
properties
warp_data % fdawarp class with alignment data
q_pca % srvf principal directions
f_pca % f principal directions
latent % latent values
coef % prinicapl coefficients
id % point used for f(0)
mqn % mean srvf
U % eigenvectors
stds % geodesic directions
end
methods
function obj = fdavpca(fdawarp)
%fdavpca Construct an instance of this class
% Input:
% fdawarp: fdawarp class
if (isempty(fdawarp.fn))
error('Please align fdawarp class using time_warping!');
end
obj.warp_data = fdawarp;
end
function obj = calc_fpca(obj,no,id)
% CALC_FPCA Vertical Functional Principal Component Analysis
% -------------------------------------------------------------------------
% This function calculates vertical functional principal component analysis
% on aligned data
%
% Usage: obj.calc_fpca(no)
% obj.calc_fpca(no,id)
%
% Inputs:
% warp_data: struct from time_warping of aligned data
% no: number of principal components to extract
% id: point to use for f(0) (default = midpoint)
%
% Output:
% fdavpca object
fn = obj.warp_data.fn;
t = obj.warp_data.time;
qn = obj.warp_data.qn;
if nargin < 2
no = 3;
id = round(length(t)/2);
obj.id = id;
elseif nargin < 3
id = round(length(t)/2);
obj.id = id;
else
obj.id = id;
end
% Parameters
coefs = -2:2;
NP = 1:no; % number of principal components
Nstd = length(coefs);
% FPCA
mq_new = mean(qn,2);
m_new = sign(fn(obj.id,:)).*sqrt(abs(fn(obj.id,:))); % scaled version
obj.mqn = [mq_new; mean(m_new)];
K = cov([qn;m_new]');
[obj.U,S,~] = svd(K);
s = diag(S);
stdS = sqrt(s);
s = s(1:no);
obj.U = obj.U(:,1:no);
% compute the PCA in the q domain
obj.q_pca = zeros(length(mq_new)+1,Nstd,no);
for k = NP
for i = 1:Nstd
obj.q_pca(:,i,k) = [mq_new; mean(m_new)] + coefs(i)*stdS(k)*obj.U(:,k);
end
end
% compute the correspondence to the original function domain
obj.f_pca = zeros(length(mq_new),Nstd,no);
for k = NP
for i = 1:Nstd
obj.f_pca(:,i,k) = cumtrapzmid(t,obj.q_pca(1:end-1,i,k).*abs(obj.q_pca(1:end-1,i,k)),sign(obj.q_pca(end,i,k)).*(obj.q_pca(end,i,k).^2), obj.id);
end
fbar = mean(fn,2);
fsbar = mean(obj.f_pca(:,:,k),2);
err = repmat(fbar-fsbar,1,Nstd);
obj.f_pca(:,:,k) = obj.f_pca(:,:,k) + err;
end
% coefficients
c = zeros(size(qn,2),no);
for jj = NP
for ii = 1:size(qn,2)
c(ii,jj) = dot([qn(:,ii);m_new(ii)]-obj.mqn,obj.U(:,jj));
end
end
obj.latent = s;
obj.coef = c;
obj.stds = coefs;
end
function plot(obj)
% plot plot elastic vertical fPCA results
% -------------------------------------------------------------
% Usage: obj.plot()
cl = 'rbgmc';
[~, ~, p1] = size(obj.q_pca);
num_plot = ceil(p1/3);
k = 1;
for ii = 1:num_plot
if (k > size(obj.q_pca,3))
break
end
figure;
for k1 = 1:3
k = k1+(ii-1)*3;
subplot(2,3,k1);
if (k > size(obj.q_pca,3))
break
end
for i = 1:length(obj.stds)
plot(obj.warp_data.time, obj.q_pca(1:end-1,i,k), cl(i), 'linewidth', 2); hold on;
end
title(['q domain: PD ' num2str(k)], 'fontsize', 14);
subplot(2,3,k1+3);
for i = 1:length(obj.stds)
plot(obj.warp_data.time, obj.f_pca(:,i,k), cl(i), 'linewidth', 2); hold on;
end
title(['f domain: PD ' num2str(k)], 'fontsize', 14);
end
end
cumm_coef = 100*cumsum(obj.latent)./sum(obj.latent);
figure
plot(cumm_coef);title('Coefficient Cumulative Percentage');ylabel('Percentage');xlabel('Index')
end
end
end