-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquareRootARKalmanFilter.m
More file actions
324 lines (296 loc) · 13.1 KB
/
SquareRootARKalmanFilter.m
File metadata and controls
324 lines (296 loc) · 13.1 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
function [x_kf varargout] = SquareRootARKalmanFilter(z,M,MAlen,varargin)
%--------------------------------------------------------------------------
% Syntax: x_kf = SquareRootARKalmanFilter(z,M,MAlen,N);
% x_kf = SquareRootARKalmanFilter(z,M,MAlen,'EWMA');
% x_kf = SquareRootARKalmanFilter(z,M,MAlen,N,'EWMA');
% x_kf = SquareRootARKalmanFilter(z,M,MAlen,N,'UWMA');
% [x_kf KF] = SquareRootARKalmanFilter(z,M,MAlen,N);
% [x_kf KF] = SquareRootARKalmanFilter(z,M,MAlen,'EWMA');
% [x_kf KF] = SquareRootARKalmanFilter(z,M,MAlen,N,'EWMA');
% [x_kf KF] = SquareRootARKalmanFilter(z,M,MAlen,N,'UWMA');
%
% Inputs: z is an m x n matrix containing n samples of an
% m-dimensional signal
%
% M is the desired model order of the auto-regressive (AR)
% model used to model the state transition.
%
% MAlen is the length of the moving average to apply to z
% (used during covariance estimation)
%
% N is the length of the lookback window to use during
% covariance estimation. When N is specified and nargin == 4,
% this function applies an exponentially weighted moving
% average (with memory N) during covariance estimation
%
% When nargin == 4, 'EWMA' instructs this function to use
% exponentially weighted (recursive) covariance estimation
%
% When nargin == 5, 'EWMA' instructs this function to apply
% an exponentially weighted moving average with (memory N) to
% z during covariance estimation. Alternatively, 'UWMA'
% applies an unweighted moving average of length N to z
%
% Outputs: x_kf is an m x n matrix containing the Kalman filter
% "true state" estimate derived from noisy measurements z.
%
% KF is a struct containing the following fields:
%
% KF.x_pr - Apriori state estimates
% KF.a_pr - Apriori AR coefficients
% KF.a_po - Aposteriori AR coefficients
% KF.UP_pr - Apriori error covariance estimates (U)
% KF.DP_pr - Apriori error covariance estimates (D)
% KF.UPa_pr - Apriori AR coefficients error covariance (U)
% KF.DPa_pr - Apriori AR coefficients error covariance (D)
% KF.UP_po - Aposteriori error covariance estimates (U)
% KF.DP_po - Aposteriori error covariance estimates (D)
% KF.UPa_po - Aposteriori AR coefficient error covariance (U)
% KF.DPa_po - Aposteriori AR coefficient error covariance (D)
% KF.UQ - Process covariance estimates (U)
% KF.DQ - Process covariance estimates (D)
% KF.UQa - AR coefficients state covariance estimates (U)
% KF.DQa - AR coefficients state covariance estimates (D)
% KF.R - Noise covariance estimates
% KF.UR - Noise covariance estimates (U)
% KF.DR - Noise covariance estimates (D)
% KF.Ra - AR coefficients noise covariance estimates
% KF.URa - AR coefficients noise covariance estimates (U)
% KF.DRa - AR coefficients noise covariance estimates (D)
%
% Description: This function performs square root Kalman auto-regressive
% (AR) filtering on noisy input data z. The assumed system
% model is that the noisy measurements (z) = linear
% combination of M previous true state (x) samples + white
% noise. This function estimates the process/noise
% covariances of the input data at each iteration using a
% smoothed version of z as a surrogate for the true process
% state.
%
% NOTE: This square root implementation of the Kalman AR
% filter produces the same output as StandardARKalmanFilter()
% except when the noisy measurements are poorly-conditioned,
% in which case SquareRootARKalmanFilter() prodcues a MORE
% NUMERICALLY STABLE output (at the cost of greater
% computational complexity.)
%
% NOTE: "Square root" refers to the fact that, instead of
% computing the error covariance P at each iteration, this
% function computes U and D such that [U D] = myUD(P). This
% strategy is known to be more numerically stable.
%
% Author: Brian Moore
% brimoor@umich.edu
%
% Date: July 12, 2012
%--------------------------------------------------------------------------
% Parse user inputs
if (nargin == 4)
if ~ischar(varargin{1})
N = varargin{1};
MAType = 'EWMA';
CovMethod = 'Standard';
else
N = MAlen;
MAType = 'EWMA';
CovMethod = 'EWMA';
end
elseif (nargin == 5)
N = varargin{1};
MAType = varargin{2};
CovMethod = 'Standard';
else
error('Input syntax error. Type ''help SquareRootARKalmanFilter'' for assistance.');
end
% AR Coefficient memory
MEM_RATE = 0.75; % 0 = No memory, 1 = Infinite memory
%--------------------------------------------------------------------------
% State variable initializations
%--------------------------------------------------------------------------
% Data dimension
DIM = size(z,1);
% Number of iterations
n = size(z,2);
% Apriori state estimates
x_apriori = zeros(DIM*M,n);
a_apriori = zeros(DIM*M,n);
% Aposteriori state estimates
x_aposteriori = zeros(DIM*M,n);
a_aposteriori = zeros(DIM*M,n);
% Apriori error covariance estimates
UP_apriori = zeros(DIM*M,DIM*M,n);
DP_apriori = zeros(DIM*M,DIM*M,n);
UPa_apriori = zeros(DIM*M,DIM*M,n);
DPa_apriori = zeros(DIM*M,DIM*M,n);
% Aposteriori error covariance estimates
UP_aposteriori = zeros(DIM*M,DIM*M,n);
DP_aposteriori = zeros(DIM*M,DIM*M,n);
UPa_aposteriori = zeros(DIM*M,DIM*M,n);
DPa_aposteriori = zeros(DIM*M,DIM*M,n);
% Process variance estimates
UQ = zeros(DIM*M,DIM*M,n);
DQ = zeros(DIM*M,DIM*M,n);
UQa = eye(DIM*M);
DQa = eye(DIM*M);
% Measurement variance estimates
R = zeros(DIM,DIM,n);
UR = zeros(DIM,DIM,n);
DR = zeros(DIM,DIM,n);
Ra = zeros(DIM,DIM,n);
URa = zeros(DIM,DIM,n);
DRa = zeros(DIM,DIM,n);
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% More initializations
%--------------------------------------------------------------------------
% MA smoothed measurements
eval(['smoothed_z = ' MAType '(z,MAlen);']);
% Simulation start index
startIndex = N+MAlen-1;
% Initial aposteriori state estimates
for i = 1:DIM
for j = 1:M
for k = 1:M
x_aposteriori((i-1)*M+j,startIndex-k) = smoothed_z(i,startIndex-(j-1)-k);
end
end
a_aposteriori((i-1)*M+1,startIndex-1) = 1;
end
% Initial aposteriori error covariance estimate
UP_aposteriori(:,:,startIndex-1) = eye(DIM*M);
DP_aposteriori(:,:,startIndex-1) = eye(DIM*M);
UPa_aposteriori(:,:,startIndex-1) = eye(DIM*M);
DPa_aposteriori(:,:,startIndex-1) = eye(DIM*M);
% Model constants
Ai = zeros(DIM*M);
for j = 1:DIM
for k = 2:M
Ai((j-1)*M+k,(j-1)*M+k-1) = 1;
end
end
H = zeros(DIM,DIM*M);
for j = 1:DIM
H(j,(j-1)*M+1) = 1;
end
Ha = zeros(DIM,DIM*M);
%--------------------------------------------------------------------------
%--------------------------------------------------------------------------
% Perform Kalman filtering
%--------------------------------------------------------------------------
Qtemp = zeros(DIM);
for i = startIndex:n
%----------------------------------------------------------------------
% Estimate noise/process covariances
%----------------------------------------------------------------------
if strcmpi(CovMethod,'Standard')
[R(:,:,i) Qtemp] = StandardCovEst(z,smoothed_z,i,N);
else
[R(:,:,i) Qtemp] = EWMACovEst(z,smoothed_z,i,N,R(:,:,i-1),Qtemp);
end
% Decay AR model variance
DQa = (1 - MEM_RATE) * DQa;
Ra(:,:,i) = R(:,:,i) + Qtemp;
%----------------------------------------------------------------------
%----------------------------------------------------------------------
% Update Ha
%----------------------------------------------------------------------
for j = 1:DIM
Ha(j,(M*(j-1)+1):(M*j)) = x_aposteriori(((j-1)*M+1):(j*M),(i-1));
end
%----------------------------------------------------------------------
%----------------------------------------------------------------------
% Square root Kalman filter the AR parameters
%----------------------------------------------------------------------
try
% Update UD decompositions
[URa(:,:,i) DRa(:,:,i)] = myUD(Ra(:,:,i));
% Time update
[a_apriori(:,i) UPa_apriori(:,:,i) DPa_apriori(:,:,i)] = thornton(a_aposteriori(:,i-1),UPa_aposteriori(:,:,i-1),DPa_aposteriori(:,:,i-1),UQa,DQa);
% Decorrelate measurements
z_ind = myUnitTriSysSol(URa(:,:,i),z(:,i),'upper');
H_ind = myUnitTriSysSol(URa(:,:,i),Ha,'upper');
% Measurement Update
a_aposteriori(:,i) = a_apriori(:,i);
UPa_aposteriori(:,:,i) = UPa_apriori(:,:,i);
DPa_aposteriori(:,:,i) = DPa_apriori(:,:,i);
for j = 1:DIM
[a_aposteriori(:,i) UPa_aposteriori(:,:,i) DPa_aposteriori(:,:,i)] = bierman(z_ind(j),DRa(j,j,i),H_ind(j,:),a_aposteriori(:,i),UPa_aposteriori(:,:,i),DPa_aposteriori(:,:,i));
end
catch e
%disp(e.message);
a_aposteriori(:,i) = a_aposteriori(:,i-1);
UPa_aposteriori(:,:,i) = UPa_aposteriori(:,:,i-1);
DPa_aposteriori(:,:,i) = DPa_aposteriori(:,:,i-1);
end
%----------------------------------------------------------------------
%----------------------------------------------------------------------
% Update Ai
%----------------------------------------------------------------------
for j = 1:DIM
Ai((j-1)*M+1,((j-1)*M+1):(j*M)) = a_aposteriori(((j-1)*M+1):(j*M),i)';
end
%----------------------------------------------------------------------
%----------------------------------------------------------------------
% Square root Kalman filter the data
%----------------------------------------------------------------------
try
% Update UD decompositions
[UQtemp DQtemp] = myUD(Qtemp);
[UR(:,:,i) DR(:,:,i)] = myUD(R(:,:,i));
for j = 1:DIM
for k =1:DIM
UQ(M*(j-1)+1,M*(k-1)+1,i) = UQtemp(j,k);
DQ(M*(j-1)+1,M*(k-1)+1,i) = DQtemp(j,k);
end
end
% Time update
[x_apriori(:,i) UP_apriori(:,:,i) DP_apriori(:,:,i)] = thornton(x_aposteriori(:,i-1),UP_aposteriori(:,:,i-1),DP_aposteriori(:,:,i-1),UQ(:,:,i),DQ(:,:,i),Ai);
% Decorrelate measurements
z_ind = myUnitTriSysSol(UR(:,:,i),z(:,i),'upper');
H_ind = myUnitTriSysSol(UR(:,:,i),H,'upper');
% Measurement Update
x_aposteriori(:,i) = x_apriori(:,i);
UP_aposteriori(:,:,i) = UP_apriori(:,:,i);
DP_aposteriori(:,:,i) = DP_apriori(:,:,i);
for j = 1:DIM
[x_aposteriori(:,i) UP_aposteriori(:,:,i) DP_aposteriori(:,:,i)] = bierman(z_ind(j),DR(j,j,i),H_ind(j,:),x_aposteriori(:,i),UP_aposteriori(:,:,i),DP_aposteriori(:,:,i));
end
catch e
%disp(e.message);
for j = 1:DIM
for k = M:-1:2
x_aposteriori((j-1)*M+k,i) = x_aposteriori((j-1)*M+(k-1),i-1);
end
x_aposteriori((j-1)*M+1,i) = z(j,i);
end
UP_aposteriori(:,:,i) = eye(DIM*M);
DP_aposteriori(:,:,i) = eye(DIM*M);
end
%----------------------------------------------------------------------
end
%--------------------------------------------------------------------------
% Return user requested data
x_kf = x_aposteriori;
if (nargout == 2)
varargout{1} = struct('x_pr',x_apriori, ...
'a_pr',a_apriori, ...
'a_po',a_aposteriori, ...
'UP_pr',UP_apriori, ...
'DP_pr',DP_apriori, ...
'UPa_pr',UPa_apriori, ...
'DPa_pr',DPa_apriori, ...
'UP_po',UP_aposteriori, ...
'DP_po',DP_aposteriori, ...
'UPa_po',UPa_aposteriori, ...
'DPa_po',DPa_aposteriori, ...
'UQ',UQ, ...
'DQ',DQ, ...
'UQa',UQa, ...
'DQa',DQa, ...
'R',R, ...
'UR',UR, ...
'DR',DR, ...
'Ra',Ra, ...
'URa',URa, ...
'DRa',DRa);
end