KalmanFilter
cubature.m
Go to the documentation of this file.
1 function [estimates, covariances ] = cubature(f_func,dt_between_measurements,start_time,state_count,sensor_count,measurement_count,C_func,Q_root,R_root,P_0_root,x_0, measurements)
2 %Runs Cubature Kalman filter on data. The initial
3 %estimate and covariances are at the time step before all the
4 %measurements - be wary of the off-by-one error. If f_func is a
5 %linear function the code is equivalent to discrete-discrete Kalman
6 %filter.
7 %[estimates, covariances] = cubature(f_func,jacobian_func,state_count,sensor_count,measurement_count,C,Q_root,R_root,P_0_root,x_0, measurements)
8 %INPUT:
9 % f_func: x_{k+1} = f_func(x_k,t) where x_k is the state. The
10 % function's second argument is time t for cases when the function
11 % changes with time. The argument can be also used an internal
12 % counter variable for f_func when start_time is set to zero and
13 % dt_between_measurements is set to 1.
14 %
15 % dt_between_measurements: time distance between incoming
16 % measurements. Used for incrementing time counter for each
17 % successive measurement with the time counter initialized with
18 % start_time. The time counter is fed into f_func(x,t) as t.
19 %
20 % start_time: the time of first measurement
21 %
22 % state_count: dimension of the state
23 %
24 % sensor_count: dimension of observation vector
25 %
26 % C_func: observation matrix function that converts single argument vector
27 % of dimension of 'state_count' by 1 to system measurement of size 'sensor_count' by 1
28 %
29 % R_root: The root of sensor error covariance matrix R where
30 % R = R_root*(R_root'). R_root is of size 'sensor_count by
31 % sensor_count'. R_root = chol(R)' is one way to derive it.
32 %
33 % Q_root: The root of process error covariance matrix Q where
34 % Q = Q_root*(Q_root'). Q_root is of size 'state_count by
35 % state_count'. Q_root = chol(Q)' is one way to derive it.
36 %
37 % P_0_root: The root of initial covariance matrix P_0 where
38 % P_0 = P_0_root*(P_root'); P_0_root is of size 'state_count by
39 % state_count'. % P_0_root = chol(P_0)' is one way to derive it.
40 %
41 % x_0:Initial state estimate of size 'state_count by 1'
42 %
43 % measurements: ith column is ith measurement. Matrix of size
44 % 'sensor_count by measurement_count'
45 %
46 %OUTPUT:
47 % estimates: 'state_count by measurement_count+1'
48 % ith column is ith estimate. first column is x_0
49 %
50 % covariances: cell of size 'measurement_count+1' by 1
51 % where each entry is the P covariance matrix at that time
52 % Time is computed based on dt_between_measurements
53 
54  %make sure input is valid
55  assert(size(P_0_root,1)==state_count &&...
56  size(P_0_root,2)==state_count);
57  %assert(size(C,1)==sensor_count && size(C,2)==state_count);
58  assert(size(Q_root,1)==state_count &&...
59  size(Q_root,2)==state_count);
60  assert(size(x_0,1)==state_count && size(x_0,2)==1);
61  assert(size(R_root,1)==sensor_count &&...
62  size(R_root,2)==sensor_count);
63  test = f_func(x_0,start_time);
64  assert(size(test,1)==state_count && size(test,2)==1);
65  %test = jacobian_func(x_0,start_time);
66  %assert(size(test,1)==state_count && size(test,2)==state_count);
67  assert(size(measurements,1)==sensor_count &&...
68  size(measurements,2)==measurement_count);
69 
70  x_km1_p = x_0;
71  P_root_km1_p = P_0_root;
72 
73  estimates = zeros(state_count,measurement_count + 1);
74  covariances = cell(measurement_count + 1, 1);
75 
76  estimates(1:state_count,1) = x_0;
77  covariances{1,1} = P_0_root*P_0_root';
78 
79  current_time = start_time;
80 
81  for k=1:measurement_count
82  %loop through all measurements and follow through predict
83  %and update phase
84  [x_k_m,P_root_km] = cubature_predict_phase(f_func,...
85  current_time,P_root_km1_p,x_km1_p,...
86  Q_root);
87  [x_k_p,P_root_kp] = cubature_update_phase(R_root,...
88  P_root_km,C_func,x_k_m,measurements(:,k));
89 
90  x_km1_p = x_k_p;
91  P_root_km1_p = P_root_kp;
92 
93  %store results
94  estimates(:,k+1) = x_k_p;
95  covariances{k+1,1} = P_root_kp*(P_root_kp');
96 
97  current_time = current_time + dt_between_measurements;
98  end
99 end
function cubature(in f_func, in dt_between_measurements, in start_time, in state_count, in sensor_count, in measurement_count, in C_func, in Q_root, in R_root, in P_0_root, in x_0, in measurements)
function cubature_predict_phase(in f_func, in t, in P_0_sqrt, in x_0, in Q_root)
function cubature_update_phase(in R_root, in P_root, in C_func, in estimate, in measurement)