-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkalman.m
63 lines (54 loc) · 1.63 KB
/
kalman.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
%function kalman(alpha, duration, dt);
alpha = 1;
duration = 20;
dt = 1;
measnoise = 10; % position measurement noise (feet)
accelnoise = 0.5; % acceleration noise (feet/sec^2)
a = [1 dt; 0 1]; % transition matrix
c = [1 0]; % measurement matrix
x = [0; 0]; % initial state vector
xhat = x; % initial state estimate
Q = accelnoise^2 * [dt^4/4 dt^3/2;
dt^3/2 dt^2]; % process noise covariance
P = Q; % initial estimation covariance
R = measnoise^2; % measurement error covariance
% set up the size of the innovations vector
Inn = zeros(size(R));
pos = []; % true position array
poshat = []; % estimated position array
posmeas = []; % measured position array
Counter = 0;
for t = 0 : dt: duration,
Counter = Counter + 1;
% Simulate the process
ProcessNoise = accelnoise * [(dt^2/2)*randn; dt*randn];
x = a * x + ProcessNoise;
% Simulate the measurement
MeasNoise = measnoise * randn;
z = c * x + MeasNoise;
% Innovation
Inn = z - c * xhat;
% Covariance of Innovation
s = c * P * c' + R;
% Gain matrix
K = a * P * c' * inv(s);
% State estimate
xhat = a * xhat + K * Inn;
% Covariance of prediction error
P = a * P * a' + Q - a * P * c' * inv(s) * c * P * a';
% Save some parameters in vectors for plotting later
pos = [pos; x(1)];
posmeas = [posmeas; z];
poshat = [poshat; xhat(1)];
end
% Plot the results
t = 0 : dt : duration;
t = t';
plot(t,pos,'r',t,poshat,'g',t,posmeas,'b');
grid;
xlabel('Time (sec)');
ylabel('Position (feet)');
title('Kalman Filter Performance');
pos
posmeas
poshat