Changeset 1237
- Timestamp:
- 10/29/10 16:17:33 (14 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/pmsm/simulator_zdenek/ekf_example/matrix_vs.cpp
r1235 r1237 196 196 int *U_ij; 197 197 int *x_i; 198 int dzs; 199 198 200 a = U; // iyth row of U 199 201 for (iy=0; iy<dimy; iy++, a+=dimx) { … … 220 222 } 221 223 } 222 intdzs = (((long)difz[iy])<<15)/alpha; // apply scaling to innovations224 dzs = (((long)difz[iy])<<15)/alpha; // apply scaling to innovations 223 225 // no shift due to gamma 224 226 for (i=0,x_i=xp,b_i=b; i<dimx; i++,x_i++,b_i++) { … … 245 247 int irows,jrows; 246 248 long sigma; // in qAU+15!! 249 long z; 247 250 248 251 for (i=0,Dold_i=Dold,D_i=D;i<rows;i++,Dold_i++,D_i++) { … … 263 266 264 267 for (i=rows-1, Dold_i=Dold+i, D_i=D+i; 265 true; i--, Dold_i--,D_i--) { // stop if i==0 at the END!268 1; i--, Dold_i--,D_i--) { // stop if i==0 at the END! 266 269 irows=i*rows; 267 270 sigma = 0; … … 274 277 sigma += (((long)(*G_ik)**G_ik)>>16)**(Q+j+j*rows); 275 278 276 } 277 278 if (sigma>( 1<<(qAU+15))) {279 } 280 281 if (sigma>((long)1<<(qAU+15))) { 279 282 *D_i = 32767; 280 *(Dold+i)-=*(Q+i+irows);283 // *(Dold+i)-=*(Q+i+irows); 281 284 } else { 282 285 *D_i=sigma>>qAU; … … 291 294 k<rows; k++, PSIU_ik++, PSIU_jk++, Dold_k++) { 292 295 293 sigma += ((( long(*PSIU_ik)**PSIU_jk)>>qAU)**Dold_k);296 sigma += ((((long)(*PSIU_ik)**PSIU_jk)>>qAU)**Dold_k); 294 297 } 295 298 … … 299 302 } 300 303 301 longz=(sigma/(*D_i))<<(15-qAU); // shift to q15304 z=(sigma/(*D_i))<<(15-qAU); // shift to q15 302 305 if (z>32767) z=32767; 303 306 if (z<-32768) z=-32768; … … 372 375 double xd(double(x)/32768.); 373 376 return round(sqrt(xd)*32768); 374 377 375 378 //sqrt(x) == 1/2*2^(1/2)+1/2*2^(1/2)*(x-1/2)-1/4*2^(1/2)*(x-1/2)^2 376 379 // = k1 + k1*(x-0.5) - k2*(x-0.5)(x-0.5); … … 393 396 void householder(int *Ch /*= int *PSICh*/, int *Q, unsigned int dimx) { 394 397 int k,j,i; 395 int sigma,alpha,beta; 398 int alpha,beta; 399 long sigma; 396 400 int B[25];//beware 397 401 int w[5]; … … 399 403 400 404 // copy Q to B 401 for (i=0;i<dimx*dimx;i++) { 405 for (i=0;i<dimx*dimx;i++) 406 { 402 407 B[i]=Q[i]; 403 408 } 404 409 405 for (k=dimx-1; k>=0; k--) { 410 for (k=dimx-1; k>=0; k--) 411 { 406 412 sigma=0; 407 for (j=0;j<dimx;j++) { 408 sigma+=(long(B[k*dimx+j])*B[k*dimx+j])>>15; 409 } 410 for (j=0;j<=k;j++) { 411 sigma+=(long(Ch[k*dimx+j])*Ch[k*dimx+j])>>15; 413 for (j=0;j<dimx;j++) 414 { 415 sigma+=(long)B[k*dimx+j]*B[k*dimx+j]; 416 } 417 for (j=0;j<=k;j++) 418 { 419 sigma+=(long)Ch[k*dimx+j]*Ch[k*dimx+j]; 412 420 } 413 421 /* double sigf=double(sigma)/(1<<15); 414 422 double alpf = sqrt(sigf);*/ 415 if (sigma>16384) sigma=16384; 416 alpha=int_sqrt(sigma); 423 // if (sigma>16384) sigma=16384; 424 // alpha=int_sqrt(sigma); 425 alpha = (int)(sqrt((double)sigma)+0.5); // predelat pro DSP 417 426 // alpha = alpf*(1<<15); 418 427 // … … 420 429 for (j=0;j<dimx;j++) { 421 430 w[j]=B[k*dimx+j]; 422 sigma+=(long (w[j])*w[j])>>15;431 sigma+=(long)w[j])*w[j]; 423 432 } 424 433 for (j=0; j<=k;j++) { … … 428 437 v[j]=Ch[k*dimx+j]; 429 438 } 430 sigma+=(long(v[j])*v[j])>>15; 431 } 432 alpha=sigma>>1; 433 if (alpha==0) alpha =1; 439 sigma+=(long)v[j]*v[j]; 440 } 441 442 alpha=sigma>>16; 443 if (alpha==0) alpha =1; 444 434 445 for (i=0;i<=k;i++) { 435 446 sigma=0; 436 447 for (j=0;j<dimx;j++) { 437 sigma+=(long (B[i*dimx+j])*w[j])>>15;448 sigma+=(long)B[i*dimx+j]*w[j]; 438 449 } 439 450 for (j=0;j<=k;j++) { 440 sigma+=(long(Ch[i*dimx+j])*v[j])>>15; 441 } 442 for (j=0;j<dimx;j++) { 443 B[i*dimx+j]-=(long(sigma)*w[j]/alpha); 451 sigma+=(long)Ch[i*dimx+j]*v[j]; 452 } 453 454 sigma = sigma >> 15; // navrat do Q15 455 456 for (j=0;j<dimx;j++) 457 { 458 B[i*dimx+j]-=(sigma*w[j])/alpha; 444 459 }; 445 for (j=0;j<=k;j++) { 446 Ch[i*dimx+j]-=(long(sigma)*v[j]/alpha); 460 461 for (j=0;j<=k;j++) 462 { 463 Ch[i*dimx+j]-=(sigma*v[j])/alpha; 447 464 } 448 465 } … … 457 474 int w[5]; 458 475 459 for (iy=0; iy<dimy; iy++) { 476 for (iy=0; iy<dimy; iy++) 477 { 460 478 alpha=R[iy]; 461 479 delta = difz[iy]; 462 480 463 for (j=0;j<dimx;j++) { 481 for (j=0;j<dimx;j++) 482 { 464 483 sigma=Ch[iy*dimx+j]; 465 484 beta=alpha; 466 alpha+=( long(sigma)*sigma)>>15;485 alpha+=((long)sigma*sigma)>>15; 467 486 // double ab=(double)alpha*beta/32768./32768.; 468 487 // double s_ab=sqrt(ab); 469 gamma= int_sqrt(((long)alpha*beta)>>15);488 gamma=sqrt((double)((long)alpha*beta))+0.5; // predelat v DSP 470 489 //gamma = round(s_ab*(1<<15)); 471 eta=(long (beta)<<15) / gamma;490 // eta=((long)beta<<15) / gamma; 472 491 //zeta=(long(sigma)<<15)/ gamma; 473 492 w[j]=0; 474 493 for (i=0;i<=j;i++) { 475 494 tau=Ch[i*dimx+j]; 476 Ch[i*dimx+j]=((long (eta)*Ch[i*dimx+j])>>15) -(long(sigma)*w[i])/gamma;477 w[i]+=( long(tau)*sigma)>>15;495 Ch[i*dimx+j]=((long)beta*Ch[i*dimx+j] -(long)sigma*w[i])/gamma; 496 w[i]+=((long)tau*sigma)>>15; 478 497 } 479 498 } … … 481 500 //epsilon=(long(difz)<<15) / (alpha); // q15*q13/q13 = q15 482 501 for (i=0;i<dimx;i++) { 483 xp[i]+=( long(w[i])*delta)/alpha;502 xp[i]+=((long)w[i]*delta)/alpha; 484 503 } 485 504 } … … 490 509 int i,j,k; 491 510 int rho,s,c,tau; 492 511 493 512 int A[25];//beware 494 513 // copy Q to A … … 496 515 A[i]=Q[i]; 497 516 } 498 499 517 518 500 519 for (i=dimx-1; i>=0; i--){ 501 520 for (j=0; j<dimx; j++) {