-
Notifications
You must be signed in to change notification settings - Fork 0
/
kf_update.m
102 lines (97 loc) · 2.5 KB
/
kf_update.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
%KF_UPDATE Kalman Filter update step
%
% Syntax:
% [X,P,K,IM,IS,LH] = KF_UPDATE(X,P,Y,H,R)
%
% In:
% X - Nx1 mean state estimate after prediction step
% P - NxN state covariance after prediction step
% Y - Dx1 measurement vector.
% H - Measurement matrix.
% R - Measurement noise covariance.
%
% Out:
% X - Updated state mean
% P - Updated state covariance
% K - Computed Kalman gain
% IM - Mean of predictive distribution of Y
% IS - Covariance or predictive mean of Y
% LH - Predictive probability (likelihood) of measurement.
%
% Description:
% Kalman filter measurement update step. Kalman Filter
% model is
%
% x[k] = A*x[k-1] + B*u[k-1] + q, q ~ N(0,Q)
% y[k] = H*x[k] + r, r ~ N(0,R)
%
% Prediction step of Kalman filter computes predicted
% mean m-[k] and covariance P-[k] of state:
%
% p(x[k] | y[1:k-1]) = N(x[k] | m-[k], P-[k])
%
% See for instance KF_PREDICT how m-[k] and P-[k] are
% calculated.
%
% Update step computes the posterior mean m[k] and
% covariance P[k] of state given new measurement:
%
% p(x[k] | y[1:k]) = N(x[k] | m[k], P[k])
%
% Innovation distribution is defined as
%
% p(y[k] | y[1:k-1]) = N(y[k] | IM[k], IS[k])
%
% Updated mean x[k] and covarience P[k] are given by
% the following equations (not the only possible ones):
%
% v[k] = y[k] - H[k]*m-[k]
% S[k] = H[k]*P-[k]*H[k]' + R[k]
% K[k] = P-[k]*H[k]'*[S[k]]^(-1)
% m[k] = m-[k] + K[k]*v[k]
% P[k] = P-[k] - K[k]*S[k]*K[k]'
%
% Example:
% m = m0;
% P = P0;
% M = m0;
% for i=1:size(Y,2)
% [m,P] = kf_predict(m,P,A,Q);
% [m,P] = kf_update(m,P,Y(:,i),H,R);
% M = [M m];
% end
%
% See also:
% KF_PREDICT, EKF_UPDATE
% History:
% 26.02.2007 JH Added the equations for calculating the updated
% means and covariances to the description section.
% 12.01.2003 SS Symmetrized covariance update
% 20.11.2002 SS The first official version.
%
% Copyright (C) 2002, 2003 Simo Särkkä
% Copyright (C) 2007 Jouni Hartikainen
%
% $Id$
%
% This software is distributed under the GNU General Public
% Licence (version 2 or later); please refer to the file
% Licence.txt, included with the software, for details.
function [X,P,K,IM,IS,LH] = kf_update(X,P,y,H,R)
%
% Check which arguments are there
%
if nargin < 5
error('Too few arguments');
end
%
% update step
%
IM = H*X;
IS = (R + H*P*H');
K = P*H'/IS;
X = X + K * (y-IM);
P = P - K*IS*K';
if nargout > 5
LH = gauss_pdf(y,IM,IS);
end