Changeset 1340 for applications/pmsm
- Timestamp:
- 04/28/11 22:18:33 (14 years ago)
- Location:
- applications/pmsm/simulator_zdenek/ekf_example
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/pmsm/simulator_zdenek/ekf_example/matrix_vs.cpp
r1333 r1340 85 85 void bierman_fast(int16 *difz, int16 *xp, int16 *U, int16 *D, int16 *R, unsigned int16 dimy, unsigned int16 dimx ) 86 86 { 87 int16 alpha; 87 int16 alpha; // in qD!! 88 88 int16 beta,lambda; 89 int16 b[5]; // ok even for 4-dim state 89 int16 b[5]; // ok even for 4-dim state // in qD!!! 90 90 int16 *a; // in [0,1] -> q15 91 91 unsigned int16 iy,j,i; … … 96 96 int16 *U_ij; 97 97 int16 *x_i; 98 99 int32 z_pom;100 int16 z_pom_int16;101 98 102 99 int16 Ucopy[16]; … … 113 110 *b_j=((int32)(*D_j)*(*a_j))>>15; 114 111 115 alpha = R[iy]; //\alpha = R+vDv = R+a*b 116 // R in q15, a in q15, b=q15 117 // gamma = (1<<15)/alpha; //(in q15) 118 //min alpha = R[iy] = 164 119 //max gamma = 0.0061 => gamma_ref = q7 112 alpha = (R[iy])>>(15-qD); //\alpha = R+vDv = R+a*b 120 113 for (j=0,a_j=a,b_j=b,D_j=D; j<dimx; j++,a_j++,b_j++,D_j++) { 121 /* beta=alpha; 122 lambda = -((int32)(*a_j)<<15)/beta; 123 alpha += ((int32)(*a_j)*(*b_j))>>15; 124 D[j] = ((int32)beta**D_j)/alpha;*/ 125 /*xx*/ 126 lambda=alpha; 114 115 lambda=alpha; // in qD 127 116 alpha += ((int32)(*a_j)*(*b_j))>>15; 128 117 D[j] = ((int32)lambda**D_j)/alpha; 129 z_pom_int16 = -((int32)(*a_j)<<15)/lambda;130 /*xx*/131 118 132 119 if (*D_j==0) *D_j=1; … … 134 121 for (i=0,b_i=b,U_ij=U+j; i<j; i++, b_i++,U_ij+=dimx) { 135 122 beta = *U_ij; 136 // *U_ij += ((int32)lambda*(*b_i))>>15; // puvodni reseni137 123 *U_ij -= ((int32)(*a_j)*(*b_i))/lambda; // pozadovane optimalni reseni 138 // *U_ij -= ((int32)((int16)((int32)(*a_j)<<15)/lambda)**b_i)>>15; // tohle funguje - problem je s tim pretypovanim na (int16)139 // *U_ij -= (int16)((int32)(*a_j)*(*b_i))/lambda;140 // z_pom = (((int32)(*a_j)*(*b_i))/lambda);141 /* z_pom = (int32)(*U_ij)-(int16)((int32)(*a_j)*(*b_i))/lambda;142 if (z_pom > 32767) z_pom = 32767;143 if (z_pom < - 32768) z_pom = -32768;144 *U_ij = z_pom; /**/145 // *U_ij += ((int32)z_pom_int16*(*b_i))>>15; // puvodni reseni - jen jina konstanta146 124 *b_i += ((int32)beta*(*b_j))>>15; 147 125 } … … 156 134 void bierman_fastC(int16 *difz, int16 *xp, int16 *U, int16 *D, int16 *C, int16 *R, unsigned int16 dimy, unsigned int16 dimx ) 157 135 { 158 int16 alpha; 136 int16 alpha; // in qD 159 137 int16 beta,lambda; 160 int16 b[5]; // ok even for 4-dim state 138 int16 b[5]; // ok even for 4-dim state // in qD 161 139 int16 *a; // in [0,1] -> qCU 162 140 unsigned int16 iy,j,i; … … 180 158 // a is a row 181 159 for (j=0,a_j=a,b_j=b,D_j=D; j<dimx; j++,b_j++,D_j++,a_j++) 182 *b_j=((int32)(*D_j)*(*a_j))>>15; 183 184 alpha = R[iy]; //\alpha = R+vDv = R+a*b160 *b_j=((int32)(*D_j)*(*a_j))>>15; 161 162 alpha = (R[iy])>>(15-qD); //\alpha = R+vDv = R+a*b 185 163 // R in q15, a in q15, b=q15 186 164 // gamma = (1<<15)/alpha; //(in q15) … … 232 210 int16 *G_ik,*G_jk; 233 211 int16 irows,jrows; 234 int32 sigma; // in qAU+ 15!!212 int32 sigma; // in qAU+qD!! 235 213 int32 z; 236 214 … … 259 237 sigma += (((int32)(*PSIU_ik)**PSIU_ik)>>(qAU))*(*Dold_k); 260 238 } 261 sigma += (int32)(*(Q+i+irows))<< qAU;239 sigma += (int32)(*(Q+i+irows))<<(qAU+qD-15); 262 240 for (j=i+1, G_ik=G+irows+i+1; j<rows; j++,G_ik++) { 263 sigma += ((( int32)(*G_ik)**G_ik)>>(30-qAU))**(Q+j+j*rows);241 sigma += ((((int32)(*G_ik)**G_ik)>>15)**(Q+j+j*rows))>>(30-qAU-qD); 264 242 } 265 243 … … 284 262 for (k=i,G_ik=G+irows+i,G_jk=G+jrows+i,Q_kk=Q+k*rows+k; 285 263 k<rows;k++,G_ik++,G_jk++,Q_kk+=rows+1) { 286 sigma += (((( int32)*G_ik)**G_jk)>>(30-qAU))**Q_kk;264 sigma += (((((int32)*G_ik)**G_jk)>>15)**Q_kk)>>(30-qD-qAU); 287 265 } 288 266 … … 308 286 if (i==0) return; 309 287 } 310 }311 312 /* square root of 0<a<1 using taylor at 0.5 in q15*/313 int16 int_sqrt(int16 x) {314 double xd(double(x)/32768.);315 return round(sqrt(xd)*32768);316 317 //sqrt(x) == 1/2*2^(1/2)+1/2*2^(1/2)*(x-1/2)-1/4*2^(1/2)*(x-1/2)^2318 // = k1 + k1*(x-0.5) - k2*(x-0.5)(x-0.5);319 #define k1 23170 //0.5*sqrt(2)*32768320 #define k2 11585 //0.25*sqrt(2)*32768321 322 int16 tmp;323 if (x>6554) {324 int16 xm05=x-16384;325 tmp = ((int32)k1*xm05)>>15;326 tmp-=(((int32(k2)*xm05)>>15)*xm05)>>15;327 tmp +=k1;328 } else {329 tmp = 4*x;330 tmp-=int32(8*x)*x>>15;331 }332 return tmp;333 288 } 334 289 -
applications/pmsm/simulator_zdenek/ekf_example/matrix_vs.h
r1329 r1340 13 13 #define qAU 14 14 14 #define qCU 15 15 #define qD 1 415 #define qD 13 16 16 #define qCh 14 17 17