root/library/tests/test_kalman.cpp @ 565

Revision 565, 2.8 kB (checked in by vbarta, 15 years ago)

using own error macros (basically copied from IT++, but never aborting)

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