-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathkalman.m
48 lines (40 loc) · 1.47 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
% Copyright 2018 by Martin Moene
%
% https:%github.com/martinmoene/kalman-estimator
%
% Distributed under the Boost Software License, Version 1.0.
% (See accompanying file LICENSE.txt or copy at http:%www.boost.org/LICENSE_1_0.txt)
function... % [K, xhat, P] = kalman(u, z, A, B, H, Q, R, P, xhat)
[
K... % Kalman gain
, xhat... % Updated system state estimate
, P... % Updated estimate error covariance
] = kalman(
u... % Control inputs
, z... % Measurements inputs
, A... % System dynamics matrix: state-k-1 => state-k
, B... % Control input matrix: control => state
, H... % Measurement output matrix: state => measurement estimation
, Q... % Process noise covariance
, R... % Measurement noise covariance
, P... % (Initial) estimate error covariance
, xhat... % (Initial) system state estimate
)
% Update the time:
%t += dt;
% --------------------------------------
% 1. Predict (time update)
% 1a: Project the state ahead:
xhat = A * xhat + B * u;
% 1b: Project the error covariance ahead:
P = A * P * A' + Q;
% --------------------------------------
% 2. Correct (measurement update)
% 2a: Compute the Kalman gain:
K = P * H' * inv(H * P * H' + R);
% 2b: Update estimate with measurement:
xhat = xhat + K * (z - H * xhat);
% 2c: Update the error covariance:
I = eye(length(xhat));
P = (I - K * H) * P;
end % kalman