comparison toolboxes/FullBNT-1.0.7/Kalman/kalman_forward_backward.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 % KALMAN_FORWARD_BACKWARD Forward Backward Propogation in Information Form
2 %
3 %
4 % Note :
5 %
6 % M file accompanying my technical note
7 %
8 % A Technique for Painless Derivation of Kalman Filtering Recursions
9 %
10 % available from http://www.mbfys.kun.nl/~cemgil/papers/painless-kalman.ps
11 %
12
13 % Uses :
14
15 % Change History :
16 % Date Time Prog Note
17 % 07-Jun-2001 2:24 PM ATC Created under MATLAB 5.3.1.29215a (R11.1)
18
19 % ATC = Ali Taylan Cemgil,
20 % SNN - University of Nijmegen, Department of Medical Physics and Biophysics
21 % e-mail : cemgil@mbfys.kun.nl
22
23 A = [1 1;0 1];
24 C = [1 0];
25 Q = eye(2)*0.01^2;
26 R = 0.001^2;
27 mu1 = [0;1];
28 P1 = 3*Q;
29
30 inv_Q = inv(Q);
31 inv_R = inv(R);
32
33 y = [0 1.1 2 2.95 3.78];
34
35 T = length(y);
36 L = size(Q,1);
37
38 %%%%% Forward message Passing
39 h_f = zeros(L, T);
40 K_f = zeros(L, L, T);
41 g_f = zeros(1, T);
42 h_f_pre = zeros(L, T);
43 K_f_pre = zeros(L, L, T);
44 g_f_pre = zeros(1, T);
45
46
47 K_f_pre(:, :, 1) = inv(P1);
48 h_f_pre(:,1) = K_f_pre(:, :, 1)*mu1;
49 g_f_pre(1) = -0.5*log(det(2*pi*P1)) - 0.5*mu1'*inv(P1)*mu1;
50
51 for i=1:T,
52 h_f(:,i) = h_f_pre(:,i) + C'*inv_R*y(:,i);
53 K_f(:,:,i) = K_f_pre(:,:,i) + C'*inv_R*C;
54 g_f(i) = g_f_pre(i) -0.5*log(det(2*pi*R)) - 0.5*y(:,i)'*inv_R*y(:,i);
55 if i<T,
56 M = inv(A'*inv_Q*A + K_f(:,:,i));
57 h_f_pre(:,i+1) = inv_Q*A*M*h_f(:,i);
58 K_f_pre(:,:,i+1) = inv_Q - inv_Q*A*M*A'*inv_Q;
59 g_f_pre(i+1) = g_f(i) -0.5*log(det(2*pi*Q)) + 0.5*log(det(2*pi*M)) + 0.5*h_f(:,i)'*M*h_f(:,i);
60 end;
61 end
62
63 %%% Backward Message Passing
64 h_b = zeros(L, T);
65 K_b = zeros(L, L, T);
66 g_b = zeros(1, T);
67
68 h_b_post = zeros(L, T);
69 K_b_post = zeros(L, L, T);
70 g_b_post = zeros(1, T);
71
72 for i=T:-1:1,
73 h_b(:,i) = h_b_post(:,i) + C'*inv_R*y(:,i);
74 K_b(:,:,i) = K_b_post(:,:,i) + C'*inv_R*C;
75 g_b(i) = g_b_post(i) - 0.5*log(det(2*pi*R)) - 0.5*y(:,i)'*inv_R*y(:,i);
76 if i>1,
77 M = inv(inv_Q + K_b(:,:,i));
78 h_b_post(:,i-1) = A'*inv(Q)*M*h_b(:,i);
79 K_b_post(:,:,i-1) = A'*inv_Q*(Q - M)*inv_Q*A;
80 g_b_post(i-1) = g_b(i) -0.5*log(det(2*pi*Q)) + 0.5*log(det(2*pi*M)) + 0.5*h_b(:,i)'*M*h_b(:,i);
81 end;
82 end;
83
84
85 %%%% Smoothed Estimates
86
87 mu = zeros(size(h_f));
88 Sig = zeros(size(K_f));
89 g = zeros(size(g_f));
90 lalpha = zeros(size(g_f));
91
92 for i=1:T,
93 Sig(:,:,i) = inv(K_b_post(:,:,i) + K_f(:,:,i));
94 mu(:,i) = Sig(:,:,i)*(h_b_post(:,i) + h_f(:,i));
95 g(i) = g_b_post(i) + g_f(:,i);
96 lalpha(i) = g(i) + 0.5*log(det(2*pi*Sig(:,:,i))) + 0.5*mu(:,i)'*inv(Sig(:,:,i))*mu(:,i);
97 end;