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