-
Notifications
You must be signed in to change notification settings - Fork 40
/
kf_loop.m
71 lines (65 loc) · 1.84 KB
/
kf_loop.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
%KF_LOOP Performs the prediction and update steps of the Kalman filter
% for a set of measurements.
%
% Syntax:
% [MM,PP] = KF_LOOP(X,P,H,R,Y,A,Q)
%
% In:
% X - Nx1 initial estimate for the state mean
% P - NxN initial estimate for the state covariance
% H - DxN measurement matrix
% R - DxD measurement noise covariance
% Y - DxM matrix containing all the measurements.
% A - Transition matrix of the discrete model (optional, default identity)
% Q - Process noise of the discrete model (optional, default zero)
%
% Out:
% MM - Filtered state mean sequence
% PP - Filtered state covariance sequence
%
% Description:
% Calculates state estimates for a set measurements using the
% Kalman filter. This function is for convience, as it basically consists
% only of a space reservation for the estimates and of a for-loop which
% calls the predict and update steps of the KF for each time step in
% the measurements.
%
% See also:
% KF_PREDICT, KF_UPDATE
% History:
%
% 12.2.2007 JH Initial version.
%
% Copyright (C) 2007 Jouni Hartikainen
%
% 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 [MM,PP] = kf_loop(X,P,H,R,Y,A,Q)
% Check the input parameters.
if nargin < 5
error('Too few arguments');
end
if nargin < 6
A = [];
end
if nargin < 7
Q = [];
end
% Apply the defaults
if isempty(A)
A = eye(size(X,1));
end
if isempty(Q)
Q = zeros(size(X,1));
end
% Space for the estimates.
MM = zeros(size(X,1), size(Y,2));
PP = zeros(size(X,1), size(X,1), size(Y,2));
% Filtering steps.
for i = 1:size(Y,2)
[X,P] = kf_predict(X,P,A,Q);
[X,P] = kf_update(X,P,Y(:,i),H,R);
MM(:,i) = X;
PP(:,:,i) = P;
end