root/applications/pmsm/simulator_zdenek/ekf_example/ekf_obj.h @ 1468

Revision 1468, 22.6 kB (checked in by smidl, 11 years ago)

uprava generatoru

  • Property svn:eol-style set to native
Line 
1/*!
2  \file
3  \brief Bayesian Filtering for linear Gaussian models (Kalman Filter) and extensions
4  \author Vaclav Smidl.
5
6  -----------------------------------
7  BDM++ - C++ library for Bayesian Decision Making under Uncertaint16y
8
9  Using IT++ for numerical operations
10  -----------------------------------
11*/
12
13#ifndef EKFfix_H
14#define EKFfix_H
15
16
17#include <estim/kalman.h>
18#include "fixed.h"
19#include "matrix.h"
20#include "matrix_vs.h"
21#include "reference_Q15.h"
22#include "parametry_motoru.h"
23#include "mpf_double.h"
24#include "fast_exp.h"
25#include "ekf_mm.h"
26#include "qmath.h"
27
28using namespace bdm;
29
30double minQ(double Q); 
31
32void mat_to_int16(const imat &M, int16 *I);
33void vec_to_int16(const ivec &v, int16 *I);
34void int16_to_mat(int16 *I, imat &M, int rows, int cols);
35void int16_to_vec(int16 *I, ivec &v, int len);
36void UDtof(const mat &U, const vec &D, imat &Uf, ivec &Df, const vec &xref);
37
38
39//! EKF for testing q44
40class EKFtest: public EKF_UD{
41        void bayes ( const vec &yt, const vec &cond ) {
42                EKF_UD::bayes(yt,cond);
43                vec D =  prior()._R()._D();
44               
45                if (D(3)>10) D(3) = 10;
46               
47                prior()._R().__D()=D;
48        }
49};
50UIREGISTER(EKFtest);
51
52
53class EKFscale: public EKFCh{
54        LOG_LEVEL(EKFscale, logCh, logA, logC);
55public:
56        mat Tx;
57        mat Ty;
58       
59        void from_setting ( const Setting &set ){
60                EKFCh::from_setting(set);
61                vec v;
62                UI::get(v, set, "xmax", UI::compulsory);
63                Tx = diag(1./v);
64                UI::get(v, set, "ymax", UI::compulsory);
65                Ty = diag(1./v);
66               
67                UI::get(log_level, set, "log_level", UI::optional);
68        };
69       
70        void log_register(logger &L, const string &prefix){
71                BM::log_register ( L, prefix );
72               
73                L.add_vector ( log_level, logCh, RV ("Ch", dimension()*dimension() ), prefix );
74                L.add_vector ( log_level, logA, RV ("A", dimension()*dimension() ), prefix );
75                L.add_vector ( log_level, logC, RV ("C", dimensiony()*dimension() ), prefix );
76        };
77       
78        void log_write() const{
79                BM::log_write();
80            if ( log_level[logCh] ) {
81                        mat Chsc=Tx*(est._R()._Ch());
82                        vec v(Chsc._data(), dimension()*dimension());
83                        if (v(0)<0)
84                                v= -v;
85                        log_level.store( logCh, round(v*32767));
86                }
87                if (log_level[logA]){
88                        mat Asc = Tx*A*inv(Tx);
89                        vec v(Asc._data(), dimension()*dimension());
90                        log_level.store( logA, round(v*32767));
91                }
92                if (log_level[logC]){
93                        mat Csc = Ty*C*inv(Tx);
94                        vec v(Csc._data(), dimensiony()*dimension());
95                        log_level.store( logC, round(v*32767));
96                }
97        }
98       
99};
100UIREGISTER(EKFscale);
101
102/*!
103\brief Extended Kalman Filter with UD matrices in fixed point16 arithmetic
104
105An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean.
106*/
107class EKFfixedUD : public BM {
108        public:
109                LOG_LEVEL(EKFfixedUD,logU, logG, logD, logA, logP);
110               
111                void init_ekf(double Tv);
112                void ekf(double ux, double uy, double isxd, double isyd);
113                               
114                /* Constants - definovat jako konstanty ?? ?kde je vyhodnejsi aby v pameti byli?*/
115                int16 Q[16]; /* matrix [4,4] */
116                int16 R[4]; /* matrix [2,2] */
117               
118                int16 x_est[4]; /* estimate and prediction */
119               
120                int16 PSI[16]; /* matrix [4,4] */
121                int16 PSIU[16]; /* matrix PIS*U, [4,4] */
122               
123                int16 Uf[16]; // upper triangular of covariance (inplace)
124                int16 Df[4];  // diagonal covariance
125                int16 Dfold[4]; // temp of D
126                int16 G[16];  // temp for bierman
127               
128                int16 cA, cB, cC, cG, cH;  // cD, cE, cF, cI ... nepouzivane
129               
130                enorm<fsqmat> E;
131                mat Ry;
132               
133        public:
134                //! Default constructor
135                EKFfixedUD ():BM(),E(),Ry(2,2){
136                        int16 i;
137                        for(i=0;i<16;i++){Q[i]=0;}
138                        for(i=0;i<4;i++){R[i]=0;}
139                       
140                        for(i=0;i<4;i++){x_est[i]=0;}
141                        for(i=0;i<16;i++){Uf[i]=0;}
142                        for(i=0;i<4;i++){Df[i]=0;}
143                        for(i=0;i<16;i++){G[i]=0;}
144                        for(i=0;i<4;i++){Dfold[i]=0;}
145                       
146                        for(i=0;i<16;i++){PSI[i]=0;}
147                       
148                        set_dim(4);
149                        dimy = 2;
150                        dimc = 2;
151                        E._mu()=zeros(4);
152                        E._R()=zeros(4,4);
153                        init_ekf(0.000125);
154                };
155                //! Here dt = [yt;ut] of appropriate dimensions
156                void bayes ( const vec &yt, const vec &ut );
157                //!dummy!
158                const epdf& posterior() const {return E;};
159                void log_register(logger &L, const string &prefix){
160                        BM::log_register ( L, prefix );
161                       
162                                L.add_vector ( log_level, logG, RV("G",16), prefix );
163                                L.add_vector ( log_level, logU, RV ("U", 16 ), prefix );
164                                L.add_vector ( log_level, logD, RV ("D", 4 ), prefix );
165                                L.add_vector ( log_level, logA, RV ("A", 16 ), prefix );
166                                L.add_vector ( log_level, logP, RV ("P", 16 ), prefix );
167                               
168                };
169                //void from_setting();
170};
171
172UIREGISTER(EKFfixedUD);
173
174/*!
175 * \brief Extended Kalman Filter with UD matrices in fixed point16 arithmetic
176 *
177 * An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean.
178 */
179class EKFfixedUD2 : public BM {
180public:
181        LOG_LEVEL(EKFfixedUD2,logU, logG, logD, logA, logC, logP);
182       
183        void init_ekf2(double Tv);
184        void ekf2(double ux, double uy, double isxd, double isyd);
185       
186        /* Constants - definovat jako konstanty ?? ?kde je vyhodnejsi aby v pameti byli?*/
187        int16 Q[4]; /* matrix [4,4] */
188        int16 R[4]; /* matrix [2,2] */
189       
190        int16 x_est[2]; /* estimate and prediction */
191        int16 y_est[2]; /* estimate and prediction */
192        int16 y_old[2]; /* estimate and prediction */
193       
194        int16 PSI[4]; /* matrix [4,4] */
195        int16 PSIU[4]; /* matrix PIS*U, [4,4] */
196        int16 C[4]; /* matrix [4,4] */
197       
198        int16 Uf[4]; // upper triangular of covariance (inplace)
199        int16 Df[2];  // diagonal covariance
200        int16 Dfold[2]; // temp of D
201        int16 G[4];  // temp for bierman
202       
203        int16 cA, cB, cC, cG, cH;  // cD, cE, cF, cI ... nepouzivane
204       
205        enorm<fsqmat> E;
206        mat Ry;
207       
208public:
209        //! Default constructor
210        EKFfixedUD2 ():BM(),E(),Ry(2,2){
211                int16 i;
212                for(i=0;i<4;i++){Q[i]=0;}
213                for(i=0;i<4;i++){R[i]=0;}
214               
215                for(i=0;i<2;i++){x_est[i]=0;}
216                for(i=0;i<2;i++){y_est[i]=0;}
217                for(i=0;i<2;i++){y_old[i]=0;}
218                for(i=0;i<4;i++){Uf[i]=0;}
219                for(i=0;i<2;i++){Df[i]=0;}
220                for(i=0;i<4;i++){G[i]=0;}
221                for(i=0;i<2;i++){Dfold[i]=0;}
222               
223                for(i=0;i<4;i++){PSI[i]=0;}
224                for(i=0;i<4;i++){C[i]=0;}
225               
226                set_dim(2);
227                dimc = 2;
228                dimy = 2;
229                E._mu()=zeros(2);
230                E._R()=zeros(2,2);
231                init_ekf2(0.000125);
232        };
233        //! Here dt = [yt;ut] of appropriate dimensions
234        void bayes ( const vec &yt, const vec &ut );
235        //!dummy!
236        const epdf& posterior() const {return E;};
237        void log_register(logger &L, const string &prefix){
238                BM::log_register ( L, prefix );
239               
240                L.add_vector ( log_level, logG, RV("G2",4), prefix );
241                L.add_vector ( log_level, logU, RV ("U2", 4 ), prefix );
242                L.add_vector ( log_level, logD, RV ("D2", 2 ), prefix );
243                L.add_vector ( log_level, logA, RV ("A2", 4 ), prefix );
244                L.add_vector ( log_level, logC, RV ("C2", 4 ), prefix );
245                L.add_vector ( log_level, logP, RV ("P2", 4 ), prefix );
246               
247        };
248        //void from_setting();
249};
250
251UIREGISTER(EKFfixedUD2);
252
253/*!
254 * \brief Extended Kalman Filter with UD matrices in fixed point16 arithmetic
255 *
256 * An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean.
257 */
258class EKFfixedUD3 : public BM {
259public:
260        LOG_LEVEL(EKFfixedUD3,logU, logG, logD, logA, logC, logP);
261       
262        void init_ekf3(double Tv);
263        void ekf3(double ux, double uy, double isxd, double isyd);
264       
265        /* Constants - definovat jako konstanty ?? ?kde je vyhodnejsi aby v pameti byli?*/
266        int16 Q[9]; /* matrix [4,4] */
267        int16 R[4]; /* matrix [2,2] */
268       
269        int16 x_est[3]; /* estimate and prediction */
270        int16 y_est[2]; /* estimate and prediction */
271        int16 y_old[2]; /* estimate and prediction */
272       
273        int16 PSI[9]; /* matrix [4,4] */
274        int16 PSIU[9]; /* matrix PIS*U, [4,4] */
275        int16 C[6]; /* matrix [4,4] */
276       
277        int16 Uf[9]; // upper triangular of covariance (inplace)
278        int16 Df[3];  // diagonal covariance
279        int16 Dfold[3]; // temp of D
280        int16 G[9];  // temp for bierman
281       
282        int16 cA, cB, cC, cG, cF, cH;  // cD, cE, cF, cI ... nepouzivane
283       
284        enorm<fsqmat> E;
285        mat Ry;
286       
287public:
288        //! Default constructor
289        EKFfixedUD3 ():BM(),E(),Ry(2,2){
290                int16 i;
291                for(i=0;i<9;i++){Q[i]=0;}
292                for(i=0;i<4;i++){R[i]=0;}
293               
294                for(i=0;i<3;i++){x_est[i]=0;}
295                for(i=0;i<2;i++){y_est[i]=0;}
296                for(i=0;i<2;i++){y_old[i]=0;}
297                for(i=0;i<9;i++){Uf[i]=0;}
298                for(i=0;i<3;i++){Df[i]=0;}
299                for(i=0;i<4;i++){G[i]=0;}
300                for(i=0;i<3;i++){Dfold[i]=0;}
301               
302                for(i=0;i<9;i++){PSI[i]=0;}
303                for(i=0;i<6;i++){C[i]=0;}
304               
305                set_dim(3);
306                dimc = 2;
307                dimy = 2;
308                E._mu()=zeros(3);
309                E._R()=zeros(3,3);
310                init_ekf3(0.000125);
311        };
312        //! Here dt = [yt;ut] of appropriate dimensions
313        void bayes ( const vec &yt, const vec &ut );
314        //!dummy!
315        const epdf& posterior() const {return E;};
316        void log_register(logger &L, const string &prefix){
317                BM::log_register ( L, prefix );         
318        };
319        //void from_setting();
320};
321
322UIREGISTER(EKFfixedUD3);
323
324/*!
325 * \brief Extended Kalman Filter with Chol matrices in fixed point16 arithmetic
326 *
327 * An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean.
328 */
329class EKFfixedCh : public BM {
330public:
331        LOG_LEVEL(EKFfixedCh,logCh, logA, logP);
332       
333        void init_ekf(double Tv);
334        void ekf(double ux, double uy, double isxd, double isyd);
335       
336        /* Constants - definovat jako konstanty ?? ?kde je vyhodnejsi aby v pameti byli?*/
337        int16 Q[16]; /* matrix [4,4] */
338        int16 R[4]; /* matrix [2,2] */
339       
340        int16 x_est[4]; /* estimate and prediction */
341       
342        int16 PSI[16]; /* matrix [4,4] */
343        int16 PSICh[16]; /* matrix PIS*U, [4,4] */
344       
345        int16 Chf[16]; // upper triangular of covariance (inplace)
346       
347        int16 cA, cB, cC, cG, cH;  // cD, cE, cF, cI ... nepouzivane
348       
349        enorm<chmat> E;
350        mat Ry;
351       
352public:
353        //! Default constructor
354        EKFfixedCh ():BM(),E(),Ry(2,2){
355                int16 i;
356                for(i=0;i<16;i++){Q[i]=0;}
357                for(i=0;i<4;i++){R[i]=0;}
358               
359                for(i=0;i<4;i++){x_est[i]=0;}
360                for(i=0;i<16;i++){Chf[i]=0;}
361               
362                for(i=0;i<16;i++){PSI[i]=0;}
363               
364                set_dim(4);
365                dimc = 2;
366                dimy =2;
367                E._mu()=zeros(4);
368                E._R()=zeros(4,4);
369                init_ekf(0.000125);
370        };
371        //! Here dt = [yt;ut] of appropriate dimensions
372        void bayes ( const vec &yt, const vec &ut );
373        //!dummy!
374        const epdf& posterior() const {return E;};
375        void log_register(logger &L, const string &prefix){
376                BM::log_register ( L, prefix );
377               
378                L.add_vector ( log_level, logCh, RV ("Ch", 16 ), prefix );
379                L.add_vector ( log_level, logA, RV ("A", 16 ), prefix );
380                L.add_vector ( log_level, logP, RV ("P", 16 ), prefix );               
381        };
382        //void from_setting();
383};
384
385UIREGISTER(EKFfixedCh);
386
387/*!
388 * \brief Extended Kalman Filter with UD matrices in fixed point16 arithmetic
389 *
390 * An approximation of the exact Bayesian filter with Gaussian noices and non-linear evolutions of their mean.
391 */
392class EKFfixedCh2 : public BM {
393public:
394        LOG_LEVEL(EKFfixedCh2,logCh, logA, logC, logP, logDet, logRem);
395       
396        void init_ekf2(double Tv);
397        void ekf2(double ux, double uy, double isxd, double isyd);
398       
399        /* Constants - definovat jako konstanty ?? ?kde je vyhodnejsi aby v pameti byli?*/
400        int16 Q[4]; /* matrix [4,4] */
401        int16 R[4]; /* matrix [2,2] */
402       
403        int16 x_est[2]; /* estimate and prediction */
404        int16 y_est[2]; /* estimate and prediction */
405        int16 y_old[2]; /* estimate and prediction */
406       
407        int16 PSI[4]; /* matrix [4,4] */
408        int16 PSICh[4]; /* matrix PIS*U, [4,4] */
409        int16 C[4]; /* matrix [4,4] */
410       
411        int16 Chf[4]; // upper triangular of covariance (inplace)
412       
413        int16 cA, cB, cC, cG, cH;  // cD, cE, cF, cI ... nepouzivane
414       
415        enorm<fsqmat> E;
416        mat Ry;
417       
418public:
419        //! Default constructor
420        EKFfixedCh2 ():BM(),E(),Ry(2,2){
421                int16 i;
422                for(i=0;i<4;i++){Q[i]=0;}
423                for(i=0;i<4;i++){R[i]=0;}
424               
425                for(i=0;i<2;i++){x_est[i]=0;}
426                for(i=0;i<2;i++){y_est[i]=0;}
427                for(i=0;i<2;i++){y_old[i]=0;}
428                for(i=0;i<4;i++){Chf[i]=0;}
429               
430                for(i=0;i<4;i++){PSI[i]=0;}
431                for(i=0;i<4;i++){C[i]=0;}
432               
433                set_dim(2);
434                dimc = 2;
435                dimy = 2;
436                E._mu()=zeros(2);
437                E._R()=zeros(2,2);
438                init_ekf2(0.000125);
439        };
440        //! Here dt = [yt;ut] of appropriate dimensions
441        void bayes ( const vec &yt, const vec &ut );
442        //!dummy!
443        const epdf& posterior() const {return E;};
444        void log_register(logger &L, const string &prefix){
445                BM::log_register ( L, prefix );
446               
447                L.add_vector ( log_level, logCh, RV ("Ch2", 4 ), prefix );
448                L.add_vector ( log_level, logA, RV ("A2", 4 ), prefix );
449                L.add_vector ( log_level, logC, RV ("C2", 4 ), prefix );
450                L.add_vector ( log_level, logP, RV ("P2", 4 ), prefix );
451                L.add_vector ( log_level, logDet, RV ("Det", 1 ), prefix );
452                L.add_vector ( log_level, logRem, RV ("Rem", 1 ), prefix );
453               
454        };
455        void from_setting ( const Setting &set ){
456                BM::from_setting(set);
457                vec dQ,dR;
458                UI::get ( dQ, set, "dQ", UI::optional );
459                UI::get ( dQ, set, "dR", UI::optional );
460                if (dQ.length()==2){
461                        Q[0]=prevod(dQ[0],15);      // 1e-3
462                        Q[3]=prevod(dQ[1],15);      // 1e-3
463                }
464                if (dR.length()==2){
465                        R[0]=prevod(dR[0],15);      // 1e-3
466                        R[3]=prevod(dR[1],15);      // 1e-3
467                }
468        }
469};
470
471UIREGISTER(EKFfixedCh2);
472
473
474//! EKF for comparison of EKF_UD with its fixed-point16 implementation
475class EKF_UDfix : public BM {
476        protected:
477                //! logger
478                LOG_LEVEL(EKF_UDfix,logU, logG);
479                //! Internal Model f(x,u)
480                shared_ptr<diffbifn> pfxu;
481               
482                //! Observation Model h(x,u)
483                shared_ptr<diffbifn> phxu;
484               
485                //! U part
486                mat U;
487                //! D part
488                vec D;
489                               
490                mat A;
491                mat C;
492                mat Q;
493                vec R;
494               
495                enorm<ldmat> est;
496               
497               
498        public:
499               
500                //! copy constructor duplicated
501                EKF_UDfix* _copy() const {
502                        return new EKF_UDfix(*this);
503                }
504               
505                const enorm<ldmat>& posterior()const{return est;};
506               
507                enorm<ldmat>& prior() {
508                        return const_cast<enorm<ldmat>&>(posterior());
509                }
510               
511                EKF_UDfix(){}
512               
513               
514                EKF_UDfix(const EKF_UDfix &E0): pfxu(E0.pfxu),phxu(E0.phxu), U(E0.U), D(E0.D){}
515               
516                //! Set nonlinear functions for mean values and covariance matrices.
517                void set_parameters ( const shared_ptr<diffbifn> &pfxu, const shared_ptr<diffbifn> &phxu, const mat Q0, const vec R0 );
518               
519                //! Here dt = [yt;ut] of appropriate dimensions
520                void bayes ( const vec &yt, const vec &cond = empty_vec );
521               
522                void log_register ( bdm::logger& L, const string& prefix ){
523                        BM::log_register ( L, prefix );
524                       
525                        if ( log_level[logU] )
526                                L.add_vector ( log_level, logU, RV ( dimension()*dimension() ), prefix );
527                        if ( log_level[logG] )
528                                L.add_vector ( log_level, logG, RV ( dimension()*dimension() ), prefix );
529                       
530                }
531                /*! Create object from the following structure
532               
533                \code
534                class = 'EKF_UD';
535                OM = configuration of bdm::diffbifn;    % any offspring of diffbifn, bdm::diffbifn::from_setting
536                IM = configuration of bdm::diffbifn;    % any offspring of diffbifn, bdm::diffbifn::from_setting
537                dQ = [...];                             % vector containing diagonal of Q
538                dR = [...];                             % vector containing diagonal of R
539                --- optional fields ---
540                mu0 = [...];                            % vector of statistics mu0
541                dP0 = [...];                            % vector containing diagonal of P0
542                -- or --
543                P0 = [...];                             % full matrix P0
544                --- inherited fields ---
545                bdm::BM::from_setting
546                \endcode
547                If the optional fields are not given, they will be filled as follows:
548                \code
549                mu0 = [0,0,0,....];                     % empty statistics
550                P0 = eye( dim );             
551                \endcode
552                */
553                void from_setting ( const Setting &set );
554               
555                void validate() {};
556                // TODO dodelat void to_setting( Setting &set ) const;
557               
558};
559UIREGISTER(EKF_UDfix);
560
561
562class MPF_pmsm_red:public BM{
563                double qom, qth, r;
564
565
566public:
567                MPF_pmsm_red(){
568                        dimy=2;
569                        dimc=2;
570                        qom=1e-1;
571                        qth=1e-6;
572                        r=1e-1;
573                };
574        void bayes ( const vec &val, const vec &cond ) {
575/*              const double &isa = val(0);
576                const double &isb = val(1);
577                const double &usa = cond(0);
578                const double &usb = cond(1);*/
579                mpf_bayes((floatx)val(0),(floatx)val(1),(floatx)cond(0), (floatx)cond(1));//isa,isb,usa,usb);
580        }
581       
582        class mp:public epdf{
583                LOG_LEVEL(mp,logth,logom);
584        public:
585                mp():epdf(){set_dim(3);         log_level[logth]=true;log_level[logom]=true;
586}
587                vec sample() const {return zeros(3);}
588                double evallog(const vec &v) const {return 0.0;}
589                vec mean() const {vec tmp(3); 
590                        floatx es,ec,eo;
591                        mpf_mean(&es, &ec, &eo); 
592                        tmp(0)=es;tmp(1)=ec;tmp(2)=eo;
593                        return tmp;
594                }
595                vec variance() const {return zeros(3);}
596                void log_register ( bdm::logger& L, const string& prefix ) {
597                        epdf::log_register ( L, prefix );
598                        if ( log_level[logth] ) {
599                                int th_dim = N; // dimension - dimension of cov
600                                L.add_vector( log_level, logth, RV ( th_dim ), prefix );
601                        }
602                        if ( log_level[logom] ) {
603                                int th_dim = N; // dimension - dimension of cov
604                                L.add_vector( log_level, logom, RV ( th_dim ), prefix );
605                        }
606                }
607                void log_write() const {
608                        epdf::log_write();
609                        if ( log_level[logth] ) {
610                                floatx Th[N];
611                                mpf_th(Th);
612                                vec th(N); 
613                                for(int i=0;i<N;i++){th(i)=Th[i];}
614                                log_level.store( logth, th );
615                        }
616                        if ( log_level[logom] ) {
617                                floatx Om[N];
618                                mpf_om(Om);
619                                vec om(N); 
620                                for(int i=0;i<N;i++){om(i)=Om[i];}
621                                log_level.store( logom, om );
622                        }
623                }
624        };
625       
626        mp mypdf;
627        const mp& posterior() const {return mypdf;}
628       
629        void from_setting(const Setting &set){
630            BM::from_setting(set);
631                UI::get ( log_level, set, "log_level", UI::optional );
632
633                UI::get(qom,set,"qom",UI::optional);
634                UI::get(qth,set,"qth",UI::optional);
635                UI::get(r,set,"r",UI::optional);
636        }
637        void validate(){
638                mpf_init((floatx)qom,(floatx)qth,(floatx)r);
639               
640        }
641};
642UIREGISTER(MPF_pmsm_red);
643
644//! EKF with covariance R estimated by the VB method
645class EKFvbR: public EKFfull{   
646    LOG_LEVEL(EKFvbR,logR,logQ,logP);
647
648        //! Statistics of the R estimator
649        mat PsiR;
650        //! Statistics of the Q estimator
651        mat PsiQ;
652        //! forgetting factor
653        double phi;
654        //! number of VB iterations
655        int niter;
656        //! degrees of freedom
657        double nu;
658        //! stabilizing element
659        mat PsiR0;
660        //! stabilizing element
661        mat PsiQ0;
662       
663        void from_setting(const Setting &set){
664            EKFfull::from_setting(set);
665                if (!UI::get(phi,set,"phi",UI::optional)){
666                        phi = 0.99;
667                }
668                if (!UI::get(niter,set,"niter",UI::optional)){
669                        niter = 3;
670                }
671                PsiQ = Q;
672                PsiQ0 = Q;
673                PsiR = R;
674                PsiR0 = R;
675                nu = 3;
676    }   
677        void log_register ( logger &L, const string &prefix ) {
678                EKFfull::log_register(L,prefix);
679                L.add_vector(log_level, logR, RV("{R }",vec_1<int>(4)), prefix);
680                L.add_vector(log_level, logQ, RV("{Q }",vec_1<int>(4)), prefix);
681                L.add_vector(log_level, logP, RV("{P }",vec_1<int>(4)), prefix);
682        }
683        void bayes ( const vec &val, const vec &cond ) {
684                vec diffx, diffy;
685                mat Psi_vbQ;
686                mat Psi_vbR;
687                nu = phi*nu + (1-phi)*2 + 1.0;
688               
689                //save initial values of posterior
690                vec mu0=est._mu();
691                fsqmat P0=est._R();
692                vec xpred = pfxu->eval(mu0,cond);
693               
694                for (int i=0; i<niter; i++){
695                        est._mu() = mu0;
696                        est._R() = P0;
697                       
698                        EKFfull::bayes(val,cond);
699
700//                      diffy = val - fy._mu();
701//                      Psi_vbR = phi*PsiR + (1-phi)*PsiR0+ outer_product(diffy,diffy)/*+C*mat(est._R())*C.T()*/;
702//                      R = Psi_vbR/(nu-2);
703       
704                        diffx = est._mu() - xpred;
705                        Psi_vbQ = phi*PsiQ + (1-phi)*PsiQ0+ outer_product(diffx,diffx);/*mat(est._R());  A*mat(P0)*A.T();*/
706                        //
707                        Psi_vbQ(0,1) = 0.0;
708                        Psi_vbQ(1,0) = 0.0;
709                        Q = Psi_vbQ/(nu-2);
710                }
711                PsiQ = Psi_vbQ;
712                PsiR = Psi_vbR;
713//              cout <<"==" << endl << Psi << endl << diff << endl << P0 << endl << ":" << Q;
714                log_level.store(logQ, vec(Q._M()._data(),4));
715                log_level.store(logR, vec(R._M()._data(),4));
716                {
717                        mat Ch(2,2);
718                        Ch=chol(est._R()._M());
719                        log_level.store(logP, vec(Ch._data(),4));
720                }
721        }
722};
723UIREGISTER(EKFvbR);
724
725
726class ekfChfix: public BM{
727        ekf_data E;
728public:
729        ekfChfix(){
730                init_ekfCh2(&E,0.000125);set_dim(2);    dimc = 2;
731                dimy = 2;
732        } 
733        void bayes ( const vec &val, const vec &cond ) {
734                int16 ux,uy;
735                ux=prevod(cond[0]/Uref,15);
736                uy=prevod(cond[1]/Uref,15);
737               
738                int16 yx,yy;
739                // zadani mereni
740                yx=prevod(val[0]/Iref,15);
741                yy=prevod(val[1]/Iref,15);
742               
743                int32 detS;
744                int16 rem;
745                ekfCh2(&E, ux,uy,yx,yy, &detS, &rem); //detS,rem asssigned inside
746               
747                Est._mu()=vec_2(E.x_est[0]*Wref/32768., E.x_est[1]*Thetaref/32768.);
748               
749                ll = 0.99*ll+( -0.5*(double)rem/32767)*10000-0.0*log((double)detS); // detS in q(8+15+15)-(8+8). - multiplicative differece is not important
750        }
751        const epdf& posterior() const {return Est;};
752        void from_setting ( const Setting &set ){
753                BM::from_setting(set);
754                vec dQ,dR;
755                UI::get ( dQ, set, "dQ", UI::optional );
756                UI::get ( dR, set, "dR", UI::optional );
757                if (dQ.length()==2){
758                        E.Q[0]=prevod(dQ[0],15);      // 1e-3
759                        E.Q[3]=prevod(dQ[1],15);      // 1e-3
760                }
761                if (dR.length()==2){
762                        E.dR[0]=prevod(dR[0],15);      // 1e-3
763                        E.dR[1]=prevod(dR[1],15);      // 1e-3
764                }
765        }
766       
767        enorm<fsqmat> Est;
768        mat Ry;
769};
770UIREGISTER(ekfChfix);
771
772
773
774class ekfChfixQ: public BM{
775        LOG_LEVEL(ekfChfixQ,logQ,logCh,logC,logRes)
776        ekf_data E;
777        int64 PSI_Q0, PSI_Q1, PSI_Q0_reg, PSI_Q1_reg;
778        int Q_ni;
779        int phi_Q;
780public:
781        ekfChfixQ(){
782                init_ekfCh2(&E,0.000125);set_dim(2);    dimc = 2;
783                dimy = 2;
784                Q_ni = 7;
785                phi_Q = ((1<<Q_ni) -1)<<(15-Q_ni);
786
787                PSI_Q0_reg = ((int64)((1<<15)-phi_Q)*E.Q[0])<<Q_ni;
788                PSI_Q1_reg = ((int64)((1<<15)-phi_Q)*E.Q[3])<<Q_ni;
789                PSI_Q0 = ((int64)E.Q[0])<<Q_ni;
790                PSI_Q1 = ((int64)E.Q[3])<<Q_ni;
791        } 
792        void bayes ( const vec &val, const vec &cond ) {
793                int16 ux,uy;
794                ux=prevod(cond[0]/Uref,15);
795                uy=prevod(cond[1]/Uref,15);
796               
797                int16 yx,yy;
798                // zadani mereni
799                yx=prevod(val[0]/Iref,15);
800                yy=prevod(val[1]/Iref,15);
801               
802                int16  rem;
803                int32 detS;
804                ekfCh2(&E, ux,uy,yx,yy, &detS, &rem);
805               
806                Est._mu()=vec_2(E.x_est[0]*Uref/32768., E.x_est[1]*Uref/32768.);
807               
808                int16 xerr0 = E.x_est[0]-E.x_pred[0];
809                PSI_Q0 = ((int64)(phi_Q*PSI_Q0) + (((int64)xerr0*xerr0)<<15) + PSI_Q0_reg)>>15;         
810                E.Q[0] = PSI_Q0>>Q_ni;
811
812                xerr0 = E.x_est[1]-E.x_pred[1];
813                PSI_Q1 = ((int64)(phi_Q*PSI_Q1) + (((int64)xerr0*xerr0)<<15) + PSI_Q1_reg)>>15;         
814                E.Q[3] = PSI_Q1>>Q_ni;
815               
816                cout << E.Q[0] << "," << E.Q[3] <<endl;
817                ll = -0.5*qlog(detS)-0.5*rem;
818        }
819        const epdf& posterior() const {return Est;};
820        void from_setting ( const Setting &set ){
821                BM::from_setting(set);
822                vec dQ,dR;
823                UI::get ( dQ, set, "dQ", UI::optional );
824                UI::get ( dR, set, "dR", UI::optional );
825                if (dQ.length()==2){
826                        E.Q[0]=prevod(dQ[0],15);      // 1e-3
827                        E.Q[3]=prevod(dQ[1],15);      // 1e-3
828                }
829                if (dR.length()==2){
830                        E.dR[0]=prevod(dR[0],15);      // 1e-3
831                        E.dR[1]=prevod(dR[1],15);      // 1e-3
832                }
833
834                UI::get(Q_ni, set, "Q_ni", UI::optional);
835                { // zmena Q!!
836                        phi_Q = ((1<<Q_ni) -1)<<(15-Q_ni);
837
838                        PSI_Q0_reg = ((int64)((1<<15)-phi_Q)*E.Q[0])<<Q_ni;
839                        PSI_Q1_reg = ((int64)((1<<15)-phi_Q)*E.Q[3])<<Q_ni;
840                        PSI_Q0 = ((int64)E.Q[0])<<Q_ni;
841                        PSI_Q1 = ((int64)E.Q[3])<<Q_ni;
842
843                }
844                UI::get(log_level, set, "log_level", UI::optional);
845
846        }
847        void log_register ( logger &L, const string &prefix ) {
848                BM::log_register(L,prefix);
849                L.add_vector(log_level, logQ, RV("{Q }",vec_1<int>(2)), prefix);
850                L.add_vector(log_level, logCh, RV("{Ch }",vec_1<int>(3)), prefix);
851                L.add_vector(log_level, logC, RV("{C }",vec_1<int>(4)), prefix);
852                L.add_vector(log_level, logRes, RV("{dy }",vec_1<int>(2)), prefix);
853        }
854        void log_write() const {
855                BM::log_write();
856                if ( log_level[logQ] ) {
857                        log_level.store( logQ, vec_2((double)E.Q[0],(double)E.Q[3] ));
858                }
859                if ( log_level[logCh] ) {
860                        log_level.store( logCh, vec_3((double)E.Chf[0],(double)E.Chf[1], (double)E.Chf[3]));
861                }
862                if ( log_level[logRes] ) {
863                        log_level.store( logRes, vec_2((double)E.difz[0],(double)E.difz[1]));
864                }
865                if ( log_level[logC] ) {
866                        vec v(4);
867                        for (int i=0;i<4;i++) v(i)=E.C[i];
868                        log_level.store( logC, v);
869                }
870        }
871       
872        enorm<fsqmat> Est;
873        mat Ry;
874};
875UIREGISTER(ekfChfixQ);
876
877#endif // KF_H
878
Note: See TracBrowser for help on using the browser.