Changeset 83 for bdm/estim/libKF.cpp

Show
Ignore:
Timestamp:
04/21/08 16:03:29 (16 years ago)
Author:
smidl
Message:

oprava Kalmana pro nove enorm

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • bdm/estim/libKF.cpp

    r67 r83  
    130130        vec u = dt.get ( dimy,dimy+dimu-1 ); 
    131131        vec y = dt.get ( 0,dimy-1 ); 
    132  
     132        vec pom(dimy); 
     133         
    133134        //TODO get rid of Q in qr()! 
    134135//      mat Q; 
    135136 
    136137        //R and Q are already set in set_parameters() 
    137         preA.set_submatrix ( dimy,0, ( _P->_Ch() ) *C.T() ); 
     138        preA.set_submatrix ( dimy,0, ( _P._Ch() ) *C.T() ); 
    138139        //Fixme can be more efficient if .T() is not used 
    139         preA.set_submatrix ( dimy,dimy, ( _P->_Ch() ) *A.T() ); 
     140        preA.set_submatrix ( dimy,dimy, ( _P._Ch() ) *A.T() ); 
    140141 
    141142//      if ( !qr ( preA,Q,postA ) ) it_warning ( "QR in kalman unstable!" ); 
    142143        if ( !qr ( preA,postA ) ) it_warning ( "QR in kalman unstable!" ); 
    143144 
    144         ( _Ry->_Ch() ) =postA ( 0,dimy-1, 0,dimy-1 ); 
    145         _K = ( postA ( 0,dimy-1 ,dimy,dimy+dimx-1 ) ).T(); 
    146         ( _P->_Ch() ) = postA ( dimy,dimy+dimx-1,dimy,dimy+dimx-1 ); 
    147         _Ry->inv ( *_iRy ); 
    148         fy._cached ( true ); 
    149         *_mu = A* ( *_mu ) + B*u + ( _K ) * ( _iRy->_Ch() ).T() * ( y-C* ( *_mu )-D*u ); 
    150  
    151         /*      cout << "P:" <<_P->to_mat() <<endl; 
    152                 cout << "Ry:" <<_Ry->to_mat() <<endl; 
    153                 cout << "iRy:" <<_iRy->to_mat() <<endl;*/ 
     145        ( _Ry._Ch() ) =postA ( 0,dimy-1, 0,dimy-1 ); 
     146        _K = inv(A)*( postA ( 0,dimy-1 ,dimy,dimy+dimx-1 ) ).T(); 
     147        ( _P._Ch() ) = postA ( dimy,dimy+dimx-1,dimy,dimy+dimx-1 ); 
     148         
     149        _mu = A* ( _mu ) + B*u; 
     150        _yp = C*_mu -D*u; 
     151         
     152        backward_substitution(_Ry._Ch(),( y-_yp),pom); 
     153        _mu +=  ( _K ) * pom; 
     154 
     155                cout << "P:" <<_P.to_mat() <<endl; 
     156                cout << "Ry:" <<_Ry.to_mat() <<endl; 
     157                cout << "_K:" <<_K <<endl; 
     158         
    154159        if ( evalll==true ) { //likelihood of observation y 
    155160                ll=fy.evalpdflog ( y ); 
     
    165170 
    166171        //initialize matrices A C, later, these will be only updated! 
    167         pfxu->dfdx_cond ( *_mu,zeros ( dimu ),A,true ); 
     172        pfxu->dfdx_cond ( _mu,zeros ( dimu ),A,true ); 
    168173//      pfxu->dfdu_cond ( *_mu,zeros ( dimu ),B,true ); 
    169174        B.clear(); 
    170         phxu->dfdx_cond ( *_mu,zeros ( dimu ),C,true ); 
     175        phxu->dfdx_cond ( _mu,zeros ( dimu ),C,true ); 
    171176//      phxu->dfdu_cond ( *_mu,zeros ( dimu ),D,true ); 
    172177        D.clear(); 
     
    184189void EKFCh::bayes ( const vec &dt ) { 
    185190 
    186         vec u = dt.get ( dimy,dimy+dimu-1 ); 
    187         vec y = dt.get ( 0,dimy-1 ); 
    188  
    189         pfxu->dfdx_cond ( *_mu,u,A,false ); //update A by a derivative of fx 
    190         phxu->dfdx_cond ( *_mu,u,C,false ); //update A by a derivative of fx 
     191        vec pom(dimy); 
     192        vec u = dt.get ( dimy,dimy+dimu-1 ); 
     193        vec y = dt.get ( 0,dimy-1 ); 
     194 
     195        pfxu->dfdx_cond ( _mu,u,A,false ); //update A by a derivative of fx 
     196        phxu->dfdx_cond ( _mu,u,C,false ); //update A by a derivative of fx 
    191197 
    192198        //R and Q are already set in set_parameters() 
    193         preA.set_submatrix ( dimy,0, ( _P->_Ch() ) *C.T() ); 
     199        preA.set_submatrix ( dimy,0, ( _P._Ch() ) *C.T() ); 
    194200        //Fixme can be more efficient if .T() is not used 
    195         preA.set_submatrix ( dimy,dimy, ( _P->_Ch() ) *A.T() ); 
     201        preA.set_submatrix ( dimy,dimy, ( _P._Ch() ) *A.T() ); 
    196202 
    197203//      mat Sttm = _P->to_mat(); 
     
    200206        if ( !qr ( preA,postA ) ) it_warning ( "QR in kalman unstable!" ); 
    201207 
    202         ( _Ry->_Ch() ) =postA ( 0,dimy-1, 0,dimy-1 ); 
     208        ( _Ry._Ch() ) =postA ( 0,dimy-1, 0,dimy-1 ); 
    203209        _K = inv(A)*( postA ( 0,dimy-1 ,dimy,dimy+dimx-1 ) ).T(); 
    204         ( _P->_Ch() ) = postA ( dimy,dimy+dimx-1,dimy,dimy+dimx-1 ); 
    205         _Ry->inv ( *_iRy ); 
    206         fy._cached ( true ); 
     210        ( _P._Ch() ) = postA ( dimy,dimy+dimx-1,dimy,dimy+dimx-1 ); 
    207211         
    208212//      mat iRY = inv(_Ry->to_mat()); 
     
    211215 
    212216        // prediction 
    213         *_mu = pfxu->eval ( *_mu ,u ); 
    214  
    215         *_yp = phxu->eval ( *_mu,u ); 
     217        _mu = pfxu->eval ( _mu ,u ); 
     218        _yp = phxu->eval ( _mu,u ); 
    216219         
    217220/*      vec mu2 = *_mu + ( _K2 ) * ( y-*_yp );*/ 
    218221         
    219222        //correction //= initial value is already prediction! 
    220         *_mu += ( _K ) * ( _iRy->_Ch() ).T() * ( y-*_yp ); 
     223        backward_substitution(_Ry._Ch(),( y-_yp ),pom); 
     224        _mu += ( _K ) *pom ; 
    221225 
    222226/*      _K = (_P->to_mat())*C.transpose() * ( _iRy->to_mat() ); 
    223227        *_mu = pfxu->eval ( *_mu ,u ) + ( _K )* ( y-*_yp );*/ 
    224228         
    225         /*      cout << "P:" <<_P->to_mat() <<endl; 
    226                 cout << "Ry:" <<_Ry->to_mat() <<endl; 
    227                 cout << "iRy:" <<_iRy->to_mat() <<endl;*/ 
     229                cout << "P:" <<_P.to_mat() <<endl; 
     230                cout << "Ry:" <<_Ry.to_mat() <<endl; 
     231                cout << "_K:" <<_K <<endl; 
    228232 
    229233        if ( evalll==true ) { //likelihood of observation y