root/pmsm/sim_var_arx.cpp @ 254

Revision 254, 2.2 kB (checked in by smidl, 15 years ago)

create namespace bdm

  • Property svn:eol-style set to native
Line 
1/*!
2  \file
3  \brief Disturbances of PMSM model are fitted by an ARX model
4  \author Vaclav Smidl.
5
6  \ingroup PMSM
7  -----------------------------------
8  BDM++ - C++ library for Bayesian Decision Making under Uncertainty
9
10  Using IT++ for numerical operations
11  -----------------------------------
12*/
13
14#include <itpp/itbase.h>
15#include <estim/arx.h>
16
17#include <stat/loggers.h>
18
19using namespace bdm;
20
21vec getPsi_a ( int t, mat &D, mat &Du , mat &X );
22vec getPsi_b ( int t, mat &D, mat &Du , mat &X );
23
24int main() {
25        // Kalman filter
26        int Ndat = 90000;
27        mat D;
28        mat Du;
29        mat X;
30
31        dirfilelog L ( "exp/sim_var_arx",1000 );
32
33        it_file itf ( "sim_var.it" );
34        itf >> Name ( "D" ) >> D;
35        itf >> Name ( "Du" ) >> Du;
36        itf >> Name ( "X" ) >> X;
37
38        Array<std::string> Names = "{ia ib om dom r }";
39        int rglen = Names.length();
40        //Regressor
41        RV rgr ( Names );
42        mat V0 = 0.0001*eye ( rglen ); V0 ( 0,0 ) =200;
43        double nu0 = rglen+1;
44
45        //Autoregressive model
46        ARX Ar_a ( rgr,V0,nu0 ,0.95 );
47        ARX Ar_b ( rgr,V0,nu0 ,0.95 );
48        const epdf& pA= Ar_a._epdf();
49        const epdf& pB= Ar_b._epdf();
50
51        RV rta ( "{th_a }",vec_1 ( rglen ) );
52        RV rtb ( "{th_b }",vec_1 ( rglen ) );
53        int tha_log = L.add ( rta,"" );
54        int thb_log = L.add ( rtb,"" );
55        L.init();
56
57        vec Psi ( rglen );
58        vec Save ( 13 );
59        for ( int t=2; t<Ndat; t++ ) {
60                Psi =getPsi_a ( t, D,Du ,X );
61                Ar_a.bayes ( Psi );
62                Psi =getPsi_b ( t, D,Du ,X );
63                Ar_b.bayes ( Psi );
64
65                Save = pA.mean();
66                L.logit ( tha_log, Save );
67                Save = pB.mean();
68                L.logit ( thb_log, Save );
69                L.step();
70        }
71        L.finalize();
72
73        ivec bestind = Ar_a.structure_est ( egiw ( rgr,V0,nu0 ) );
74        ivec bestind2 = Ar_b.structure_est ( egiw ( rgr,V0,nu0 ) );
75        cout << bestind <<endl;
76        cout << bestind2 <<endl;
77
78        return 0;
79}
80
81vec getPsi_a ( int t, mat &D, mat &Du, mat &X ) {
82        vec Psi ( 5 );
83        Psi ( 0 ) = D ( t,0 )-Du ( t,0 ); // a=0
84
85        Psi ( 1 ) = D ( t,2 );
86        Psi ( 2 ) = D(t,2)-D(t-1,2 );
87        Psi ( 3 ) = D(t,0)-D(t-1,0 );
88        Psi ( 4 ) = X ( t,2 ) - X ( t-1,2 );
89
90        return Psi;
91}
92
93vec getPsi_b ( int t, mat &D, mat &Du, mat &X ) {
94        vec Psi ( 5 );
95        Psi ( 0 ) = D ( t,1 )-Du ( t,1 ); //b=1
96
97        Psi ( 1 ) = D(t,3);
98        Psi ( 2 ) = D(t,3)-D(t-1,3 );
99        Psi ( 3 ) = D ( t,1 )-D(t-1,1);
100        Psi ( 4 ) = X ( t,2 ) - X ( t-1,2 );
101
102        return Psi;
103}
Note: See TracBrowser for help on using the browser.