root/applications/pmsm/simulator_zdenek/test_Ch.cpp @ 1321

Revision 1321, 4.1 kB (checked in by smidl, 13 years ago)

Carlson fast with C + test

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