root/library/tests/test_kalman.cpp @ 681

Revision 681, 2.8 kB (checked in by smidl, 15 years ago)

corrections of Kalman

  • Property svn:eol-style set to native
Line 
1
2#include <estim/kalman.h>
3
4using namespace bdm;
5
6//These lines are needed for use of cout and endl
7using std::cout;
8using std::endl;
9
10int main() {
11        // Kalman filter
12        mat A, B, C, D, R, Q, P0;
13        vec mu0;
14        mat Mu0;;
15        // input from Matlab
16        it_file fin ( "testKF.it" );
17
18        mat Dt;
19        int Ndat;
20
21        bool xxx = fin.seek ( "d" );
22        if ( !xxx ) {
23                bdm_error ( "testKF.it not found" );
24        }
25        fin >> Dt;
26        fin.seek ( "A" );
27        fin >> A;
28        fin.seek ( "B" );
29        fin >> B;
30        fin.seek ( "C" );
31        fin >> C;
32        fin.seek ( "D" );
33        fin >> D;
34        fin.seek ( "R" );
35        fin >> R;
36        fin.seek ( "Q" );
37        fin >> Q;
38        fin.seek ( "P0" );
39        fin >> P0;
40        fin.seek ( "mu0" );
41        fin >> Mu0;
42        mu0 = Mu0.get_col ( 0 );
43
44        Ndat = Dt.cols();
45        int dimx = A.rows();
46
47        // Prepare for Kalman filters in BDM:
48        RV rx ( "{x }", vec_1 ( A.cols() ) );
49        RV ru ( "{u }", vec_1 ( B.cols() ) );
50        RV ry ( "{y }", vec_1 ( C.rows() ) );
51
52//      // LDMAT
53//      Kalman<ldmat> KF(rx,ry,ru);
54//      KF.set_parameters(A,B,C,D,ldmat(R),ldmat(Q));
55//      KF.set_est(mu0,ldmat(P0) );
56//      epdf& KFep = KF.posterior();
57//      mat Xt(2,Ndat);
58//      Xt.set_col( 0,KFep.mean() );
59
60        //Chol
61        KalmanCh KF;
62        KF.set_parameters ( A, B, C, D, chmat ( Q ), chmat ( R ) );
63        KF.set_statistics ( mu0, chmat ( P0 ) ); //prediction!
64        KF.set_evalll(false);
65        KF.validate();
66        const epdf& KFep = KF.posterior();
67        mat Xt ( dimx, Ndat );
68        Xt.set_col ( 0, KFep.mean() );
69
70        // FULL
71        KalmanFull KF2;
72        KF2.set_parameters( A, B, C, D,  Q, R);
73        KF2.set_statistics(  mu0, P0 );
74        KF2.set_evalll(false);
75        KF2.validate();
76        mat Xt2 ( dimx, Ndat );
77        Xt2.set_col ( 0, mu0 );
78
79
80        // EKF
81        shared_ptr<bilinfn> fxu = new bilinfn ( A, B );
82        shared_ptr<bilinfn> hxu = new bilinfn ( C, D );
83        EKFCh KFE;
84        KFE.set_parameters ( fxu, hxu, Q, R );
85        KFE.set_statistics ( mu0, chmat ( P0 ) );
86        KFE.set_evalll(false);
87        KFE.validate();
88        const epdf& KFEep = KFE.posterior();
89        mat XtE ( dimx, Ndat );
90        XtE.set_col ( 0, KFEep.mean() );
91
92        //test performance of each filter
93        Real_Timer tt;
94        vec exec_times ( 3 ); // KF, KF2, KFE
95
96        vec dt;
97        tt.tic();
98        for ( int t = 1; t < Ndat; t++ ) {
99                dt = Dt.get_col(t);
100                KF.bayes ( dt.get(0,C.rows()-1), dt.get(C.rows(), dt.length()-1) );
101                Xt.set_col ( t, KFep.mean() );
102        }
103        exec_times ( 0 ) = tt.toc();
104
105        tt.tic();
106        for ( int t = 1; t < Ndat; t++ ) {
107                dt = Dt.get_col(t);
108                KF2.bayes ( dt.get(0,C.rows()-1), dt.get(C.rows(), dt.length()-1) );
109                Xt2.set_col ( t, KF2.posterior().mean() );
110        }
111        exec_times ( 1 ) = tt.toc();
112
113        tt.tic();
114        for ( int t = 1; t < Ndat; t++ ) {
115                dt = Dt.get_col(t);
116                KFE.bayes ( dt.get(0,C.rows()-1), dt.get(C.rows(), dt.length()-1) );
117                XtE.set_col ( t, KFEep.mean() );
118        }
119        exec_times ( 2 ) = tt.toc();
120
121
122        it_file fou ( "testKF_res.it" );
123        fou << Name ( "xth" ) << Xt;
124        fou << Name ( "xth2" ) << Xt2;
125        fou << Name ( "xthE" ) << XtE;
126        fou << Name ( "exec_times" ) << exec_times;
127        //Exit program:
128        return 0;
129
130}
Note: See TracBrowser for help on using the browser.