KalmanFilter
linear.m
Go to the documentation of this file.
1 %LINEAR EXAMPLE
2 clear all;
3 addpath('..')
4 C = [1, 0.1]; %observation matrix to obtain measurement y (y = C*x + noise)
5 A = [-1,0.2;-0.1,-1]; %(d/dt)x(t) = A*x(t)
6 R_c = [0.1^2]; %continuous sensor noise
7 Q_c = [0.001^2,0;0,0.001^2]; %continuous process noise
8 P_0 = eye(2);%initial covariance
9 state_count = 2;
10 
11 t_start = 0;
12 t_final = 20;
13 outputs = 10000;
14 dt = (t_final-t_start)/outputs;
15 
16 A_d = eye(2) + dt.*A;%x_{k+1} = A_d*x_k
17 Q_d = dt.*Q_c;
18 R_d = (1/dt).*R_c;
19 
20 func = @(x) A_d*x;
21 isOctave = exist('OCTAVE_VERSION', 'builtin') ~= 0;
22 if isOctave==true
23  jacobian_func = @(x) A_d + 0.*x;
24 else
25  xx = sym('x',[state_count,1]);
26  jacobian_func = matlabFunction(jacobian(func(xx),xx),'Vars',xx);
27 end
28 func = @(x,t) func(x) + 0.*t;
29 jacobian_func = @(x,t) jacobian_func(x) + 0.*t;
30 
31 x_0 = [100; 80]; %initial real data state
32 state_count = length(x_0);
33 sensor_count = 1;
34 ideal_data = zeros(state_count,outputs); %data with no process noise
35 process_noise_data = zeros(state_count,outputs);
36 measurements = zeros(sensor_count,outputs);
37 x = x_0;
38 x_noise = x_0;
39 
40 if isOctave==true
41  randn("seed",1);
42 else
43  rng(1);
44 end
45 
46 for ii=1:outputs
47  x = x + dt.*(A*x);%Explicit Eulers
48  ideal_data(:,ii) = x;%the first column of ideal_data and process_noise_data is x_1
49  x_noise = x_noise + dt.*(A*x_noise) + sqrt(Q_d)*randn(state_count,1);%Euler-Maruyama
50  process_noise_data(:,ii) = x_noise;
51  measurements(ii) = C*x_noise + sqrt(R_d)*randn(sensor_count,1);
52 end
53 
54 [estimates, covariances ] = ddekf(func,jacobian_func,dt,t_start,state_count,...
55  sensor_count,outputs,C,chol(Q_d)',chol(R_d)',chol(P_0)',x_0, measurements);
56 
57 
58 covariances{end}
59 
function ddekf(in f_func, in jacobian_func, in dt_between_measurements, in start_time, in state_count, in sensor_count, in measurement_count, in C, in Q_root, in R_root, in P_0_root, in x_0, in measurements)