root/library/tests/stresssuite/kalman_stress.cpp @ 721

Revision 721, 2.9 kB (checked in by mido, 15 years ago)

stresssuite - halfway point

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