KalmanFilter
ukf2.m
Go to the documentation of this file.
1 function [estimates, covariances] = ukf2(f_func, state_count, sensor_count, measurement_count,C,Q,R,P_0,x_0, measurements)
2 %_Faster_ Unscented Kalman filter implementation.
3 %[estimates, covariances] = ukf2(f_func, state_count, sensor_count, measurement_count,C,Q,R,P_0,x_0, measurements)
4 %INPUT
5 % f_func: function when given a state, produces the next state
6 % x_{k+1} = f_func(x_{k})
7 %
8 % state_count: length of state
9 %
10 % sensor_count:amount of measurements
11 %
12 % measurement_count: total number of measurements
13 %
14 % C: observation function that takes in state X_k and index k and returns
15 % appropriate measurement: observation = C(X_k,k)
16 %
17 % Q: function that takes in argument index k and returns state_count by
18 % state_count process noise matrix: Q_k = Q(k)
19 %
20 % R: function that takes in argument index k and returns sensor_count by
21 % sensor_count sensor noise covariance: R_k = R(k)
22 %
23 % P_0: state_count by state_count initial covariance
24 %
25 % x_0: initial estimate of state (state_count by 1)
26 %
27 % measurements: sensor_count by measurement_count matrix where the k^th
28 % column is the k^th measurement
29 %
30 %OUTPUT
31 % estimates: state_count by (measurement_count + 1) matrix where the
32 % k^th column is the k^th estimate (first column is x_0)
33 %
34 % covariances: 1 by (measurement_count + 1) cell count where the kth
35 % index is the computed (state_count by state_count) covariance matrix
36 % (first entry is P_0)
37 
38 
39 assert(state_count == size(x_0,1));
40 for i=1:measurement_count
41  assert(size(C,1) == sensor_count & size(C,2) == state_count);
42  assert(size(Q,1) == state_count & size(Q,2) == state_count);
43  assert(size(R,1) == sensor_count & size(R,2) == sensor_count);
44 end
45 assert(size(P_0,1) == state_count & size(P_0,2) == state_count);
46 assert(size(x_0,1) == state_count & size(x_0,2) == 1);
47 assert(size(measurements,1) == sensor_count & size(measurements,2) == measurement_count);
48 assert(size(f_func(zeros(state_count,1)),1) == state_count & size(f_func(zeros(state_count,1)),2) == 1);
49 
50 
51 P_update = P_0;
52 x_update = x_0;
53 covariances = cell(1,measurement_count+1);
54 estimates = zeros(state_count,measurement_count+1);
55 covariances{1,1} = P_0;
56 estimates(:,1) = x_0;
57 
58 sigma_points = zeros(state_count,state_count*2+1);
59 propagated = zeros(state_count,state_count*2,+1);
60 measurement_points = zeros(sensor_count,state_count*2+1);
61 cross_covariance = zeros(state_count,sensor_count);
62 Z_cov = zeros(sensor_count,sensor_count);
63 P_cov = zeros(state_count,state_count);
64 
65 alpha = 1;
66 beta = 2;
67 KK = 0;
68 lambda = alpha^2*(state_count+KK) - state_count;
69 
70 n_0_m = (lambda/(state_count + lambda));
71 n_i_m = 0.5*(1/(state_count + lambda));
72 n_0_c = (lambda/(state_count+lambda) + 1 - alpha^2 + beta);
73 n_i_c = n_i_m;
74 
75 for ii=1:measurement_count
76  P_sqrt = chol(P_update)';
77  sigma_points = x_update + (sqrt(state_count + lambda).*[zeros(state_count,1),P_sqrt, -P_sqrt]);
78 
79  propagated = f_func(sigma_points);
80  measurement_points = C*sigma_points;
81 
82  x_predict = n_0_m.*propagated(:,1) + n_i_m.*(sum(propagated(:,2:end),2));
83  z_predict = n_0_m.*measurement_points(:,1) + n_i_m.*(sum(measurement_points(:,2:end),2));
84 
85 
86  Z_cov = n_0_c.*(z_predict - measurement_points(:,1))*(z_predict - measurement_points(:,1))';
87  P_cov = n_0_c.*(x_predict - propagated(:,1))*(x_predict-propagated(:,1))';
88  cross_covariance = n_0_c.*(x_predict - propagated(:,1))*(z_predict - measurement_points(:,1))';
89 
90 
91  diff_z = measurement_points(:,2:end) - z_predict;
92  diff_x = propagated(:,2:end) - x_predict;
93  Z_cov = Z_cov + n_i_c.*diff_z*(diff_z');
94  P_cov = P_cov + n_i_c.*diff_x*(diff_x');
95  cross_covariance = cross_covariance + n_i_c.*(diff_x*(diff_z'));
96 
97  P_cov = P_cov + Q;
98  P_cov = 0.5.*(P_cov + P_cov');
99  Z_cov = Z_cov + R;
100  Z_cov = 0.5*(Z_cov + Z_cov');
101 
102  %update phase
103  Kalman_gain = cross_covariance*inv(Z_cov);
104  x_update = x_predict + Kalman_gain*(measurements(:,ii) - z_predict);
105 
106  P_update = P_cov - Kalman_gain*Z_cov*(Kalman_gain');
107  covariances{1,ii+1} = P_update;
108  estimates(:,ii+1) = x_update;
109 end
function ukf2(in f_func, in state_count, in sensor_count, in measurement_count, in C, in Q, in R, in P_0, in x_0, in measurements)