root/applications/pmsm/simulator_zdenek/test_UD.cpp

Revision 1314, 4.6 kB (checked in by smidl, 13 years ago)

test UD pro int/long variantu

Line 
1#include "ekf_example/matrix_vs.h"
2#include "ekf_example/ekf_obj.h"
3int main(){
4        int16 i;
5        mat A = 0.99*eye(5);
6        A(0.3) = 0.06;
7        A(0,2) = 0.01;
8        A(1,2) = 0.01;
9        A(1,3) = -0.07;
10        A(3,2) = 0.0001;
11
12        RNG_randomize();
13       
14        mat U=eye(5);
15        for (int16 i=0; i<5;i++) {
16                for (int16 j=i+1; j<5;j++) U(i,j)=2*randu(1)(0)-1;
17        }
18        mat Q = diag(vec(" 0.2000 0.3000 0.4000 0.5000 0.6"));
19        vec R = vec(" 0.2000 0.3000");
20       
21                vec D = randu(5)*0.4;
22                vec xref = ones(5);
23
24                int16 PSI[25];
25                int16 PSIU[25]={0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0};
26                int16 Uf[25]={0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0};
27                int16 Gf[25]={0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0, 0,0,0,0,0};
28                int16 Df[5]={0,0,0,0,0};
29                int16 Dfold[5]={0,0,0,0,0};
30               
31                int16 multip=1<<15;
32               
33        /////////// COPY
34        imat Af=round_i(A*multip);
35        mat_to_int16(Af, PSI);
36        mat_to_int16(round_i(U*multip),Uf);             
37        vec_to_int16(round_i(D*multip), Df); 
38        int16 Qf[25];
39        mat_to_int16(round_i(Q*multip), Qf);
40        int16 Rf[2];
41        vec_to_int16(round_i(R*multip), Rf);
42       
43       
44        ////////////// Test mmultAU
45        mmultAU(PSI,Uf,PSIU,5,5);       
46       
47        mat PhiU =A*U;
48        cout << "A*U" << round_i(PhiU*multip) <<endl;
49        cout << "PSIU: "; for (i=0; i<25;i++) cout << PSIU[i] << ","; cout <<endl;
50       
51        Mat<int16> PUcmp(PSIU,5,5);
52        imat PUcmpi=PUcmp;
53        cout << "Delta PSI: " << round_i(PhiU*multip-(1<<(15-qAU))*PUcmp) <<endl;
54       
55        mat_to_int16(round_i(PhiU*multip/(1<<(15-qAU))),PSIU); //<< make is same
56
57/////////// Test Thorton:
58        int16 dim=5;double sigma; int16 j,k; 
59        vec Din = D;
60        mat G=eye(5);
61        for (i=dim-1; i>=0;i--){
62                sigma = 0.0; 
63                for (j=0; j<dim; j++) {
64                        sigma += PhiU(i,j)*PhiU(i,j) *Din(j); 
65                        sigma += G(i,j)*G(i,j) * Q(j,j); 
66                }
67                D(i) = sigma; 
68                for (j=0;j<i;j++){ 
69                        sigma = 0.0; 
70                        for (k=0;k<dim;k++){ 
71                                sigma += PhiU(i,k)*Din(k)*PhiU(j,k); 
72                        }
73                        for (k=0;k<dim;k++){ 
74                                sigma += G(i,k)*Q(k,k)*G(j,k); 
75                        }
76                        //
77                        U(j,i) = sigma/D(i); 
78                        for (k=0;k<dim;k++){ 
79                                PhiU(j,k) = PhiU(j,k) - U(j,i)*PhiU(i,k); 
80                        }
81                        for (k=0;k<dim;k++){ 
82                                G(j,k) = G(j,k) - U(j,i)*G(i,k); 
83                        }
84                }
85        }
86       
87       
88        //thorton_fast(Uf,Df,PSIU,Qf,Gf,Dfold,5);
89        thorton(Uf,Df,PSIU,Qf,Gf,Dfold,5);
90
91        /////// disp
92       
93        cout << endl<<"after thorton " <<endl;
94        cout << "U: " << round_i(U*multip) << endl;
95        cout << "D: " << round_i(D*multip) << endl;
96        cout << "Uf: "; for (i=0; i<25;i++) {cout << Uf[i] << ","; ((i+1)%5==0)? cout << endl:cout<<""; }
97        cout << "Df: "; for (i=0; i<5;i++) cout << Df[i] << ","; cout << endl;
98
99        Mat<int16> Ucmp(Uf,5,5);
100        Vec<int16> Dcmp(Df,5);
101        cout << "Delta U: " << round_i(U*multip-Ucmp) << endl;
102        cout << "Delat D: " << round_i(D*multip-Dcmp) << endl;
103       
104        // synchronize
105        mat_to_int16(round_i(U*multip),Uf);             
106        vec_to_int16(round_i(D*multip), Df); 
107       
108
109        vec     ydif = 2*randu(2)-1;
110        vec     xp = 2*randu(5)-1;
111       
112        int16 difz[2];
113        vec_to_int16(round_i(ydif*multip), difz);
114        int16 xf[5];
115        vec_to_int16(round_i(xp*multip), xf);
116
117        cout << "x: "<< round_i(xp*multip) <<endl;
118        cout << "xf: "; for (i=0; i<5;i++) cout << xf[i] << ","; cout << endl;
119       
120       
121        int16 xf_old[5];
122        for (i=0;i<5;i++) xf_old[i]=xf[i];
123       
124        /////// Test bierman
125        double dz,alpha,gamma,beta,lambda;
126        vec a;
127        vec b;
128        mat C = zeros(2,5);
129        C(0,0)=.05;C(0,1)=-.01;
130        C(1,0)=0.; C(1,1) = .07;
131       
132        int16 Cf[10];
133        imat iC=round_i(C*multip);
134        mat_to_int16(iC, Cf);
135       
136       
137        for (int16 iy=0; iy<2; iy++){
138                a     = U.T()*C.get_row(iy);    // a is not modified, but
139                b     = elem_mult(D,a);                          // b is modified to become unscaled Kalman gain.
140                dz    = ydif(iy); 
141               
142                alpha = R(iy); 
143                gamma = 1/alpha; 
144                for (j=0;j<dim;j++){
145                        beta   = alpha; 
146                        alpha  = alpha + a(j)*b(j); 
147                        lambda = -a(j)*gamma; 
148                        gamma  = 1.0/alpha; 
149                        D(j) = beta*gamma*D(j); 
150                       
151                        //                      cout << "a: " << alpha << "g: " << gamma << endl;
152                        for (i=0;i<j;i++){
153                                beta   = U(i,j); 
154                                U(i,j) = beta + b(i)*lambda; 
155                                b(i)   = b(i) + b(j)*beta; 
156                        }
157                }
158                double dzs = gamma*dz;  // apply scaling to innovations
159                xp   = xp + dzs*b; // multiply by unscaled Kalman gain
160               
161                //cout << "Ub: " << U << endl;
162                //cout << "Db: " << D << endl <<endl;
163               
164        }
165       
166        bierman_fastC(difz,xf, Uf, Df, Cf, Rf, 2, 5);
167        cout << endl<<"after Bierman" <<endl;
168/*      cout << "U: " << round_i(U*multip) << endl;
169        cout << "D: " << round_i(D*multip) << endl;
170        cout << "Uf: "; for (i=0; i<25;i++) {cout << Uf[i] << ","; ((i+1)%5==0)? cout << endl:cout<<""; }
171        cout << "Df: "; for (i=0; i<5;i++) cout << Df[i] << ","; cout << endl;*/
172        cout << "x: "<< round_i(xp*multip) <<endl;
173        cout << "xf: "; for (i=0; i<5;i++) cout << xf[i] << ","; cout << endl;
174
175        {
176        Mat<int16> Ucmp(Uf,5,5);
177        Vec<int16> Dcmp(Df,5);
178        cout << "Delta U: " << round_i(U*multip-Ucmp) << endl;
179        cout << "Delat D: " << round_i(D*multip-Dcmp) << endl;
180        }
181       
182return 0;       
183}
Note: See TracBrowser for help on using the browser.