Changeset 37 for tests/testKF.cpp

Show
Ignore:
Timestamp:
03/14/08 18:11:21 (16 years ago)
Author:
smidl
Message:

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

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • tests/testKF.cpp

    r33 r37  
    99 
    1010int main() { 
    11  
    12  
    1311        // Kalman filter 
    1412        mat A, B,C,D,R,Q,P0; 
     
    1816        it_file fin( "testKF.it" ); 
    1917 
    20         mat Dt, Xt,Xt2,XtE,Xtf; 
     18        mat Dt; 
    2119        int Ndat; 
    2220 
     
    4038         
    4139        Ndat = Dt.cols(); 
    42         Xt=zeros( 2,Ndat ); 
    43         Xt2=zeros( 2,Ndat ); 
    44         Xtf=zeros( 2,Ndat ); 
    45         XtE=zeros( 2,Ndat ); 
     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() ); 
    4660 
    47 //      cout << KF; 
    48         RV rx("1","{x}","2","0"); 
    49         RV ru("2","{u}","1","0"); 
    50         RV ry("3","{y}","1","0"); 
    51         // 
    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         // 
    56         Kalman<fsqmat> KFf(rx,ry,ru); 
    57         KFf.set_parameters(A,B,C,D,fsqmat(R),fsqmat(Q)); 
    58         KFf.set_est(mu0,fsqmat(P0) ); 
    59         // 
     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 
    6079        KalmanFull KF2( A,B,C,D,R,Q,P0,mu0 ); 
    61         // 
     80        mat Xt2(dimx,Ndat);      
     81        Xt2.set_col( 0,mu0); 
     82 
     83         
     84        // EKF 
    6285        bilinfn fxu(rx,ru,A,B); 
    6386        bilinfn hxu(rx,ru,C,D); 
    64         EKF<ldmat> KFE(rx,ry,ru); 
     87        EKFCh KFE(rx,ry,ru); 
    6588        KFE.set_parameters(&fxu,&hxu,Q,R); 
    66         KFE.set_est(mu0,P0); 
     89        KFE.set_est(mu0,chmat(P0)); 
     90        epdf& KFEep = KFE._epdf(); 
     91        mat XtE(dimx,Ndat);      
     92        XtE.set_col( 0,KFEep.mean() ); 
    6793 
    68         epdf& KFep = KF._epdf(); 
    69         epdf& KFfep = KFf._epdf(); 
    70         epdf& KFEep = KFE._epdf(); 
     94        //test performance of each filter 
     95        Real_Timer tt; 
     96        vec exec_times(4); // KF, KFf KF2, KFE 
    7197         
    72         Xt.set_col( 0,KFep.mean() ); 
    73         Xtf.set_col( 0,KFfep.mean() ); 
    74         Xt2.set_col( 0,KF2.mu ); 
    75         XtE.set_col( 0,KFEep.mean() ); 
     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(); 
    76106        for ( int t=1;t<Ndat;t++ ) { 
    77107                KFf.bayes( Dt.get_col( t )); 
    78                 KF.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++ ) { 
    79114                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++ ) { 
    80121                KFE.bayes( Dt.get_col( t )); 
    81                 Xt.set_col( t,KFep.mean() ); 
    82                 Xtf.set_col( t,KFfep.mean() ); 
    83                 Xt2.set_col(t,KF2.mu); 
    84122                XtE.set_col( t,KFEep.mean() ); 
    85123        } 
     124        exec_times(3) = tt.toc(); 
     125 
    86126 
    87127        it_file fou( "testKF_res.it" ); 
     
    90130        fou << Name("xth2") << Xt2; 
    91131        fou << Name("xthE") << XtE; 
     132        fou << Name("exec_times") << exec_times; 
    92133        //Exit program: 
    93134        return 0;