-
Notifications
You must be signed in to change notification settings - Fork 6
/
CKF.m
105 lines (62 loc) · 2.65 KB
/
CKF.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
classdef CKF
properties
Q;
R;
nx;
xkk1;
Pkk1;
xkk;
Pkk;
end
methods
% Constructor
function obj = CKF(Q, R, nx)
obj.Q = Q;
obj.R = R;
obj.nx = nx;
end
% Propagation
function obj = Predict(obj, xkk, Pkk)
obj.xkk1 = xkk;
obj.Pkk1 = Pkk + obj.Q;
end
% Correction
function obj = Update(obj, z)
R = obj.R;
xkk1 = obj.xkk1;
Pkk1 = obj.Pkk1;
%%%========================================================================
%%% Genrate a set of Cubature Points
%%%========================================================================
nx = obj.nx; % state vector dimension
nPts = 2*nx; % No. of Cubature Points
CPtArray = sqrt(nPts/2)*[eye(nx) -eye(nx)];
%%%========================================================================
%%% find the square-root of Pkk1 using Singular Value Decomposition (SVD)
%%%========================================================================
Pkk1 = 0.5*(Pkk1+Pkk1'); % make Pkk1 to be symmetric (Property of a covariance matrix !)
[U, S, V] = svd(Pkk1);
Skk1 = 0.5*(U+V)*sqrt(S);
%%%========================================================================
Xi = repmat(xkk1,1,nPts) + Skk1*CPtArray;
Zi = MstEq(Xi);
zkk1 = sum(Zi,2)/nPts; % predicted Measurement
X = (Xi-repmat(xkk1,1,nPts))/sqrt(nPts);
Z = (Zi-repmat(zkk1,1,nPts))/sqrt(nPts);
Pzz = Z*Z'+ R; % Innovations Covariance
Pxz = X*Z'; % cross-covariance
G = Pxz*pinv(Pzz); % Cubature Kalman Gain
xkk = xkk1 + G*(z - zkk1);
Pkk = Pkk1 - G*Pzz*G';
% Save objects
obj.xkk = xkk;
obj.Pkk = Pkk;
end
end
methods(Static)
function z = MstEq(x)
z = [ 0.8*cos(x(1,:)) - 0.2*cos(x(1,:) + x(2,:)) ;
0.8*sin(x(1,:)) - 0.2*sin(x(1,:) + x(2,:)) ];
end
end
end