comparison toolboxes/FullBNT-1.0.7/Kalman/kalman_update.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function [xnew, Vnew, loglik, VVnew] = kalman_update(A, C, Q, R, y, x, V, varargin)
2 % KALMAN_UPDATE Do a one step update of the Kalman filter
3 % [xnew, Vnew, loglik] = kalman_update(A, C, Q, R, y, x, V, ...)
4 %
5 % INPUTS:
6 % A - the system matrix
7 % C - the observation matrix
8 % Q - the system covariance
9 % R - the observation covariance
10 % y(:) - the observation at time t
11 % x(:) - E[X | y(:, 1:t-1)] prior mean
12 % V(:,:) - Cov[X | y(:, 1:t-1)] prior covariance
13 %
14 % OPTIONAL INPUTS (string/value pairs [default in brackets])
15 % 'initial' - 1 means x and V are taken as initial conditions (so A and Q are ignored) [0]
16 % 'u' - u(:) the control signal at time t [ [] ]
17 % 'B' - the input regression matrix
18 %
19 % OUTPUTS (where X is the hidden state being estimated)
20 % xnew(:) = E[ X | y(:, 1:t) ]
21 % Vnew(:,:) = Var[ X(t) | y(:, 1:t) ]
22 % VVnew(:,:) = Cov[ X(t), X(t-1) | y(:, 1:t) ]
23 % loglik = log P(y(:,t) | y(:,1:t-1)) log-likelihood of innovatio
24
25 % set default params
26 u = [];
27 B = [];
28 initial = 0;
29
30 args = varargin;
31 for i=1:2:length(args)
32 switch args{i}
33 case 'u', u = args{i+1};
34 case 'B', B = args{i+1};
35 case 'initial', initial = args{i+1};
36 otherwise, error(['unrecognized argument ' args{i}])
37 end
38 end
39
40 % xpred(:) = E[X_t+1 | y(:, 1:t)]
41 % Vpred(:,:) = Cov[X_t+1 | y(:, 1:t)]
42
43 if initial
44 if isempty(u)
45 xpred = x;
46 else
47 xpred = x + B*u;
48 end
49 Vpred = V;
50 else
51 if isempty(u)
52 xpred = A*x;
53 else
54 xpred = A*x + B*u;
55 end
56 Vpred = A*V*A' + Q;
57 end
58
59 e = y - C*xpred; % error (innovation)
60 n = length(e);
61 ss = length(A);
62 S = C*Vpred*C' + R;
63 Sinv = inv(S);
64 ss = length(V);
65 loglik = gaussian_prob(e, zeros(1,length(e)), S, 1);
66 K = Vpred*C'*Sinv; % Kalman gain matrix
67 % If there is no observation vector, set K = zeros(ss).
68 xnew = xpred + K*e;
69 Vnew = (eye(ss) - K*C)*Vpred;
70 VVnew = (eye(ss) - K*C)*A*V;
71