Changeset 1230
- Timestamp:
- 10/25/10 09:41:23 (14 years ago)
- Location:
- applications/pmsm/simulator_zdenek
- Files:
-
- 4 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/pmsm/simulator_zdenek/ekf_example/ekf_obj.cpp
r1229 r1230 445 445 mmultAU(PSI,Uf,PSIU,4,4); 446 446 //thorton(int *U, int *D, int *PSIU, int *Q, int *G, int *Dold, unsigned int dimx); 447 thorton (Uf,Df,PSIU,Q,G,Dfold,4);447 thorton_fast(Uf,Df,PSIU,Q,G,Dfold,4); 448 448 449 449 { … … 469 469 int dR[2];dR[0]=R[0];dR[1]=R[3]; 470 470 //int xb[4]; xb[0]=x_est[0]<<2; xb[1]=x_est[1]<<2; xb[2]=x_est[2]<<2; xb[3]=x_est[3]; 471 bierman (difz,x_est,Uf,Df,dR,2,4);471 bierman_fast(difz,x_est,Uf,Df,dR,2,4); 472 472 //x_est[0] = xb[0]>>2; x_est[1]=xb[1]>>2; x_est[2]=xb[2]>>2; x_est[3]=xb[3]; 473 473 … … 487 487 { 488 488 // Tuning of matrix Q 489 Q[0]=prevod(.00 05,15); // 0.05489 Q[0]=prevod(.001,15); // 0.05 490 490 Q[5]=Q[0]; 491 Q[10]=prevod(0.00 01,15); // 1e-3491 Q[10]=prevod(0.001,15); // 1e-3 492 492 Q[15]=prevod(0.0001,15); // 1e-3 493 493 … … 585 585 //thorton(int *U, int *D, int *PSIU, int *Q, int *G, int *Dold, unsigned int dimx); 586 586 householder(PSICh,Q,4); 587 // COPY 588 for (int ii=0; ii<16; ii++){Chf[ii]=PSICh[ii];} 587 589 588 590 { … … 600 602 int dR[2];dR[0]=R[0];dR[1]=R[3]; 601 603 //int xb[4]; xb[0]=x_est[0]<<2; xb[1]=x_est[1]<<2; xb[2]=x_est[2]<<2; xb[3]=x_est[3]; 604 602 605 carlson(difz,x_est,Chf,dR,2,4); 603 606 //x_est[0] = xb[0]>>2; x_est[1]=xb[1]>>2; x_est[2]=xb[2]>>2; x_est[3]=xb[3]; … … 618 621 { 619 622 // Tuning of matrix Q 620 Q[0]=prevod(.0 1,15); // 0.05623 Q[0]=prevod(.001,15); // 0.05 621 624 Q[5]=Q[0]; 622 Q[10]=prevod(0.00 01,15); // 1e-3625 Q[10]=prevod(0.001,15); // 1e-3 623 626 Q[15]=prevod(0.0001,15); // 1e-3 624 627 625 Chf[0]=0x 7FFF;// ! // 0.05628 Chf[0]=0x3FFF;// ! // 0.05 626 629 Chf[1]=Chf[2]=Chf[3]=Chf[4]=0; 627 Chf[5]=0x 7FFF;//!630 Chf[5]=0x3FFF;//! 628 631 Chf[6]=Chf[6]=Chf[8]=Chf[9]=0; 629 Chf[10]=0x 7FFF;//! // 1e-3632 Chf[10]=0x3FFF;//! // 1e-3 630 633 Chf[11]=Chf[12]=Chf[13]=Chf[4]=0; 631 Chf[15]=0x 7FFF; // 1e-3634 Chf[15]=0x3FFF; // 1e-3 632 635 633 636 // Tuning of matrix R -
applications/pmsm/simulator_zdenek/ekf_example/matrix_vs.cpp
r1228 r1230 136 136 for (j=0;j<rows; j++) { 137 137 //long s1=(((long)PSIU[i+j*rows]*PSIU[i+j*rows])>>15)*(Dold[i]); 138 long s2=((((long)PSIU[i*rows+j]* PSIU[i*rows+j]))>>(2*qAU-15))*Dold[j];138 long s2=((((long)PSIU[i*rows+j]*Dold[j]))>>(2*qAU-15))*PSIU[i*rows+j]; 139 139 // printf("%d - %d\n",s1,s2); 140 140 sigma += s2; 141 141 } 142 sigma += Q[i*rows+i]<<15; 142 sigma += Q[i*rows+i]<<15; // Q is in q15 143 143 for (j=i+1;j<rows; j++) { 144 144 sigma += (((long)G[i*rows+j]*G[i*rows+j])>>13)*Q[j*rows+j]; … … 146 146 } 147 147 148 //if (sigma>16384<<15) sigma = 16384<<15;148 if (sigma>16384<<15) sigma = 16384<<15; 149 149 *(D+i)=sigma>>15; 150 150 if (D[i]==0) D[i]=1; … … 155 155 sigma =0; 156 156 for (k=0;k<rows;k++) { 157 sigma += (((long(PSIU[i*rows+k])* PSIU[j*rows+k]))>>(2*qAU-15))*Dold[k];157 sigma += (((long(PSIU[i*rows+k])*Dold[k]))>>(2*qAU-15))*PSIU[j*rows+k]; 158 158 } 159 159 for (k=0;k<rows;k++) { … … 266 266 for (k=0, PSIU_ik=PSIU+irows,Dold_k=Dold; 267 267 k<rows; k++, PSIU_ik++,Dold_k++) {//Dold_i= 268 sigma += (((long)(*PSIU_ik)**PSIU_ik)>>15)*(*Dold_k);268 sigma += (((long)(*PSIU_ik)**Dold_k)>>(2*qAU-15))*(*PSIU_ik); 269 269 } 270 270 sigma += *(Q+i+irows)<<15; … … 273 273 274 274 } 275 275 276 if (sigma>16384<<15) sigma = 16384<<15; 276 277 *D_i=sigma>>15; 277 278 if (*D_i==0) *D_i=1; … … 285 286 k<rows; k++, PSIU_ik++, PSIU_jk++, Dold_k++) { 286 287 287 sigma += ((((long)*PSIU_ik)**PSIU_jk)>>15)**Dold_k;288 sigma += ((((long)*PSIU_ik)**Dold_k)>>(2*qAU-15))**PSIU_jk; 288 289 } 289 290 … … 364 365 /* square root of 0<a<1 using taylor at 0.5 in q15*/ 365 366 int int_sqrt(int x) { 367 double xd(double(x)/32768.); 368 return round(sqrt(xd)*32768); 369 366 370 //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 371 // = k1 + k1*(x-0.5) - k2*(x-0.5)(x-0.5); … … 404 408 /* double sigf=double(sigma)/(1<<15); 405 409 double alpf = sqrt(sigf);*/ 410 if (sigma>16384) sigma=16384; 406 411 alpha=int_sqrt(sigma); 407 412 // alpha = alpf*(1<<15); … … 421 426 } 422 427 alpha=sigma>>1; 428 if (alpha==0) alpha =1; 423 429 for (i=0;i<=k;i++) { 424 430 sigma=0; … … 474 480 } 475 481 } 482 483 /* perform Householder update of Ch matrix using PSI*Ch , Q, */ 484 extern void givens(int *Ch /*= int *PSICh*/, int *Q, unsigned int dimx){ 485 int i,j,k; 486 int rho,s,c,tau; 487 488 int A[25];//beware 489 // copy Q to A 490 for (i=0;i<dimx*dimx;i++) { 491 A[i]=Q[i]; 492 } 493 494 495 for (i=dimx-1; i>=0; i--){ 496 for (j=0; j<dimx; j++) { 497 rho=int_sqrt(((long)Ch[i*dimx+i]*Ch[i*dimx+i]+long(A[i*dimx+j])*A[i*dimx+j])>>15); 498 if (rho==0) break; 499 s=(long(A[i*dimx+j])<<15)/rho; 500 c=(long(Ch[i*dimx+i])<<15)/rho; 501 for (k=0;k<=i; k++){ 502 tau=(long(c)*A[k*dimx+j]-long(s)*Ch[k*dimx+i])>>15; 503 Ch[k*dimx +i]=(long(s)*A[k*dimx+j]+long(c)*Ch[k*dimx+i])>>15; 504 A[k*dimx +j]=tau; 505 } 506 } 507 } 508 for (j=0; j<i; j++){ 509 rho=int_sqrt((long(Ch[i*dimx+i])*Ch[i*dimx+i]+long(Ch[i*dimx+j])*Ch[i*dimx+j])>>15); 510 if (rho==0) break; 511 s=(long(Ch[i*dimx+j])<<15)/rho; 512 c=(long(Ch[i*dimx+i])<<15)/rho; 513 for (k=0; k<=i; k++){ 514 tau=(long(c)*Ch[k*dimx+j]-long(s)*Ch[k*dimx+i])>>15; 515 Ch[k*dimx+i]=(long(s)*Ch[k*dimx+j]+long(c)*Ch[k*dimx+i])>>15; 516 Ch[k*dimx+j]=tau; 517 } 518 } 519 } -
applications/pmsm/simulator_zdenek/ekf_example/matrix_vs.h
r1228 r1230 34 34 extern void householder(int *Ch /*= int *PSICh*/, int *Q, unsigned int dimx); 35 35 36 /* perform Givens update of Ch matrix using PSI*Ch , Q, */ 37 extern void givens(int *Ch /*= int *PSICh*/, int *Q, unsigned int dimx); 38 36 39 /* perform Carlson update of Ch matrix using difz, R and xp, for size dimx*/ 37 40 extern void carlson(int *difz, int *xp, int *Ch, int *R, unsigned int dimy, unsigned int dimx ); -
applications/pmsm/simulator_zdenek/test_Ch.cpp
r1227 r1230 89 89 90 90 householder(PSICh,Qf,5); 91 // givens(PSICh,Qf,5); 91 92 92 93 /////// disp … … 103 104 mat Ch2 = 0.9*diag(1./sqrt(d))*Ch; 104 105 mat_to_int(round_i(Ch2*multip),Chf); 105 106 107 D = pow(diag(Ch2),2); 108 U = sqrt(diag(1./D)); 109 U = Ch2*U; 110 106 111 vec ydif = 2*randu(2)-1; 107 112 vec xp = 2*randu(5)-1; … … 159 164 160 165 carlson(difz,xf, Chf, Rf, 2, 5); 166 161 167 cout << endl<<"after Carlson" <<endl; 162 168 cout << "x: "<< round_i(xp*multip) <<endl; … … 164 170 165 171 { 166 imat Chcmp(Chf,5,5); 167 cout << "Delat Ch: " << round_i(Ch*multip-Chcmp) << endl; 172 imat Chcmp(Chf,5,5); 173 cout << "Delat Ch: " << round_i(Ch*multip-Chcmp) << endl; 174 cout << "Delat Ch: " << Chcmp << endl; 168 175 } 169 176