Revision 1191, 1.3 kB
(checked in by smidl, 14 years ago)
|
OO implementation of Kalman in Matlab
|
Line | |
---|
1 | classdef mexKalman < mexBM |
---|
2 | % Approximate Bayesian |
---|
3 | properties |
---|
4 | A |
---|
5 | B |
---|
6 | C |
---|
7 | D |
---|
8 | Q |
---|
9 | R |
---|
10 | predX; % predictive density on x |
---|
11 | predY; % predictive density on y |
---|
12 | end |
---|
13 | methods |
---|
14 | function obj=validate(obj) |
---|
15 | % TODO: check sizes of A,B,C,D,Q,R |
---|
16 | obj.apost_pdf = mexGauss; |
---|
17 | obj.predX = mexGauss; |
---|
18 | obj.predY = mexGauss; |
---|
19 | |
---|
20 | obj.log_evidence = 0; % evidence is not computed! |
---|
21 | end |
---|
22 | |
---|
23 | function dims=dimensions(obj) |
---|
24 | %please fill: dims = [size_of_posterior size_of_data size_of_condition] |
---|
25 | dims = [size(A,1),size(C,1),0] |
---|
26 | end |
---|
27 | function obj=bayes(obj,yt,ut) % approximate bayes rule |
---|
28 | % transform old estimate into new estimate |
---|
29 | |
---|
30 | %% <- in python you can use assignment: |
---|
31 | %% P=obj.apost_pdf.R ! |
---|
32 | |
---|
33 | obj.predX.mu = obj.A*obj.apost_pdf.mu + obj.B*ut; |
---|
34 | obj.predX.R = obj.A*obj.apost_pdf.R*obj.A' + obj.Q; |
---|
35 | |
---|
36 | %Data update |
---|
37 | obj.predY.R = obj.C*obj.predX.R*obj.C' + obj.R; |
---|
38 | iRy = inv(obj.predY.R); |
---|
39 | K = obj.predX.R*obj.C'*iRy; |
---|
40 | obj.apost_pdf.R = obj.predX.R- K*obj.C*obj.predX.R; % P = P -KCP; |
---|
41 | obj.predY.mu = obj.C*obj.predX.mu + obj.D*ut; |
---|
42 | obj.apost_pdf.mu = obj.predX.mu + K*(yt-obj.predY.mu); |
---|
43 | end |
---|
44 | end |
---|
45 | |
---|
46 | end |
---|