KalmanFilter
ukf.m
Go to the documentation of this file.
1 function [estimates, covariances] = ukf(f_func, state_count, sensor_count, measurement_count,C,Q,R,P_0,x_0, measurements)
2 %Unscented Kalman filter implementation.
3 %[estimates, covariances] = ukf(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 assert(state_count == size(x_0,1));
39 for i=1:measurement_count
40  %assert(size(C(x_0,i),1) == sensor_count & size(C(x_0,i),2) == state_count);
41  assert(size(Q(i),1) == state_count & size(Q(i),2) == state_count);
42  assert(size(R(i),1) == sensor_count & size(R(i),2) == sensor_count);
43 end
44 assert(size(P_0,1) == state_count & size(P_0,2) == state_count);
45 assert(size(x_0,1) == state_count & size(x_0,2) == 1);
46 assert(size(measurements,1) == sensor_count & size(measurements,2) == measurement_count);
47 assert(size(f_func(zeros(state_count,1)),1) == state_count & size(f_func(zeros(state_count,1)),2) == 1);
48 
49 
50 P_update = P_0;
51 x_update = x_0;
52 covariances = cell(1,measurement_count+1);
53 estimates = zeros(state_count,measurement_count+1);
54 covariances{1,1} = P_0;
55 estimates(:,1) = x_0;
56 
57 sigma_points = zeros(state_count,state_count*2+1);
58 propagated = zeros(state_count,state_count*2,+1);
59 measurement_points = zeros(sensor_count,state_count*2+1);
60 cross_covariance = zeros(state_count,sensor_count);
61 Z_cov = zeros(sensor_count,sensor_count);
62 P_cov = zeros(state_count,state_count);
63 
64 alpha = 1;
65 beta = 2;
66 KK = 0;
67 lambda = alpha^2*(state_count+KK) - state_count;
68 
69 n_0_m = (lambda/(state_count + lambda));
70 n_i_m = 0.5*(1/(state_count + lambda));
71 n_0_c = (lambda/(state_count+lambda) + 1 - alpha^2 + beta);
72 n_i_c = n_i_m;
73 
74 for ii=1:measurement_count
75  %predict phase
76  sigma_points(:,1) = x_update;
77  propagated(:,1) = f_func(sigma_points(:,1));
78  measurement_points(:,1) = C(sigma_points(:,1),ii);
79 
80  x_predict = n_0_m.*(propagated(:,1));
81  z_predict = n_0_m.*measurement_points(:,1);
82  P_sqrt = chol(P_update)';
83 
84  for i=1:state_count
85  sigma_points(:,i+1) = x_update + sqrt(state_count + lambda).*P_sqrt(:,i);
86  sigma_points(:,i+1+state_count) = x_update - sqrt(state_count + lambda).*P_sqrt(:,i);
87 
88  propagated(:,i+1) = f_func(sigma_points(:,i+1));
89  propagated(:,i+1+state_count) = f_func(sigma_points(:,i+1+state_count));
90  measurement_points(:,i+1) = C(sigma_points(:,i+1),ii);
91  measurement_points(:,i+1+state_count) = C(sigma_points(:,i+1+state_count),ii);
92 
93  x_predict = x_predict + n_i_m.*(propagated(:,i+1) + propagated(:,i+state_count+1));
94  z_predict = z_predict + n_i_m.*(measurement_points(:,i+1) + measurement_points(:,i+1+state_count));
95  end
96 
97  Z_cov = n_0_c.*(z_predict - measurement_points(:,1))*(z_predict - measurement_points(:,1))';
98  P_cov = n_0_c.*(x_predict - propagated(:,1))*(x_predict-propagated(:,1))';
99  cross_covariance = n_0_c.*(x_predict - propagated(:,1))*(z_predict - measurement_points(:,1))';
100  for i=2:(state_count*2+1)
101  Z_cov = Z_cov + n_i_c.*(z_predict - measurement_points(:,i))*(z_predict - measurement_points(:,i))';
102  P_cov = P_cov + n_i_c.*(x_predict - propagated(:,i))*(x_predict-propagated(:,i))';
103  cross_covariance = cross_covariance + n_i_c.*(x_predict - propagated(:,i))*(z_predict - measurement_points(:,i))';
104  end
105  P_cov = P_cov + Q(ii);
106  P_cov = 0.5.*(P_cov + P_cov');%keeping matrix symmetric
107  Z_cov = Z_cov + R(ii);
108  Z_cov = 0.5*(Z_cov + Z_cov');
109 
110  %update phase
111  Kalman_gain = cross_covariance*inv(Z_cov);
112  x_update = x_predict + Kalman_gain*(measurements(:,ii) - z_predict);
113 
114  P_update = P_cov - Kalman_gain*Z_cov*(Kalman_gain');
115  covariances{1,ii+1} = P_update;
116  estimates(:,ii+1) = x_update;
117 end
function ukf(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)