-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathekf.m
81 lines (68 loc) · 2.24 KB
/
ekf.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
function [x_hat, P] = ekf(t_km1, t_k, x_hat, P, u, z, ...
f, h, F_fcn, H_fcn, Q, R)
% ekf
%
% Runs a single iteration of an extended Kalman filter.
%
% Inputs:
%
% t_km1 Time at sample k-1
% t_k Time at sample k
% x_hat Estimate at sample k-1 (nx-by-1)
% P Estimate covariance matrix at sample k-1 (nx-by-nx)
% u Input vector (nu-by-1)
% z Measurement (nz-by-1)
% f Handle of the propagation function, mapping a state at time
% t_km1 to t_k, with the following interface:
%
% x_k = f(t_km1, t_k, x_km1, u);
%
% h Observation function, mapping a state to a predicted
% measurement, with the following interface:
%
% z_hat_k = h(t_k, x_k, u);
%
% F_fcn Function producing the Jacobian of the propagation function from
% time t_km1 to t_k:
%
% F = F_fcn(t_km1, t_k, x_km1, u);
%
% H_fcn Function producing the Jacobian of the observation function:
%
% H = H_fcn(t_k, x_k, u);
%
% Q Process noise covariance matrix (nx-by-nx)
% R Measurement noise covariance matrix (nz-by-nz)
%
% Outputs:
%
% x_hat Estimate at sample k
% P Estimate covariance matrix at sample k
%
% Copyright 2016 An Uncommon Lab
% Calculate the Jacobian using the state at k-1.
F = F_fcn(t_km1, t_k, x_hat, u);
% Propagate the estimate.
x_hat = f(t_km1, t_k, x_hat, u);
% Propagate the covariance.
P = F * P * F.' + Q;
% Predict the measurement using the predicted state estimate.
z_hat = h(t_k, x_hat, u);
% Innovation vector
y = z - z_hat;
% Calculate the observation Jacobian using the predicted state
% estimate.
H = H_fcn(t_k, x_hat, u);
% Calculate the state-innovation covariance and innovation covariance.
P_xy = P * H.';
P_yy = H * P * H.' + R;
% Calculate the Kalman gain.
K = P_xy / P_yy;
% Correct the estimate.
x_hat = x_hat + K * y;
% Correct the covariance using Joseph form for stability. This is the
% same as P = P - K * H * P, but presents less of a problem in the
% presense of floating point roundoff.
A = eye(length(x_hat)) - K * H;
P = A * P * A.' + K * R * K.';
end % ekf