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

Revision 1466, 22.1 kB (checked in by smidl, 12 years ago)

opraven test Ch

  • 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);
584        public:
585                mp():epdf(){set_dim(3);         log_level[logth]=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                }
603                void log_write() const {
604                        epdf::log_write();
605                        if ( log_level[logth] ) {
606                                floatx Th[N];
607                                mpf_th(Th);
608                                vec th(N); 
609                                for(int i=0;i<N;i++){th(i)=Th[i];}
610                                log_level.store( logth, th );
611                        }
612                }
613        };
614       
615        mp mypdf;
616        const mp& posterior() const {return mypdf;}
617       
618        void from_setting(const Setting &set){
619            BM::from_setting(set);
620                UI::get ( log_level, set, "log_level", UI::optional );
621
622                UI::get(qom,set,"qom",UI::optional);
623                UI::get(qth,set,"qth",UI::optional);
624                UI::get(r,set,"r",UI::optional);
625        }
626        void validate(){
627                mpf_init((floatx)qom,(floatx)qth,(floatx)r);
628               
629        }
630};
631UIREGISTER(MPF_pmsm_red);
632
633//! EKF with covariance R estimated by the VB method
634class EKFvbR: public EKFfull{   
635    LOG_LEVEL(EKFvbR,logR,logQ,logP);
636
637        //! Statistics of the R estimator
638        mat PsiR;
639        //! Statistics of the Q estimator
640        mat PsiQ;
641        //! forgetting factor
642        double phi;
643        //! number of VB iterations
644        int niter;
645        //! degrees of freedom
646        double nu;
647        //! stabilizing element
648        mat PsiR0;
649        //! stabilizing element
650        mat PsiQ0;
651       
652        void from_setting(const Setting &set){
653            EKFfull::from_setting(set);
654                if (!UI::get(phi,set,"phi",UI::optional)){
655                        phi = 0.99;
656                }
657                if (!UI::get(niter,set,"niter",UI::optional)){
658                        niter = 3;
659                }
660                PsiQ = Q;
661                PsiQ0 = Q;
662                PsiR = R;
663                PsiR0 = R;
664                nu = 3;
665    }   
666        void log_register ( logger &L, const string &prefix ) {
667                EKFfull::log_register(L,prefix);
668                L.add_vector(log_level, logR, RV("{R }",vec_1<int>(4)), prefix);
669                L.add_vector(log_level, logQ, RV("{Q }",vec_1<int>(4)), prefix);
670                L.add_vector(log_level, logP, RV("{P }",vec_1<int>(4)), prefix);
671        }
672        void bayes ( const vec &val, const vec &cond ) {
673                vec diffx, diffy;
674                mat Psi_vbQ;
675                mat Psi_vbR;
676                nu = phi*nu + (1-phi)*2 + 1.0;
677               
678                //save initial values of posterior
679                vec mu0=est._mu();
680                fsqmat P0=est._R();
681                vec xpred = pfxu->eval(mu0,cond);
682               
683                for (int i=0; i<niter; i++){
684                        est._mu() = mu0;
685                        est._R() = P0;
686                       
687                        EKFfull::bayes(val,cond);
688
689//                      diffy = val - fy._mu();
690//                      Psi_vbR = phi*PsiR + (1-phi)*PsiR0+ outer_product(diffy,diffy)/*+C*mat(est._R())*C.T()*/;
691//                      R = Psi_vbR/(nu-2);
692       
693                        diffx = est._mu() - xpred;
694                        Psi_vbQ = phi*PsiQ + (1-phi)*PsiQ0+ outer_product(diffx,diffx);/*mat(est._R());  A*mat(P0)*A.T();*/
695                        //
696                        Psi_vbQ(0,1) = 0.0;
697                        Psi_vbQ(1,0) = 0.0;
698                        Q = Psi_vbQ/(nu-2);
699                }
700                PsiQ = Psi_vbQ;
701                PsiR = Psi_vbR;
702//              cout <<"==" << endl << Psi << endl << diff << endl << P0 << endl << ":" << Q;
703                log_level.store(logQ, vec(Q._M()._data(),4));
704                log_level.store(logR, vec(R._M()._data(),4));
705                {
706                        mat Ch(2,2);
707                        Ch=chol(est._R()._M());
708                        log_level.store(logP, vec(Ch._data(),4));
709                }
710        }
711};
712UIREGISTER(EKFvbR);
713
714
715class ekfChfix: public BM{
716        ekf_data E;
717public:
718        ekfChfix(){
719                init_ekfCh2(&E,0.000125);set_dim(2);    dimc = 2;
720                dimy = 2;
721        } 
722        void bayes ( const vec &val, const vec &cond ) {
723                int16 ux,uy;
724                ux=prevod(cond[0]/Uref,15);
725                uy=prevod(cond[1]/Uref,15);
726               
727                int16 yx,yy;
728                // zadani mereni
729                yx=prevod(val[0]/Iref,15);
730                yy=prevod(val[1]/Iref,15);
731               
732                int16 detS, rem;
733                ekfCh2(&E, ux,uy,yx,yy, &detS, &rem);
734               
735                Est._mu()=vec_2(E.x_est[0]*Uref/32768., E.x_est[1]*Uref/32768.);
736               
737                ll = -0.5*qlog(detS)-0.5*rem;
738        }
739        const epdf& posterior() const {return Est;};
740        void from_setting ( const Setting &set ){
741                BM::from_setting(set);
742                vec dQ,dR;
743                UI::get ( dQ, set, "dQ", UI::optional );
744                UI::get ( dR, set, "dR", UI::optional );
745                if (dQ.length()==2){
746                        E.Q[0]=prevod(dQ[0],15);      // 1e-3
747                        E.Q[3]=prevod(dQ[1],15);      // 1e-3
748                }
749                if (dR.length()==2){
750                        E.dR[0]=prevod(dR[0],15);      // 1e-3
751                        E.dR[1]=prevod(dR[1],15);      // 1e-3
752                }
753        }
754       
755        enorm<fsqmat> Est;
756        mat Ry;
757};
758UIREGISTER(ekfChfix);
759
760
761
762class ekfChfixQ: public BM{
763        LOG_LEVEL(ekfChfixQ,logQ,logCh,logC,logRes)
764        ekf_data E;
765        int64 PSI_Q0, PSI_Q1, PSI_Q0_reg, PSI_Q1_reg;
766        int Q_ni;
767        int phi_Q;
768public:
769        ekfChfixQ(){
770                init_ekfCh2(&E,0.000125);set_dim(2);    dimc = 2;
771                dimy = 2;
772                Q_ni = 7;
773                phi_Q = ((1<<Q_ni) -1)<<(15-Q_ni);
774
775                PSI_Q0_reg = ((int64)((1<<15)-phi_Q)*E.Q[0])<<Q_ni;
776                PSI_Q1_reg = ((int64)((1<<15)-phi_Q)*E.Q[3])<<Q_ni;
777                PSI_Q0 = ((int64)E.Q[0])<<Q_ni;
778                PSI_Q1 = ((int64)E.Q[3])<<Q_ni;
779        } 
780        void bayes ( const vec &val, const vec &cond ) {
781                int16 ux,uy;
782                ux=prevod(cond[0]/Uref,15);
783                uy=prevod(cond[1]/Uref,15);
784               
785                int16 yx,yy;
786                // zadani mereni
787                yx=prevod(val[0]/Iref,15);
788                yy=prevod(val[1]/Iref,15);
789               
790                int16 detS, rem;
791                ekfCh2(&E, ux,uy,yx,yy, &detS, &rem);
792               
793                Est._mu()=vec_2(E.x_est[0]*Uref/32768., E.x_est[1]*Uref/32768.);
794               
795                int16 xerr0 = E.x_est[0]-E.x_pred[0];
796                PSI_Q0 = ((int64)(phi_Q*PSI_Q0) + (((int64)xerr0*xerr0)<<15) + PSI_Q0_reg)>>15;         
797                E.Q[0] = PSI_Q0>>Q_ni;
798
799                xerr0 = E.x_est[1]-E.x_pred[1];
800                PSI_Q1 = ((int64)(phi_Q*PSI_Q1) + (((int64)xerr0*xerr0)<<15) + PSI_Q1_reg)>>15;         
801                E.Q[3] = PSI_Q1>>Q_ni;
802               
803                cout << E.Q[0] << "," << E.Q[3] <<endl;
804                ll = -0.5*qlog(detS)-0.5*rem;
805        }
806        const epdf& posterior() const {return Est;};
807        void from_setting ( const Setting &set ){
808                BM::from_setting(set);
809                vec dQ,dR;
810                UI::get ( dQ, set, "dQ", UI::optional );
811                UI::get ( dR, set, "dR", UI::optional );
812                if (dQ.length()==2){
813                        E.Q[0]=prevod(dQ[0],15);      // 1e-3
814                        E.Q[3]=prevod(dQ[1],15);      // 1e-3
815                }
816                if (dR.length()==2){
817                        E.dR[0]=prevod(dR[0],15);      // 1e-3
818                        E.dR[1]=prevod(dR[1],15);      // 1e-3
819                }
820
821                UI::get(Q_ni, set, "Q_ni", UI::optional);
822                { // zmena Q!!
823                        phi_Q = ((1<<Q_ni) -1)<<(15-Q_ni);
824
825                        PSI_Q0_reg = ((int64)((1<<15)-phi_Q)*E.Q[0])<<Q_ni;
826                        PSI_Q1_reg = ((int64)((1<<15)-phi_Q)*E.Q[3])<<Q_ni;
827                        PSI_Q0 = ((int64)E.Q[0])<<Q_ni;
828                        PSI_Q1 = ((int64)E.Q[3])<<Q_ni;
829
830                }
831                UI::get(log_level, set, "log_level", UI::optional);
832
833        }
834        void log_register ( logger &L, const string &prefix ) {
835                BM::log_register(L,prefix);
836                L.add_vector(log_level, logQ, RV("{Q }",vec_1<int>(2)), prefix);
837                L.add_vector(log_level, logCh, RV("{Ch }",vec_1<int>(3)), prefix);
838                L.add_vector(log_level, logC, RV("{C }",vec_1<int>(4)), prefix);
839                L.add_vector(log_level, logRes, RV("{dy }",vec_1<int>(2)), prefix);
840        }
841        void log_write() const {
842                BM::log_write();
843                if ( log_level[logQ] ) {
844                        log_level.store( logQ, vec_2((double)E.Q[0],(double)E.Q[3] ));
845                }
846                if ( log_level[logCh] ) {
847                        log_level.store( logCh, vec_3((double)E.Chf[0],(double)E.Chf[1], (double)E.Chf[3]));
848                }
849                if ( log_level[logRes] ) {
850                        log_level.store( logRes, vec_2((double)E.difz[0],(double)E.difz[1]));
851                }
852                if ( log_level[logC] ) {
853                        vec v(4);
854                        for (int i=0;i<4;i++) v(i)=E.C[i];
855                        log_level.store( logC, v);
856                }
857        }
858       
859        enorm<fsqmat> Est;
860        mat Ry;
861};
862UIREGISTER(ekfChfixQ);
863
864#endif // KF_H
865
Note: See TracBrowser for help on using the browser.