root/tests/testKF.cpp @ 37

Revision 37, 2.7 kB (checked in by smidl, 16 years ago)

Matrix in Cholesky decomposition, Square-root Kalman and many bug fixes

Line 
1#include <itpp/itbase.h>
2#include <estim/libKF.h>
3
4using namespace itpp;
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){ it_error("testKF.it not found");}
23        fin >>Dt;
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;
36        fin.seek( "mu0" ); fin >> Mu0; 
37        mu0=Mu0.get_col(0);
38       
39        Ndat = Dt.cols();
40        int dimx = A.rows();
41        int dimy = C.rows();
42        int dimu = B.cols();
43       
44        // Prepare for Kalman filters in BDM:
45        ivec tmpsize(1);
46        tmpsize(0) = A.cols();
47        RV rx("1","{x}",tmpsize,"0");
48        tmpsize(0) = B.cols();
49        RV ru("2","{u}",tmpsize,"0");
50        tmpsize(0) = C.rows();
51        RV ry("3","{y}",tmpsize,"0");
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._epdf();
58//      mat Xt(2,Ndat);
59//      Xt.set_col( 0,KFep.mean() );
60
61        //Chol
62        KalmanCh KF(rx,ry,ru);
63        KF.set_parameters(A,B,C,D,chmat(R),chmat(Q));
64        KF.set_est(mu0,chmat(P0) ); //prediction!
65        epdf& KFep = KF._epdf();
66        mat Xt(dimx,Ndat);     
67        Xt.set_col( 0,KFep.mean() );
68       
69        //     
70        // FSQMAT
71        Kalman<ldmat> KFf(rx,ry,ru);
72        KFf.set_parameters(A,B,C,D,ldmat(R),ldmat(Q));
73        KFf.set_est(mu0,ldmat(P0) );
74        epdf& KFfep = KFf._epdf();
75        mat Xtf(dimx,Ndat);     
76        Xtf.set_col( 0,KFfep.mean() );
77       
78        // FULL
79        KalmanFull KF2( A,B,C,D,R,Q,P0,mu0 );
80        mat Xt2(dimx,Ndat);     
81        Xt2.set_col( 0,mu0);
82
83       
84        // EKF
85        bilinfn fxu(rx,ru,A,B);
86        bilinfn hxu(rx,ru,C,D);
87        EKFCh KFE(rx,ry,ru);
88        KFE.set_parameters(&fxu,&hxu,Q,R);
89        KFE.set_est(mu0,chmat(P0));
90        epdf& KFEep = KFE._epdf();
91        mat XtE(dimx,Ndat);     
92        XtE.set_col( 0,KFEep.mean() );
93
94        //test performance of each filter
95        Real_Timer tt;
96        vec exec_times(4); // KF, KFf KF2, KFE
97       
98        tt.tic();
99        for ( int t=1;t<Ndat;t++ ) {
100                KF.bayes( Dt.get_col( t ));
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                KFf.bayes( Dt.get_col( t ));
108                Xtf.set_col( t,KFfep.mean() );
109        }
110        exec_times(1) = tt.toc();
111
112        tt.tic();
113        for ( int t=1;t<Ndat;t++ ) {
114                KF2.bayes( Dt.get_col( t ));
115                Xt2.set_col( t,KF2.mu);
116        }
117        exec_times(2) = tt.toc();
118
119        tt.tic();
120        for ( int t=1;t<Ndat;t++ ) {
121                KFE.bayes( Dt.get_col( t ));
122                XtE.set_col( t,KFEep.mean() );
123        }
124        exec_times(3) = tt.toc();
125
126
127        it_file fou( "testKF_res.it" );
128        fou << Name("xth") << Xt;
129        fou << Name("xthf") << Xtf;
130        fou << Name("xth2") << Xt2;
131        fou << Name("xthE") << XtE;
132        fou << Name("exec_times") << exec_times;
133        //Exit program:
134        return 0;
135
136}
Note: See TracBrowser for help on using the browser.