Changeset 1225 for applications/pmsm/simulator_zdenek
- Timestamp:
- 10/22/10 21:14:59 (14 years ago)
- Location:
- applications/pmsm/simulator_zdenek
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/pmsm/simulator_zdenek/ekf_example/matrix_vs.cpp
r1197 r1225 12 12 #include "fixed.h" 13 13 #include "stdio.h" 14 #include <math.h> 15 16 #include "matrix_vs.h" 17 14 18 /* Matrix multiply Full matrix by upper diagonal matrix; */ 15 19 void mmultAU(int *m1, int *up, int *result, unsigned int rows, unsigned int columns) { … … 17 21 long tmp_sum=0L; 18 22 int *m2pom; 23 int *m1pom=m1; 24 int *respom=result; 19 25 20 26 for (i=0; i<rows; i++) //rows of result … … 26 32 for (k=0; k<j; k++) //inner loop up to "j" - U(j,j)==1; 27 33 { 28 tmp_sum+=(long)(* m1++)**m2pom;34 tmp_sum+=(long)(*(m1pom++))**m2pom; 29 35 m2pom+=columns; 30 36 } 31 37 // add the missing A(i,j) 32 tmp_sum +=(long)(*m1)<<15; // no need to shift 33 m1-=(j); // shift back to first element 38 tmp_sum +=(long)(*m1pom)<<15; // no need to shift 39 m1pom-=(j); // shift back to first element 40 41 // saturation effect 42 tmp_sum=tmp_sum>>(30-qAU); 43 if (tmp_sum>32767) { 44 //tmp_sum=32767; 45 } 46 if (tmp_sum<-32768) { 47 //tmp_sum=-32768; 48 } 49 // printf("Au - saturated\n"); 50 51 *respom++=tmp_sum; 52 53 tmp_sum=0; 54 } 55 m1pom+=(columns); 56 } 57 }; 58 59 /* Matrix multiply Full matrix by upper diagonal matrix; */ 60 void mmultACh(int *m1, int *up, int *result, unsigned int rows, unsigned int columns) { 61 unsigned int i, j, k; 62 long tmp_sum=0L; 63 int *m2pom; 64 int *m1pom=m1; 65 int *respom=result; 66 67 for (i=0; i<rows; i++) //rows of result 68 { 69 for (j=0; j<columns; j++) //columns of result 70 { 71 m2pom=up+j;//?? 72 73 for (k=0; k<=j; k++) //inner loop up to "j" - U(j,j)==1; 74 { 75 tmp_sum+=(long)(*(m1pom++))**m2pom; 76 m2pom+=columns; 77 } 78 m1pom-=(j+1); // shift back to first element 34 79 35 80 // saturation effect 36 81 tmp_sum=tmp_sum>>15; 37 if (tmp_sum>32767) tmp_sum=32767; 38 if (tmp_sum<-32768) tmp_sum=-32768; 39 40 *result++=tmp_sum; 82 if (tmp_sum>32767) { 83 if (i!=3) tmp_sum=32767; 84 } 85 if (tmp_sum<-32768) { 86 tmp_sum=-32768; 87 } 88 // printf("Au - saturated\n"); 89 90 *respom++=tmp_sum; 41 91 42 92 tmp_sum=0; 43 93 } 44 m1 +=(columns);94 m1pom+=(columns); 45 95 } 46 96 }; … … 49 99 50 100 void show(const char name[10], int *I, int n) { 51 52 53 101 if (!DBG) return; 102 103 printf("%s: ",name); 54 104 for (int i=0;i<n;i++) { 55 56 105 printf("%d ",*(I+i)); 106 } 57 107 printf("\n"); 58 108 } … … 83 133 for (i=rows-1; true;i--) { // check i==0 at the END! 84 134 sigma = 0; 85 135 86 136 for (j=0;j<rows; j++) { 87 88 long s2=(((long)PSIU[i*rows+j]*PSIU[i*rows+j])>>15)*(Dold[j]);137 //long s1=(((long)PSIU[i+j*rows]*PSIU[i+j*rows])>>15)*(Dold[i]); 138 long s2=((((long)PSIU[i*rows+j]*Dold[j])>>qAU)*PSIU[i*rows+j])<<(15-qAU); 89 139 // printf("%d - %d\n",s1,s2); 90 91 92 140 sigma += s2; 141 } 142 sigma += Q[i*rows+i]<<15; 93 143 for (j=i+1;j<rows; j++) { 94 144 sigma += (((long)G[i*rows+j]*G[i*rows+j])>>13)*Q[j*rows+j]; 95 145 // sigma += (((long)G[i+j*rows]*G[i+j*rows])>>13)*Q[j+j*rows]; 96 } 97 146 } 147 148 //if (sigma>16384<<15) sigma = 16384<<15; 98 149 *(D+i)=sigma>>15; 99 150 if (D[i]==0) D[i]=1; … … 104 155 sigma =0; 105 156 for (k=0;k<rows;k++) { 106 sigma += ((( (long)PSIU[i*rows+k])*PSIU[j*rows+k])>>15)*Dold[k];107 108 157 sigma += (((long(PSIU[i*rows+k])*Dold[k])>>qAU)*PSIU[j*rows+k])<<(15-qAU); 158 } 159 for (k=0;k<rows;k++) { 109 160 sigma += ((((long)G[i*rows+k])*G[j*rows+k])>>13)*Q[k*rows+k]; 110 161 } 111 162 long z=sigma/D[i]; // shift by 15 112 163 if (z>32767) z=32767; 113 164 if (z<-32768) z=-32768; … … 117 168 118 169 for (k=0;k<rows;k++) { 119 PSIU[j*rows+k] -= ((long)U[j*rows+i]*PSIU[i*rows+k])>>15; 120 } 121 170 PSIU[j*rows+k] -= ((long)U[j*rows+i]*PSIU[i*rows+k])>>15; //qAU*q15/q15=qAU 171 } 172 122 173 for (k=0;k<rows;k++) { 123 174 G[j*rows+k] -= ((long)U[j*rows+i]*G[i*rows+k])>>15; 124 175 } 125 176 126 177 } 127 178 //show("U",U,25); 128 129 179 //show("G",G,25); 180 if (i==0) return; 130 181 } 131 182 } … … 214 265 sigma = 0; 215 266 for (k=0, PSIU_ik=PSIU+irows,Dold_k=Dold; 216 k<rows; k++, PSIU_ik++,Dold_k++) {//Dold_i=217 sigma += (((long)(*PSIU_ik)**PSIU_ik)>> 15)*(*Dold_k);218 219 267 k<rows; k++, PSIU_ik++,Dold_k++) {//Dold_i= 268 sigma += (((long)(*PSIU_ik)**PSIU_ik)>>(qAU*qAU-15))*(*Dold_k); 269 } 270 sigma += *(Q+i+irows)<<15; 220 271 for (j=i+1, G_ik=G+irows+i+1; j<rows; j++,G_ik++) { 221 272 sigma += (((long)(*G_ik)**G_ik)>>13)**(Q+j+j*rows); 222 223 } 224 273 274 } 275 225 276 *D_i=sigma>>15; 226 277 if (*D_i==0) *D_i=1; … … 233 284 for (k=0, PSIU_ik=PSIU+irows, PSIU_jk=PSIU+jrows, Dold_k=Dold; 234 285 k<rows; k++, PSIU_ik++, PSIU_jk++, Dold_k++) { 235 236 sigma += ((((long)*PSIU_ik)**PSIU_jk)>> 15)**Dold_k;237 } 238 286 287 sigma += ((((long)*PSIU_ik)**PSIU_jk)>>(qAU*qAU-15))**Dold_k; 288 } 289 239 290 for (k=i,G_ik=G+irows+i,G_jk=G+jrows+i,Q_kk=Q+k*rows+k; 240 k<rows;k++,G_ik++,G_jk++,Q_kk+=rows+1) {291 k<rows;k++,G_ik++,G_jk++,Q_kk+=rows+1) { 241 292 sigma += ((((long)*G_ik)**G_jk)>>13)**Q_kk; 242 293 } 243 244 294 295 long z=sigma/(*D_i); // shift by 15 245 296 if (z>32767) z=32767; 246 297 if (z<-32768) z=-32768; … … 254 305 *PSIU_jk -= ((long)*U_ji**PSIU_ik)>>15; 255 306 } 256 307 257 308 for (k=0,G_jk=G+jrows,G_ik=G+irows; 258 k<rows;k++, G_jk++, G_ik++) {309 k<rows;k++, G_jk++, G_ik++) { 259 310 *G_jk -= ((long)*U_ji**G_ik)>>15; 260 311 } 261 312 262 313 } 263 314 if (i==0) return; … … 275 326 // a is a row 276 327 a = U+iy*dimx; // iyth row of U 277 for (j=0;j<dimx;j++) 278 b[j]=((long)D[j]*a[j])>>15; 328 for (j=0;j<dimx;j++) { 329 (j<iy)? b[j]=0: (j==iy)? b[j]=D[j] : b[j]=((long)D[j]*a[j])>>15; 330 } 279 331 280 332 alpha = (long)R[iy]; //\alpha = R+vDv = R+a*b … … 285 337 for (j=0;j<dimx;j++) { 286 338 beta = alpha; 287 lambda = -(( long)a[j]<<15)/beta;288 alpha += (( long)(a[j])*b[j]>>15);289 D[j] = ((( long)beta<<15)/alpha)*D[j]>>15; //gamma is long339 lambda = -(((long)a[j])<<15)/beta; 340 alpha += (((long)(a[j])*b[j])>>15); 341 D[j] = (((((long)beta)<<15)/alpha)*D[j])>>15; //gamma is long 290 342 if (D[j]==0) D[j]=1; 291 343 … … 309 361 310 362 } 363 364 /* square root of 0<a<1 using taylor at 0.5 in q15*/ 365 int int_sqrt(int x) { 366 //sqrt(x) == 1/2*2^(1/2)+1/2*2^(1/2)*(x-1/2)-1/4*2^(1/2)*(x-1/2)^2 367 // = k1 + k1*(x-0.5) - k2*(x-0.5)(x-0.5); 368 #define k1 23170 //0.5*sqrt(2)*32768 369 #define k2 11585 //0.25*sqrt(2)*32768 370 371 int tmp; 372 if (x>6554) { 373 int xm05=x-16384; 374 tmp = ((long)k1*xm05)>>15; 375 tmp-=(((long(k2)*xm05)>>15)*xm05)>>15; 376 tmp +=k1; 377 } else { 378 tmp = 4*x; 379 tmp-=long(8*x)*x>>15; 380 } 381 return tmp; 382 } 383 384 void householder(int *Ch /*= int *PSICh*/, int *Q, unsigned int dimx) { 385 int k,j,i; 386 int sigma,alpha,beta; 387 int B[25];//beware 388 int w[5]; 389 int v[5]; 390 391 // copy Q to B 392 for (i=0;i<dimx*dimx;i++) { 393 B[i]=Q[i]; 394 } 395 396 for (k=dimx-1; k>=0; k--) { 397 sigma=0; 398 for (j=0;j<dimx;j++) { 399 sigma+=(long(B[k*dimx+j])*B[k*dimx+j])>>15; 400 } 401 for (j=0;j<=k;j++) { 402 sigma+=(long(Ch[k*dimx+j])*Ch[k*dimx+j])>>15; 403 } 404 /* double sigf=double(sigma)/(1<<15); 405 double alpf = sqrt(sigf);*/ 406 alpha=int_sqrt(sigma); 407 // alpha = alpf*(1<<15); 408 // 409 sigma=0; 410 for (j=0;j<dimx;j++) { 411 w[j]=B[k*dimx+j]; 412 sigma+=(long(w[j])*w[j])>>15; 413 } 414 for (j=0; j<=k;j++) { 415 if (j==k) { 416 v[j]=Ch[k*dimx+j]-alpha; 417 } else { 418 v[j]=Ch[k*dimx+j]; 419 } 420 sigma+=(long(v[j])*v[j])>>15; 421 } 422 alpha=sigma>>1; 423 for (i=0;i<=k;i++) { 424 sigma=0; 425 for (j=0;j<dimx;j++) { 426 sigma+=(long(B[i*dimx+j])*w[j])>>15; 427 } 428 for (j=0;j<=k;j++) { 429 sigma+=(long(Ch[i*dimx+j])*v[j])>>15; 430 } 431 for (j=0;j<dimx;j++) { 432 B[i*dimx+j]-=(long(sigma)*w[j]/alpha); 433 }; 434 for (j=0;j<=k;j++) { 435 Ch[i*dimx+j]-=(long(sigma)*v[j]/alpha); 436 } 437 } 438 } 439 440 } 441 442 void carlson(int *difz, int *xp, int *Ch, int *R, unsigned int dimy, unsigned int dimx ) { 443 int alpha,beta,gamma; 444 int delta, eta,epsilon,zeta,sigma,tau; 445 int i,j,iy; 446 int w[5]; 447 448 for (iy=0; iy<dimy; iy++) { 449 alpha=R[iy]; 450 delta = difz[iy]; 451 452 for (j=0;j<dimx;j++) { 453 sigma=Ch[iy*dimx+j]; 454 beta=alpha; 455 alpha+=(long(sigma)*sigma)>>15; 456 // double ab=(double)alpha*beta/32768./32768.; 457 // double s_ab=sqrt(ab); 458 gamma=int_sqrt(((long)alpha*beta)>>15); 459 //gamma = round(s_ab*(1<<15)); 460 eta=(long (beta)<<15) / gamma; 461 //zeta=(long(sigma)<<15)/ gamma; 462 w[j]=0; 463 for (i=0;i<=j;i++) { 464 tau=Ch[i*dimx+j]; 465 Ch[i*dimx+j]=((long(eta)*Ch[i*dimx+j])>>15) -(long(sigma)*w[i])/gamma; 466 w[i]+=(long(tau)*sigma)>>15; 467 } 468 } 469 470 //epsilon=(long(difz)<<15) / (alpha); // q15*q13/q13 = q15 471 for (i=0;i<dimx;i++) { 472 xp[i]+=(long(w[i])*delta)/alpha; 473 } 474 } 475 } -
applications/pmsm/simulator_zdenek/ekf_example/matrix_vs.h
r1179 r1225 10 10 11 11 *************************************/ 12 13 #define qAU 15 14 15 /* Matrix multiply Full matrix by upper diagonal matrix with unit diagonal; */ 16 extern void mmultAU(int *m1, int *up, int *result, unsigned int rows, unsigned int columns); 17 12 18 /* Matrix multiply Full matrix by upper diagonal matrix; */ 13 extern void mmultA U(int *m1, int *up, int *result, unsigned int rows, unsigned int columns);19 extern void mmultACh(int *m1, int *up, int *result, unsigned int rows, unsigned int columns); 14 20 15 21 /* perform Thorton update of UD matrix using PSI*U, Q, and temporaries G, Dold, for size dimx*/ … … 24 30 /* perform Bierman update of UD matrix using difz, R and xp, for size dimx*/ 25 31 extern void bierman_fast(int *difz, int *xp, int *U, int *D, int *R, unsigned int dimy, unsigned int dimx ); 32 33 /* perform Householder update of Ch matrix using PSI*Ch , Q, */ 34 extern void householder(int *Ch /*= int *PSICh*/, int *Q, unsigned int dimx); 35 36 /* perform Carlson update of Ch matrix using difz, R and xp, for size dimx*/ 37 extern void carlson(int *difz, int *xp, int *Ch, int *R, unsigned int dimy, unsigned int dimx ); -
applications/pmsm/simulator_zdenek/test_UD.cpp
r1202 r1225 50 50 51 51 imat PUcmp(PSIU,5,5); 52 cout << "Delta PSI: " << round_i(PhiU*multip- PUcmp) <<endl;52 cout << "Delta PSI: " << round_i(PhiU*multip-2*PUcmp) <<endl; 53 53 54 mat_to_int(round_i(PhiU*multip ),PSIU); //<< make is same54 mat_to_int(round_i(PhiU*multip/2),PSIU); //<< make is same 55 55 56 56 /////////// Test Thorton: … … 85 85 86 86 87 thorton_fast(Uf,Df,PSIU,Qf,Gf,Dfold,5); 87 //thorton_fast(Uf,Df,PSIU,Qf,Gf,Dfold,5); 88 thorton(Uf,Df,PSIU,Qf,Gf,Dfold,5); 88 89 89 90 /////// disp