root/tests/testKF.cpp @ 271

Revision 271, 2.5 kB (checked in by smidl, 15 years ago)

Next major revision

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