Changeset 1241
- Timestamp:
- 10/29/10 19:10:12 (14 years ago)
- Location:
- applications/pmsm/simulator_zdenek
- Files:
-
- 4 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/pmsm/simulator_zdenek/ekf_example/ekf_obj.cpp
r1240 r1241 604 604 //thorton(int16 *U, int16 *D, int16 *PSIU, int16 *Q, int16 *G, int16 *Dold, unsigned int16 dimx); 605 605 householder(PSICh,Q,4); 606 //givens(PSICh,Q,4); 606 607 // COPY 607 608 for (int16 ii=0; ii<16; ii++){Chf[ii]=PSICh[ii];} 608 609 609 610 { 610 /* ivec Chd(Chf,16); 611 log_level.store(logCh,get_from_ivec(Chd));*/ 611 612 int Chi[16]; for (int i=0;i<16;i++) Chi[i]=Chf[i]; 613 ivec Chd(Chi,16); 614 log_level.store(logCh,get_from_ivec(Chd)); 615 imat mCh(Chd._data(), 4,4); 616 imat P = mCh*mCh.T(); 617 ivec iP(P._data(),16); 618 log_level.store(logP,get_from_ivec(iP)); 612 619 } 613 620 … … 640 647 { 641 648 // Tuning of matrix Q 642 Q[0]=prevod(.0 01,15); // 0.05649 Q[0]=prevod(.01,15); // 0.05 643 650 Q[5]=Q[0]; 644 Q[10]=prevod(0.0 01,15); // 1e-3645 Q[15]=prevod(0.0 001,15); // 1e-3651 Q[10]=prevod(0.01,15); // 1e-3 652 Q[15]=prevod(0.01,15); // 1e-3 646 653 647 654 Chf[0]=0x3FFF;// ! // 0.05 -
applications/pmsm/simulator_zdenek/ekf_example/matrix_vs.cpp
r1240 r1241 16 16 17 17 #include "matrix_vs.h" 18 #include <math.h> 18 19 19 20 /* Matrix multiply Full matrix by upper diagonal matrix; */ … … 204 205 205 206 /* square root of 0<a<1 using taylor at 0.5 in q15*/ 206 int int_sqrt(intx) {207 int16 int_sqrt(int16 x) { 207 208 double xd(double(x)/32768.); 208 209 return round(sqrt(xd)*32768); … … 213 214 #define k2 11585 //0.25*sqrt(2)*32768 214 215 215 int tmp;216 int16 tmp; 216 217 if (x>6554) { 217 int xm05=x-16384;218 int16 xm05=x-16384; 218 219 tmp = ((long)k1*xm05)>>15; 219 220 tmp-=(((long(k2)*xm05)>>15)*xm05)>>15; … … 226 227 } 227 228 228 void householder(int *Ch /*= int *PSICh*/, int *Q, unsigned int dimx) { 229 int k,j,i; 230 int alpha,beta; 231 long sigma; 232 int B[25];//beware 233 int w[5]; 234 int v[5]; 229 void householder(int16 *Ch /*= int16 *PSICh*/, int16 *Q, unsigned int16 dimx) { 230 int16 k,j,i; 231 int16 alpha,beta; 232 int32 sigma; // 2*qCh 233 int32 tmp_long; 234 int16 B[25];//Q in qCh 235 int16 w[5]; 236 int16 v[5]; 235 237 236 238 // copy Q to B 237 239 for (i=0;i<dimx*dimx;i++) 238 240 { 239 B[i]=Q[i] ;241 B[i]=Q[i]>>(15-qCh); 240 242 } 241 243 … … 245 247 for (j=0;j<dimx;j++) 246 248 { 247 sigma+=( long)B[k*dimx+j]*B[k*dimx+j];249 sigma+=((long)B[k*dimx+j]*B[k*dimx+j]); 248 250 } 249 251 for (j=0;j<=k;j++) 250 252 { 251 sigma+=(long)Ch[k*dimx+j]*Ch[k*dimx+j]; 252 } 253 /* double sigf=double(sigma)/(1<<15); 254 double alpf = sqrt(sigf);*/ 255 // if (sigma>16384) sigma=16384; 256 // alpha=int_sqrt(sigma); 257 alpha = (int)(sqrt((double)sigma)+0.5); // predelat pro DSP 258 // alpha = alpf*(1<<15); 259 // 260 sigma=0; 253 sigma+=((long)Ch[k*dimx+j]*Ch[k*dimx+j]); 254 } 255 256 //alpha in qCh 257 alpha = (int)(sqrt((double)sigma)+0.5); // predelat pro DSP 258 259 sigma=0; 261 260 for (j=0;j<dimx;j++) { 262 261 w[j]=B[k*dimx+j]; 263 sigma+=(long)w[j] )*w[j];262 sigma+=(long)w[j]*w[j]; 264 263 } 265 264 for (j=0; j<=k;j++) { … … 272 271 } 273 272 274 alpha=sigma>> 16;273 alpha=sigma>>(qCh+1); // alpha = sigma /2; 275 274 if (alpha==0) alpha =1; 276 275 … … 278 277 sigma=0; 279 278 for (j=0;j<dimx;j++) { 280 sigma+=( long)B[i*dimx+j]*w[j];279 sigma+=((long)B[i*dimx+j]*w[j]); 281 280 } 282 281 for (j=0;j<=k;j++) { … … 285 284 286 285 sigma = sigma >> 15; // navrat do Q15 287 286 if (sigma>32767)sigma=32767; 287 288 288 for (j=0;j<dimx;j++) 289 289 { 290 B[i*dimx+j]-=(sigma*w[j])/alpha; 290 tmp_long=B[i*dimx+j]-(sigma*w[j])/alpha; 291 if (tmp_long>32767) { 292 B[i*dimx+j]=32767; 293 } else { 294 if (tmp_long<-32767){ 295 B[i*dimx+j]=-32767; 296 } else { 297 B[i*dimx+j]=tmp_long; 298 } 299 } 291 300 }; 292 301 293 302 for (j=0;j<=k;j++) 294 303 { 295 Ch[i*dimx+j]-=(sigma*v[j])/alpha; 296 } 297 } 298 } 299 300 } 301 302 void carlson(int *difz, int *xp, int *Ch, int *R, unsigned int dimy, unsigned int dimx ) { 303 int alpha,beta,gamma; 304 int delta, eta,epsilon,zeta,sigma,tau; 305 int i,j,iy; 306 int w[5]; 304 tmp_long=Ch[i*dimx+j]-(sigma*v[j])/alpha; 305 if (tmp_long>32767) { 306 Ch[i*dimx+j]=32767; 307 } else { 308 if (tmp_long<-32767){ 309 Ch[i*dimx+j]=-32767; 310 } else { 311 Ch[i*dimx+j]=tmp_long; 312 } 313 } 314 } 315 } 316 } 317 318 } 319 320 void carlson(int16 *difz, int16 *xp, int16 *Ch, int16 *R, unsigned int16 dimy, unsigned int16 dimx ) { 321 int16 alpha,beta,gamma; 322 int16 delta, eta,epsilon,zeta,sigma,tau; 323 int16 i,j,iy; 324 int16 w[5]; 325 int32 tmp_long; 307 326 308 327 for (iy=0; iy<dimy; iy++) … … 325 344 for (i=0;i<=j;i++) { 326 345 tau=Ch[i*dimx+j]; 327 Ch[i*dimx+j]=((long)beta*Ch[i*dimx+j] -(long)sigma*w[i])/gamma; 346 tmp_long=((long)beta*Ch[i*dimx+j] -(long)sigma*w[i])/gamma; 347 if (tmp_long>32767) { 348 Ch[i*dimx+j]=32767; 349 } else { 350 if (tmp_long<-32767){ 351 Ch[i*dimx+j]=-32767; 352 } else { 353 Ch[i*dimx+j]=tmp_long; 354 } 355 } 356 328 357 w[i]+=((long)tau*sigma)>>15; 329 358 } … … 338 367 339 368 /* perform Householder update of Ch matrix using PSI*Ch , Q, */ 340 extern void givens(int *Ch /*= int *PSICh*/, int *Q, unsigned int dimx){ 341 int i,j,k; 342 int rho,s,c,tau; 343 344 int A[25];//beware 369 extern void givens(int16 *Ch /*= int16 *PSICh*/, int16 *Q, unsigned int16 dimx){ 370 int16 i,j,k; 371 int16 rho,s,c,tau; 372 int32 tmp_long; 373 374 int16 A[25];//beware 345 375 // copy Q to A 346 376 for (i=0;i<dimx*dimx;i++) { 347 A[i]=Q[i] ;377 A[i]=Q[i]>>(15-qCh); 348 378 } 349 379 … … 351 381 for (i=dimx-1; i>=0; i--){ 352 382 for (j=0; j<dimx; j++) { 353 rho=int_sqrt(((long)Ch[i*dimx+i]*Ch[i*dimx+i]+long(A[i*dimx+j])*A[i*dimx+j])>>15); 354 if (rho==0) break; 355 s=(long(A[i*dimx+j])<<15)/rho; 356 c=(long(Ch[i*dimx+i])<<15)/rho; 357 for (k=0;k<=i; k++){ 358 tau=(long(c)*A[k*dimx+j]-long(s)*Ch[k*dimx+i])>>15; 359 Ch[k*dimx +i]=(long(s)*A[k*dimx+j]+long(c)*Ch[k*dimx+i])>>15; 360 A[k*dimx +j]=tau; 383 tmp_long=(long)Ch[i*dimx+i]*Ch[i*dimx+i]+long(A[i*dimx+j])*A[i*dimx+j]; 384 if (tmp_long>0){ 385 rho=sqrt(double(tmp_long)); 386 s=(long(A[i*dimx+j])<<15)/rho; 387 c=(long(Ch[i*dimx+i])<<15)/rho; 388 for (k=0;k<=i; k++){ 389 tau=(long(c)*A[k*dimx+j]-long(s)*Ch[k*dimx+i])>>15; 390 Ch[k*dimx +i]=(long(s)*A[k*dimx+j]+long(c)*Ch[k*dimx+i])>>15; 391 A[k*dimx +j]=tau; 392 } 361 393 } 362 394 } 363 395 } 364 396 for (j=0; j<i; j++){ 365 rho=int_sqrt((long(Ch[i*dimx+i])*Ch[i*dimx+i]+long(Ch[i*dimx+j])*Ch[i*dimx+j])>>15); 366 if (rho==0) break; 367 s=(long(Ch[i*dimx+j])<<15)/rho; 368 c=(long(Ch[i*dimx+i])<<15)/rho; 369 for (k=0; k<=i; k++){ 370 tau=(long(c)*Ch[k*dimx+j]-long(s)*Ch[k*dimx+i])>>15; 371 Ch[k*dimx+i]=(long(s)*Ch[k*dimx+j]+long(c)*Ch[k*dimx+i])>>15; 372 Ch[k*dimx+j]=tau; 397 tmp_long=(long(Ch[i*dimx+i])*Ch[i*dimx+i]+long(Ch[i*dimx+j])*Ch[i*dimx+j]); 398 if (tmp_long>0){ 399 rho=sqrt((double)(tmp_long)); 400 s=(long(Ch[i*dimx+j])<<15)/rho; 401 c=(long(Ch[i*dimx+i])<<15)/rho; 402 for (k=0; k<=i; k++){ 403 tau=(long(c)*Ch[k*dimx+j]-long(s)*Ch[k*dimx+i])>>15; 404 Ch[k*dimx+i]=(long(s)*Ch[k*dimx+j]+long(c)*Ch[k*dimx+i])>>15; 405 Ch[k*dimx+j]=tau; 406 } 373 407 } 374 408 } 375 409 } 410 411 /* Matrix multiply Full matrix by upper diagonal matrix; */ 412 void mmultACh(int16 *m1, int16 *up, int16 *result, unsigned int16 rows, unsigned int16 columns) { 413 unsigned int16 i, j, k; 414 int32 tmp_sum=0L; 415 int16 *m2pom; 416 int16 *m1pom=m1; 417 int16 *respom=result; 418 419 for (i=0; i<rows; i++) //rows of result 420 { 421 for (j=0; j<columns; j++) //columns of result 422 { 423 m2pom=up+j;//?? 424 425 for (k=0; k<=j; k++) //inner loop up to "j" - U(j,j)==1; 426 { 427 tmp_sum+=(int32)(*(m1pom++))**m2pom; 428 m2pom+=columns; 429 } 430 m1pom-=(j+1); // shift back to first element 431 432 *respom++=tmp_sum>>15; 433 434 tmp_sum=0; 435 } 436 m1pom+=(columns); 437 } 438 } -
applications/pmsm/simulator_zdenek/ekf_example/matrix_vs.h
r1240 r1241 13 13 #define qAU 14 14 14 #define qD 14 15 #define qCh 15 15 16 16 17 #define int16 short int -
applications/pmsm/simulator_zdenek/test_Ch.cpp
r1230 r1241 31 31 /////////// COPY 32 32 imat Af=round_i(A*multip); 33 mat_to_int (Af, PSI);34 mat_to_int (round_i(Ch*multip),Chf);33 mat_to_int16(Af, PSI); 34 mat_to_int16(round_i(Ch*multip),Chf); 35 35 int Qf[25]; 36 mat_to_int (round_i(sqrt(Q)*multip), Qf);36 mat_to_int16(round_i(sqrt(Q)*multip), Qf); 37 37 int Rf[2]; 38 vec_to_int (round_i(R*multip), Rf);38 vec_to_int16(round_i(R*multip), Rf); 39 39 40 40 … … 49 49 cout << "Delta PSI: " << round_i(PhiCh*multip-PChcmp) <<endl; 50 50 51 mat_to_int (round_i(PhiCh*multip),PSICh); //<< make is same51 mat_to_int16(round_i(PhiCh*multip),PSICh); //<< make is same 52 52 53 53 mat PhiU= A*U; … … 103 103 vec d=diag(Ch*Ch.T()); 104 104 mat Ch2 = 0.9*diag(1./sqrt(d))*Ch; 105 mat_to_int (round_i(Ch2*multip),Chf);105 mat_to_int16(round_i(Ch2*multip),Chf); 106 106 107 107 D = pow(diag(Ch2),2); … … 113 113 114 114 int difz[2]; 115 vec_to_int (round_i(ydif*multip), difz);115 vec_to_int16(round_i(ydif*multip), difz); 116 116 int xf[5]; 117 vec_to_int (round_i(xp*multip), xf);117 vec_to_int16(round_i(xp*multip), xf); 118 118 119 119 cout << "x: "<< round_i(xp*multip) <<endl; … … 122 122 123 123 int xf_old[5]; 124 vec_to_int (ivec(xf,5),xf_old);124 vec_to_int16(ivec(xf,5),xf_old); 125 125 126 126 /////// Test bierman … … 171 171 { 172 172 imat Chcmp(Chf,5,5); 173 cout << "Del atCh: " << round_i(Ch*multip-Chcmp) << endl;174 cout << "Del atCh: " << Chcmp << endl;173 cout << "Delta Ch: " << round_i(Ch*multip-Chcmp) << endl; 174 cout << "Delta Ch: " << Chcmp << endl; 175 175 } 176 176