Show
Ignore:
Timestamp:
08/17/10 22:06:50 (14 years ago)
Author:
smidl
Message:

Kalman with UD (bierman, thorton) and tests

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/estim/kalman.h

    r1154 r1158  
    362362 
    363363    void validate() { 
    364         KalmanFull::validate(); 
     364       // KalmanFull::validate(); 
    365365 
    366366        // check stats and IM and OM 
     
    426426SHAREDPTR ( EKFCh ); 
    427427 
     428//! EKF using bierman and Thorton code 
     429class EKF_UD : public BM { 
     430        protected: 
     431                //! Internal Model f(x,u) 
     432                shared_ptr<diffbifn> pfxu; 
     433                 
     434                //! Observation Model h(x,u) 
     435                shared_ptr<diffbifn> phxu; 
     436                 
     437                //! U part 
     438                mat U; 
     439                //! D part 
     440                vec D; 
     441                 
     442                mat A; 
     443                mat C; 
     444                mat Q; 
     445                vec R; 
     446 
     447                enorm<ldmat> est; 
     448        public: 
     449                //! copy constructor duplicated  
     450                EKF_UD* _copy() const { 
     451                        return new EKF_UD(*this); 
     452                } 
     453 
     454                const enorm<ldmat>& posterior()const{return est;}; 
     455                 
     456                enorm<ldmat>& prior() { 
     457                        return const_cast<enorm<ldmat>&>(posterior()); 
     458                } 
     459                 
     460 
     461                EKF_UD(){} 
     462                 
     463                EKF_UD(const EKF_UD &E0): pfxu(E0.pfxu),phxu(E0.phxu), U(E0.U), D(E0.D){} 
     464                 
     465                //! Set nonlinear functions for mean values and covariance matrices. 
     466                void set_parameters ( const shared_ptr<diffbifn> &pfxu, const shared_ptr<diffbifn> &phxu, const mat Q0, const vec R0 ); 
     467                 
     468                //! Here dt = [yt;ut] of appropriate dimensions 
     469                void bayes ( const vec &yt, const vec &cond = empty_vec ); 
     470                 
     471                /*! Create object from the following structure 
     472                 
     473                \code 
     474                class = 'EKF_UD'; 
     475                OM = configuration of bdm::diffbifn;    % any offspring of diffbifn, bdm::diffbifn::from_setting 
     476                IM = configuration of bdm::diffbifn;    % any offspring of diffbifn, bdm::diffbifn::from_setting 
     477                dQ = [...];                             % vector containing diagonal of Q 
     478                dR = [...];                             % vector containing diagonal of R 
     479                --- optional fields --- 
     480                mu0 = [...];                            % vector of statistics mu0 
     481                dP0 = [...];                            % vector containing diagonal of P0 
     482                -- or -- 
     483                P0 = [...];                             % full matrix P0 
     484                --- inherited fields --- 
     485                bdm::BM::from_setting 
     486                \endcode 
     487                If the optional fields are not given, they will be filled as follows: 
     488                \code 
     489                mu0 = [0,0,0,....];                     % empty statistics 
     490                P0 = eye( dim );               
     491                \endcode 
     492                */ 
     493                void from_setting ( const Setting &set ); 
     494                 
     495                void validate() {}; 
     496                // TODO dodelat void to_setting( Setting &set ) const; 
     497                 
     498}; 
     499UIREGISTER(EKF_UD); 
     500 
    428501class UKFCh : public EKFCh{ 
    429502        public: 
     
    468541         
    469542                //step 4 
    470                 mat Xpred_dif(dim2,dim); 
     543                mat P4=Q.to_mat(); 
     544                vec tmp; 
    471545                for (i=0;i<dim2;i++){ 
    472                         Xpred_dif.set_row(i, sqrt(w(i)) * (Xik.get_col(i) - xp)); 
     546                        tmp = Xik.get_col(i)-xp; 
     547                        P4+=w(i)*outer_product(tmp,tmp); 
    473548                }                
    474                  
    475                 // NEW Sigma points 
    476                 mat tmp; 
    477                 qr(concat_vertical(Xpred_dif,Q._Ch()), tmp); 
    478                 mat Ppred=tmp.get_rows(0,dim-1); 
    479                 // New Xi(k+1) 
    480                 Xik.set_col(0,xp); 
    481                 for ( i=0;i<dim; i++){ 
    482                         vec tmp=sqrt(npk)*Ppred.get_col(i); 
    483                         Xik.set_col(i+1, _mu+tmp); 
    484                         Xik.set_col(i+1+dim, _mu-tmp); 
    485                 } 
    486                 for (i=0;i<dim2;i++){ 
    487                         Xpred_dif.set_row(i, sqrt(w(i)) * (Xik.get_col(i) - xp)); 
    488                 }                
    489                  
    490                  
    491                  
     549                                         
    492550                //step 5 
    493551                mat Yi(dimy,dim2); 
     
    501559                } 
    502560                //step 7 
    503                 mat Ypred_dif(dim2,dimy); 
    504                  
    505                 for (i=0; i<dim2; i++){ 
    506                         Ypred_dif.set_row(i, sqrt(w(i))*(Yi.get_col(i)-_yp)); 
    507                 } 
    508                 if (!qr(concat_vertical(Ypred_dif,R._Ch()) ,tmp)){ bdm_warning("QR failed");} 
    509                 _Ry._Ch() = tmp.get_rows(0,dimy-1); 
     561                mat Pvv=R.to_mat(); 
     562                for (i=0;i<dim2;i++){ 
     563                        tmp = Yi.get_col(i)-_yp; 
     564                        Pvv+=w(i)*outer_product(tmp,tmp); 
     565                }                
     566                _Ry._Ch() = chol(Pvv); 
    510567                 
    511568                // step 8 
    512                 mat Pxy=Xpred_dif.T()*Ypred_dif; 
     569                mat Pxy=zeros(dim,dimy); 
     570                for (i=0;i<dim2;i++){ 
     571                        Pxy+=w(i)*outer_product(Xi.get_col(i)-xp, Yi.get_col(i)-_yp); 
     572                }                
    513573                mat iRy=inv(_Ry._Ch()); 
    514574                 
     
    516576                mat K=Pxy*iRy*iRy.T(); 
    517577                mat K2=Pxy*inv(_Ry.to_mat()); 
    518                  
     578                                 
    519579                /////////////// new filtering density 
    520580                _mu = xp + K*(yt - _yp); 
     581                 
     582                if ( _mu ( 3 ) >pi ) _mu ( 3 )-=2*pi; 
     583                if ( _mu ( 3 ) <-pi ) _mu ( 3 ) +=2*pi; 
    521584                // fill the space in Ppred; 
    522                 mat Rpred =concat_vertical(Xpred_dif - Ypred_dif*K.T(), _Ry._Ch()*K.T()); 
    523                  
    524                 if(!qr(Rpred,tmp)) {bdm_warning("QR failed");} 
    525                 _P._Ch()=tmp.get_rows(0,dim-1); 
     585                _P._Ch()=chol(P4-K*_Ry.to_mat()*K.T()); 
    526586        } 
    527587        void from_setting(const Setting &set){