Changeset 83 for bdm/estim/libKF.cpp
- Timestamp:
- 04/21/08 16:03:29 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/estim/libKF.cpp
r67 r83 130 130 vec u = dt.get ( dimy,dimy+dimu-1 ); 131 131 vec y = dt.get ( 0,dimy-1 ); 132 132 vec pom(dimy); 133 133 134 //TODO get rid of Q in qr()! 134 135 // mat Q; 135 136 136 137 //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() ); 138 139 //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() ); 140 141 141 142 // if ( !qr ( preA,Q,postA ) ) it_warning ( "QR in kalman unstable!" ); 142 143 if ( !qr ( preA,postA ) ) it_warning ( "QR in kalman unstable!" ); 143 144 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 154 159 if ( evalll==true ) { //likelihood of observation y 155 160 ll=fy.evalpdflog ( y ); … … 165 170 166 171 //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 ); 168 173 // pfxu->dfdu_cond ( *_mu,zeros ( dimu ),B,true ); 169 174 B.clear(); 170 phxu->dfdx_cond ( *_mu,zeros ( dimu ),C,true );175 phxu->dfdx_cond ( _mu,zeros ( dimu ),C,true ); 171 176 // phxu->dfdu_cond ( *_mu,zeros ( dimu ),D,true ); 172 177 D.clear(); … … 184 189 void EKFCh::bayes ( const vec &dt ) { 185 190 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 191 197 192 198 //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() ); 194 200 //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() ); 196 202 197 203 // mat Sttm = _P->to_mat(); … … 200 206 if ( !qr ( preA,postA ) ) it_warning ( "QR in kalman unstable!" ); 201 207 202 ( _Ry ->_Ch() ) =postA ( 0,dimy-1, 0,dimy-1 );208 ( _Ry._Ch() ) =postA ( 0,dimy-1, 0,dimy-1 ); 203 209 _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 ); 207 211 208 212 // mat iRY = inv(_Ry->to_mat()); … … 211 215 212 216 // 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 ); 216 219 217 220 /* vec mu2 = *_mu + ( _K2 ) * ( y-*_yp );*/ 218 221 219 222 //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 ; 221 225 222 226 /* _K = (_P->to_mat())*C.transpose() * ( _iRy->to_mat() ); 223 227 *_mu = pfxu->eval ( *_mu ,u ) + ( _K )* ( y-*_yp );*/ 224 228 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; 228 232 229 233 if ( evalll==true ) { //likelihood of observation y