root/tests/testKF.cpp @ 100

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

test arx

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 = 10;//Dt.cols();
40        int dimx = A.rows();
41       
42        // Prepare for Kalman filters in BDM:
43        ivec tmpsize(1);
44        tmpsize(0) = A.cols();
45        RV rx("1","{x }",tmpsize,"0");
46        tmpsize(0) = B.cols();
47        RV ru("2","{u }",tmpsize,"0");
48        tmpsize(0) = C.rows();
49        RV ry("3","{y }",tmpsize,"0");
50       
51//      // LDMAT
52//      Kalman<ldmat> KF(rx,ry,ru);
53//      KF.set_parameters(A,B,C,D,ldmat(R),ldmat(Q));
54//      KF.set_est(mu0,ldmat(P0) );
55//      epdf& KFep = KF._epdf();
56//      mat Xt(2,Ndat);
57//      Xt.set_col( 0,KFep.mean() );
58
59        //Chol
60        KalmanCh KF(rx,ry,ru);
61        KF.set_parameters(A,B,C,D,chmat(R),chmat(Q));
62        KF.set_est(mu0,chmat(P0) ); //prediction!
63        epdf& KFep = KF._epdf();
64        mat Xt(dimx,Ndat);     
65        Xt.set_col( 0,KFep.mean() );
66       
67        //     
68        // FSQMAT
69        Kalman<ldmat> KFf(rx,ry,ru);
70        KFf.set_parameters(A,B,C,D,ldmat(R),ldmat(Q));
71        KFf.set_est(mu0,ldmat(P0) );
72        epdf& KFfep = KFf._epdf();
73        mat Xtf(dimx,Ndat);     
74        Xtf.set_col( 0,KFfep.mean() );
75       
76        // FULL
77        KalmanFull KF2( A,B,C,D,R,Q,P0,mu0 );
78        mat Xt2(dimx,Ndat);     
79        Xt2.set_col( 0,mu0);
80
81       
82        // EKF
83        bilinfn fxu(rx,ru,A,B);
84        bilinfn hxu(rx,ru,C,D);
85        EKFCh KFE(rx,ry,ru);
86        KFE.set_parameters(&fxu,&hxu,Q,R);
87        KFE.set_est(mu0,chmat(P0));
88        epdf& KFEep = KFE._epdf();
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(4); // KF, KFf KF2, KFE
95       
96        tt.tic();
97        for ( int t=1;t<Ndat;t++ ) {
98                KF.bayes( Dt.get_col( t ));
99                Xt.set_col( t,KFep.mean() );
100        }
101        exec_times(0) = tt.toc();
102       
103        tt.tic();
104        for ( int t=1;t<Ndat;t++ ) {
105                KFf.bayes( Dt.get_col( t ));
106                Xtf.set_col( t,KFfep.mean() );
107        }
108        exec_times(1) = tt.toc();
109
110        tt.tic();
111        for ( int t=1;t<Ndat;t++ ) {
112                KF2.bayes( Dt.get_col( t ));
113                Xt2.set_col( t,KF2.mu);
114        }
115        exec_times(2) = tt.toc();
116
117        tt.tic();
118        for ( int t=1;t<Ndat;t++ ) {
119                KFE.bayes( Dt.get_col( t ));
120                XtE.set_col( t,KFEep.mean() );
121        }
122        exec_times(3) = tt.toc();
123
124
125        it_file fou( "testKF_res.it" );
126        fou << Name("xth") << Xt;
127        fou << Name("xthf") << Xtf;
128        fou << Name("xth2") << Xt2;
129        fou << Name("xthE") << XtE;
130        fou << Name("exec_times") << exec_times;
131        //Exit program:
132        return 0;
133
134}
Note: See TracBrowser for help on using the browser.