[1174] | 1 | /************************************ |
---|
| 2 | Extended Kalman Filter |
---|
| 3 | Matrix operations |
---|
| 4 | |
---|
[1240] | 5 | V. Smidl, Z. Peroutka |
---|
[1174] | 6 | |
---|
[1240] | 7 | Rev. 28.10.2010 (ZP) |
---|
[1174] | 8 | |
---|
[1240] | 9 | 26.10.2010 Prvni verze (VS) |
---|
[1174] | 10 | |
---|
[1240] | 11 | 26.10.2010 Upravena chyba v Thorton_fast - spatne shiftovani o vypoctu SIGMA. |
---|
| 12 | 27.10.2010 Pokus o odstraneni problemu v Thorton_fast - potize dela omezovani (orezavani) varianci. |
---|
| 13 | 28.10.2010 Drobne upravy v kodu. |
---|
| 14 | |
---|
[1174] | 15 | *************************************/ |
---|
[1225] | 16 | |
---|
| 17 | #include "matrix_vs.h" |
---|
[1241] | 18 | #include <math.h> |
---|
[1313] | 19 | #include "matrix.h" |
---|
[1225] | 20 | |
---|
[1321] | 21 | #include <stdio.h> |
---|
[1174] | 22 | /* Matrix multiply Full matrix by upper diagonal matrix; */ |
---|
[1240] | 23 | void mmultAU(int16 *m1, int16 *up, int16 *result, unsigned int16 rows, unsigned int16 columns) { |
---|
| 24 | unsigned int16 i, j, k; |
---|
| 25 | int32 tmp_sum=0L; //in 15+qAU |
---|
| 26 | int16 *m2pom; |
---|
| 27 | int16 *m1pom=m1; |
---|
| 28 | int16 *respom=result; |
---|
[1174] | 29 | |
---|
[1179] | 30 | for (i=0; i<rows; i++) //rows of result |
---|
[1174] | 31 | { |
---|
[1179] | 32 | for (j=0; j<columns; j++) //columns of result |
---|
[1240] | 33 | { |
---|
[1179] | 34 | m2pom=up+j;//?? |
---|
[1174] | 35 | |
---|
[1179] | 36 | for (k=0; k<j; k++) //inner loop up to "j" - U(j,j)==1; |
---|
| 37 | { |
---|
[1240] | 38 | tmp_sum+=((int32)(*(m1pom++))**m2pom)>>(15-qAU); |
---|
[1179] | 39 | m2pom+=columns; |
---|
| 40 | } |
---|
| 41 | // add the missing A(i,j) |
---|
[1240] | 42 | tmp_sum +=(int32)(*m1pom)<<qAU; // no need to shift |
---|
[1225] | 43 | m1pom-=(j); // shift back to first element |
---|
[1174] | 44 | |
---|
[1235] | 45 | *respom++=tmp_sum>>15; |
---|
[1225] | 46 | |
---|
| 47 | tmp_sum=0; |
---|
| 48 | } |
---|
| 49 | m1pom+=(columns); |
---|
| 50 | } |
---|
| 51 | }; |
---|
| 52 | |
---|
[1313] | 53 | //same as mmultAU but different precision |
---|
| 54 | void mmultCU(int16 *m1, int16 *up, int16 *result, unsigned int16 rows, unsigned int16 columns) { |
---|
| 55 | unsigned int16 i, j, k; |
---|
| 56 | int32 tmp_sum=0L; //in 15+qAU |
---|
| 57 | int16 *m2pom; |
---|
| 58 | int16 *m1pom=m1; |
---|
| 59 | int16 *respom=result; |
---|
| 60 | |
---|
| 61 | for (i=0; i<rows; i++) //rows of result |
---|
| 62 | { |
---|
| 63 | for (j=0; j<columns; j++) //columns of result |
---|
| 64 | { |
---|
| 65 | m2pom=up+j;//?? |
---|
| 66 | |
---|
| 67 | for (k=0; k<j; k++) //inner loop up to "j" - U(j,j)==1; |
---|
| 68 | { |
---|
| 69 | tmp_sum+=((int32)(*(m1pom++))**m2pom)>>(15-qCU); |
---|
| 70 | m2pom+=columns; |
---|
| 71 | } |
---|
| 72 | // add the missing A(i,j) |
---|
| 73 | tmp_sum +=(int32)(*m1pom)<<qCU; // no need to shift |
---|
| 74 | m1pom-=(j); // shift back to first element |
---|
| 75 | |
---|
| 76 | *respom++=tmp_sum>>15; |
---|
| 77 | |
---|
| 78 | tmp_sum=0; |
---|
| 79 | } |
---|
| 80 | m1pom+=(columns); |
---|
| 81 | } |
---|
| 82 | }; |
---|
[1225] | 83 | |
---|
[1313] | 84 | |
---|
[1240] | 85 | void bierman_fast(int16 *difz, int16 *xp, int16 *U, int16 *D, int16 *R, unsigned int16 dimy, unsigned int16 dimx ) |
---|
| 86 | { |
---|
| 87 | int16 alpha; |
---|
| 88 | int16 beta,lambda; |
---|
| 89 | int16 b[5]; // ok even for 4-dim state |
---|
| 90 | int16 *a; // in [0,1] -> q15 |
---|
| 91 | unsigned int16 iy,j,i; |
---|
[1225] | 92 | |
---|
[1240] | 93 | int16 *b_j,*b_i; |
---|
| 94 | int16 *a_j; |
---|
| 95 | int16 *D_j; |
---|
| 96 | int16 *U_ij; |
---|
| 97 | int16 *x_i; |
---|
[1225] | 98 | |
---|
[1240] | 99 | int32 z_pom; |
---|
| 100 | int16 z_pom_int16; |
---|
[1304] | 101 | |
---|
| 102 | int16 Ucopy[16]; |
---|
| 103 | |
---|
| 104 | /* copy U for vector a */ |
---|
| 105 | int16 *Uc_i=Ucopy; |
---|
| 106 | int16 *U_i=U; |
---|
| 107 | for (i=0;i<dimx*dimx;i++) *(Uc_i++)=*(U_i++); |
---|
[1174] | 108 | |
---|
[1304] | 109 | a = Ucopy; // iyth row of U |
---|
[1179] | 110 | for (iy=0; iy<dimy; iy++, a+=dimx) { |
---|
| 111 | // a is a row |
---|
| 112 | for (j=0,a_j=a,b_j=b,D_j=D; j<dimx; j++,b_j++,D_j++,a_j++) |
---|
[1240] | 113 | *b_j=((int32)(*D_j)*(*a_j))>>15; |
---|
[1179] | 114 | |
---|
[1240] | 115 | alpha = R[iy]; //\alpha = R+vDv = R+a*b |
---|
[1179] | 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 |
---|
| 120 | for (j=0,a_j=a,b_j=b,D_j=D; j<dimx; j++,a_j++,b_j++,D_j++) { |
---|
[1240] | 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; |
---|
| 127 | alpha += ((int32)(*a_j)*(*b_j))>>15; |
---|
| 128 | D[j] = ((int32)lambda**D_j)/alpha; |
---|
| 129 | z_pom_int16 = -((int32)(*a_j)<<15)/lambda; |
---|
| 130 | /*xx*/ |
---|
| 131 | |
---|
[1179] | 132 | if (*D_j==0) *D_j=1; |
---|
| 133 | |
---|
| 134 | for (i=0,b_i=b,U_ij=U+j; i<j; i++, b_i++,U_ij+=dimx) { |
---|
| 135 | beta = *U_ij; |
---|
[1240] | 136 | // *U_ij += ((int32)lambda*(*b_i))>>15; // puvodni reseni |
---|
| 137 | *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 konstanta |
---|
| 146 | *b_i += ((int32)beta*(*b_j))>>15; |
---|
[1179] | 147 | } |
---|
| 148 | } |
---|
| 149 | // no shift due to gamma |
---|
| 150 | for (i=0,x_i=xp,b_i=b; i<dimx; i++,x_i++,b_i++) { |
---|
[1240] | 151 | *x_i += ((int32)difz[iy]*(*b_i))/alpha; // multiply by unscaled Kalman gain |
---|
[1179] | 152 | } |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
[1313] | 156 | void bierman_fastC(int16 *difz, int16 *xp, int16 *U, int16 *D, int16 *C, int16 *R, unsigned int16 dimy, unsigned int16 dimx ) |
---|
| 157 | { |
---|
| 158 | int16 alpha; |
---|
| 159 | int16 beta,lambda; |
---|
| 160 | int16 b[5]; // ok even for 4-dim state |
---|
| 161 | int16 *a; // in [0,1] -> qCU |
---|
| 162 | unsigned int16 iy,j,i; |
---|
| 163 | |
---|
| 164 | int16 *b_j,*b_i; |
---|
| 165 | int16 *a_j; |
---|
| 166 | int16 *D_j; |
---|
| 167 | int16 *U_ij; |
---|
| 168 | int16 *x_i; |
---|
| 169 | |
---|
| 170 | // int32 z_pom; |
---|
| 171 | // int16 z_pom_int16; |
---|
| 172 | |
---|
| 173 | int16 UC[16]; // in q15 |
---|
| 174 | |
---|
| 175 | /* copy U for vector a */ |
---|
| 176 | mmultCU(C,U,UC,dimy,dimx); |
---|
| 177 | |
---|
| 178 | a = UC; // iyth row of U |
---|
| 179 | for (iy=0; iy<dimy; iy++, a+=dimx) { |
---|
| 180 | // a is a row |
---|
| 181 | 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*b |
---|
| 185 | // R in q15, a in q15, b=q15 |
---|
| 186 | // gamma = (1<<15)/alpha; //(in q15) |
---|
| 187 | //min alpha = R[iy] = 164 |
---|
| 188 | //max gamma = 0.0061 => gamma_ref = q7 |
---|
| 189 | for (j=0,a_j=a,b_j=b,D_j=D; j<dimx; j++,a_j++,b_j++,D_j++) { |
---|
| 190 | /* beta=alpha; |
---|
| 191 | * lambda = -((int32)(*a_j)<<15)/beta; |
---|
| 192 | * alpha += ((int32)(*a_j)*(*b_j))>>15; |
---|
| 193 | * D[j] = ((int32)beta**D_j)/alpha;*/ |
---|
| 194 | /*xx*/ |
---|
| 195 | lambda=alpha; |
---|
| 196 | alpha += ((int32)(*a_j)*(*b_j))>>15; |
---|
| 197 | D[j] = ((int32)lambda**D_j)/alpha; |
---|
| 198 | // z_pom_int16 = -((int32)(*a_j)<<15)/lambda; |
---|
| 199 | /*xx*/ |
---|
| 200 | |
---|
| 201 | if (*D_j==0) *D_j=1; |
---|
| 202 | |
---|
| 203 | for (i=0,b_i=b,U_ij=U+j; i<j; i++, b_i++,U_ij+=dimx) { |
---|
| 204 | beta = *U_ij; |
---|
| 205 | // *U_ij += ((int32)lambda*(*b_i))>>15; // puvodni reseni |
---|
| 206 | *U_ij -= ((int32)(*a_j)*(*b_i))/lambda; // pozadovane optimalni reseni |
---|
| 207 | *b_i += ((int32)beta*(*b_j))>>15; |
---|
[1326] | 208 | //printf("%d, %d, %d\n", ((int32)(*a_j)*(*b_i))/lambda, *b_i, beta); |
---|
[1313] | 209 | } |
---|
| 210 | } |
---|
| 211 | // no shift due to gamma |
---|
| 212 | for (i=0,x_i=xp,b_i=b; i<dimx; i++,x_i++,b_i++) { |
---|
| 213 | *x_i += ((int32)difz[iy]*(*b_i))/alpha; // multiply by unscaled Kalman gain |
---|
| 214 | } |
---|
| 215 | } |
---|
| 216 | } |
---|
| 217 | |
---|
[1179] | 218 | // Thorton procedure - Kalman predictive variance in UD |
---|
[1245] | 219 | void thorton(int16 *U, int16 *D, int16 *PSIU, int16 *Q, int16 *G, int16 *Dold, unsigned int16 rows) { |
---|
| 220 | thorton_fast(U, D, PSIU, Q, G, Dold, rows); |
---|
| 221 | } |
---|
| 222 | |
---|
| 223 | // Thorton procedure - Kalman predictive variance in UD |
---|
[1240] | 224 | void thorton_fast(int16 *U, int16 *D, int16 *PSIU, int16 *Q, int16 *G, int16 *Dold, unsigned int16 rows) { |
---|
| 225 | unsigned int16 i,j,k; |
---|
[1179] | 226 | // copy D to Dold |
---|
[1240] | 227 | int16 *Dold_i,*Dold_k; |
---|
| 228 | int16 *D_i; |
---|
| 229 | int16 *PSIU_ij,*PSIU_ik,*PSIU_jk; |
---|
| 230 | int16 *Q_jj,*Q_ii,*Q_kk; |
---|
| 231 | int16 *U_ji; |
---|
| 232 | int16 *G_ik,*G_jk; |
---|
| 233 | int16 irows,jrows; |
---|
| 234 | int32 sigma; // in qAU+15!! |
---|
| 235 | int32 z; |
---|
[1179] | 236 | |
---|
| 237 | for (i=0,Dold_i=Dold,D_i=D;i<rows;i++,Dold_i++,D_i++) { |
---|
| 238 | *Dold_i=*D_i; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | // initialize G = eye() |
---|
| 242 | G_ik= G; |
---|
[1235] | 243 | *G_ik++=32767; |
---|
[1179] | 244 | for (i=0;i<rows-1;i++) { |
---|
| 245 | // clean elem before diag |
---|
| 246 | for (k=0; k<rows; k++) { |
---|
| 247 | *G_ik++=0; |
---|
| 248 | } |
---|
[1235] | 249 | *G_ik++=32767; |
---|
[1179] | 250 | } |
---|
| 251 | // eye created |
---|
| 252 | |
---|
| 253 | for (i=rows-1, Dold_i=Dold+i, D_i=D+i; |
---|
[1237] | 254 | 1; i--, Dold_i--,D_i--) { // stop if i==0 at the END! |
---|
[1179] | 255 | irows=i*rows; |
---|
| 256 | sigma = 0; |
---|
[1197] | 257 | for (k=0, PSIU_ik=PSIU+irows,Dold_k=Dold; |
---|
[1233] | 258 | k<rows; k++, PSIU_ik++,Dold_k++) { |
---|
[1240] | 259 | sigma += (((int32)(*PSIU_ik)**PSIU_ik)>>(qAU))*(*Dold_k); |
---|
[1225] | 260 | } |
---|
[1240] | 261 | sigma += (int32)(*(Q+i+irows))<<qAU; |
---|
[1179] | 262 | for (j=i+1, G_ik=G+irows+i+1; j<rows; j++,G_ik++) { |
---|
[1313] | 263 | sigma += (((int32)(*G_ik)**G_ik)>>(30-qAU))**(Q+j+j*rows); |
---|
[1237] | 264 | } |
---|
[1235] | 265 | |
---|
[1240] | 266 | if (sigma>((int32)1<<(qAU+15))) { |
---|
[1235] | 267 | *D_i = 32767; |
---|
[1237] | 268 | // *(Dold+i)-=*(Q+i+irows); |
---|
[1235] | 269 | } else { |
---|
| 270 | *D_i=sigma>>qAU; |
---|
| 271 | } |
---|
[1179] | 272 | if (*D_i==0) *D_i=1; |
---|
| 273 | |
---|
| 274 | for (j=0;j<i;j++) { |
---|
| 275 | jrows = j*rows; |
---|
| 276 | |
---|
| 277 | sigma =0; |
---|
| 278 | for (k=0, PSIU_ik=PSIU+irows, PSIU_jk=PSIU+jrows, Dold_k=Dold; |
---|
| 279 | k<rows; k++, PSIU_ik++, PSIU_jk++, Dold_k++) { |
---|
[1225] | 280 | |
---|
[1240] | 281 | sigma += ((((int32)(*PSIU_ik)**PSIU_jk)>>qAU)**Dold_k); |
---|
[1179] | 282 | } |
---|
[1225] | 283 | |
---|
[1235] | 284 | for (k=i,G_ik=G+irows+i,G_jk=G+jrows+i,Q_kk=Q+k*rows+k; |
---|
[1225] | 285 | k<rows;k++,G_ik++,G_jk++,Q_kk+=rows+1) { |
---|
[1313] | 286 | sigma += ((((int32)*G_ik)**G_jk)>>(30-qAU))**Q_kk; |
---|
[1179] | 287 | } |
---|
[1225] | 288 | |
---|
[1237] | 289 | z=(sigma/(*D_i))<<(15-qAU); // shift to q15 |
---|
[1179] | 290 | if (z>32767) z=32767; |
---|
| 291 | if (z<-32768) z=-32768; |
---|
| 292 | |
---|
| 293 | U_ji=U+jrows+i; |
---|
[1240] | 294 | *U_ji = (int16)z; |
---|
[1179] | 295 | |
---|
| 296 | |
---|
| 297 | for (k=0,PSIU_ik=PSIU+irows,PSIU_jk=PSIU+jrows; |
---|
| 298 | k<rows;k++,PSIU_ik++,PSIU_jk++) { |
---|
[1240] | 299 | *PSIU_jk -= ((int32)*U_ji**PSIU_ik)>>15; |
---|
[1179] | 300 | } |
---|
[1225] | 301 | |
---|
[1179] | 302 | for (k=0,G_jk=G+jrows,G_ik=G+irows; |
---|
[1225] | 303 | k<rows;k++, G_jk++, G_ik++) { |
---|
[1240] | 304 | *G_jk -= ((int32)*U_ji**G_ik)>>15; |
---|
[1179] | 305 | } |
---|
[1225] | 306 | |
---|
[1179] | 307 | } |
---|
[1235] | 308 | if (i==0) return; |
---|
[1179] | 309 | } |
---|
| 310 | } |
---|
| 311 | |
---|
[1225] | 312 | /* square root of 0<a<1 using taylor at 0.5 in q15*/ |
---|
[1241] | 313 | int16 int_sqrt(int16 x) { |
---|
[1230] | 314 | double xd(double(x)/32768.); |
---|
| 315 | return round(sqrt(xd)*32768); |
---|
[1237] | 316 | |
---|
[1225] | 317 | //sqrt(x) == 1/2*2^(1/2)+1/2*2^(1/2)*(x-1/2)-1/4*2^(1/2)*(x-1/2)^2 |
---|
| 318 | // = k1 + k1*(x-0.5) - k2*(x-0.5)(x-0.5); |
---|
| 319 | #define k1 23170 //0.5*sqrt(2)*32768 |
---|
| 320 | #define k2 11585 //0.25*sqrt(2)*32768 |
---|
| 321 | |
---|
[1241] | 322 | int16 tmp; |
---|
[1225] | 323 | if (x>6554) { |
---|
[1241] | 324 | int16 xm05=x-16384; |
---|
[1294] | 325 | tmp = ((int32)k1*xm05)>>15; |
---|
| 326 | tmp-=(((int32(k2)*xm05)>>15)*xm05)>>15; |
---|
[1225] | 327 | tmp +=k1; |
---|
| 328 | } else { |
---|
| 329 | tmp = 4*x; |
---|
[1294] | 330 | tmp-=int32(8*x)*x>>15; |
---|
[1225] | 331 | } |
---|
| 332 | return tmp; |
---|
| 333 | } |
---|
| 334 | |
---|
[1241] | 335 | void householder(int16 *Ch /*= int16 *PSICh*/, int16 *Q, unsigned int16 dimx) { |
---|
| 336 | int16 k,j,i; |
---|
| 337 | int16 alpha,beta; |
---|
| 338 | int32 sigma; // 2*qCh |
---|
| 339 | int32 tmp_long; |
---|
| 340 | int16 B[25];//Q in qCh |
---|
| 341 | int16 w[5]; |
---|
| 342 | int16 v[5]; |
---|
[1225] | 343 | |
---|
| 344 | // copy Q to B |
---|
[1237] | 345 | for (i=0;i<dimx*dimx;i++) |
---|
| 346 | { |
---|
[1241] | 347 | B[i]=Q[i]>>(15-qCh); |
---|
[1225] | 348 | } |
---|
| 349 | |
---|
[1237] | 350 | for (k=dimx-1; k>=0; k--) |
---|
| 351 | { |
---|
[1225] | 352 | sigma=0; |
---|
[1237] | 353 | for (j=0;j<dimx;j++) |
---|
| 354 | { |
---|
[1294] | 355 | sigma+=((int32)B[k*dimx+j]*B[k*dimx+j]); |
---|
[1225] | 356 | } |
---|
[1237] | 357 | for (j=0;j<=k;j++) |
---|
| 358 | { |
---|
[1294] | 359 | sigma+=((int32)Ch[k*dimx+j]*Ch[k*dimx+j]); |
---|
[1225] | 360 | } |
---|
[1241] | 361 | |
---|
| 362 | //alpha in qCh |
---|
[1294] | 363 | alpha = (int16)(sqrt((double)sigma)+0.5); // predelat pro DSP |
---|
[1241] | 364 | |
---|
| 365 | sigma=0; |
---|
[1225] | 366 | for (j=0;j<dimx;j++) { |
---|
| 367 | w[j]=B[k*dimx+j]; |
---|
[1294] | 368 | sigma+=(int32)w[j]*w[j]; |
---|
[1225] | 369 | } |
---|
| 370 | for (j=0; j<=k;j++) { |
---|
| 371 | if (j==k) { |
---|
| 372 | v[j]=Ch[k*dimx+j]-alpha; |
---|
| 373 | } else { |
---|
| 374 | v[j]=Ch[k*dimx+j]; |
---|
| 375 | } |
---|
[1294] | 376 | sigma+=(int32)v[j]*v[j]; |
---|
[1225] | 377 | } |
---|
[1237] | 378 | |
---|
[1241] | 379 | alpha=sigma>>(qCh+1); // alpha = sigma /2; |
---|
[1237] | 380 | if (alpha==0) alpha =1; |
---|
| 381 | |
---|
[1225] | 382 | for (i=0;i<=k;i++) { |
---|
| 383 | sigma=0; |
---|
| 384 | for (j=0;j<dimx;j++) { |
---|
[1294] | 385 | sigma+=((int32)B[i*dimx+j]*w[j]); |
---|
[1225] | 386 | } |
---|
| 387 | for (j=0;j<=k;j++) { |
---|
[1294] | 388 | sigma+=(int32)Ch[i*dimx+j]*v[j]; |
---|
[1225] | 389 | } |
---|
[1237] | 390 | |
---|
| 391 | sigma = sigma >> 15; // navrat do Q15 |
---|
[1245] | 392 | // if (sigma>32767)sigma=32767; |
---|
[1241] | 393 | |
---|
[1237] | 394 | for (j=0;j<dimx;j++) |
---|
| 395 | { |
---|
[1241] | 396 | tmp_long=B[i*dimx+j]-(sigma*w[j])/alpha; |
---|
| 397 | if (tmp_long>32767) { |
---|
| 398 | B[i*dimx+j]=32767; |
---|
| 399 | } else { |
---|
| 400 | if (tmp_long<-32767){ |
---|
| 401 | B[i*dimx+j]=-32767; |
---|
| 402 | } else { |
---|
| 403 | B[i*dimx+j]=tmp_long; |
---|
| 404 | } |
---|
| 405 | } |
---|
[1225] | 406 | }; |
---|
[1237] | 407 | |
---|
| 408 | for (j=0;j<=k;j++) |
---|
| 409 | { |
---|
[1241] | 410 | tmp_long=Ch[i*dimx+j]-(sigma*v[j])/alpha; |
---|
| 411 | if (tmp_long>32767) { |
---|
| 412 | Ch[i*dimx+j]=32767; |
---|
| 413 | } else { |
---|
| 414 | if (tmp_long<-32767){ |
---|
| 415 | Ch[i*dimx+j]=-32767; |
---|
| 416 | } else { |
---|
| 417 | Ch[i*dimx+j]=tmp_long; |
---|
| 418 | } |
---|
| 419 | } |
---|
| 420 | } |
---|
[1225] | 421 | } |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | } |
---|
| 425 | |
---|
[1241] | 426 | void carlson(int16 *difz, int16 *xp, int16 *Ch, int16 *R, unsigned int16 dimy, unsigned int16 dimx ) { |
---|
| 427 | int16 alpha,beta,gamma; |
---|
| 428 | int16 delta, eta,epsilon,zeta,sigma,tau; |
---|
| 429 | int16 i,j,iy; |
---|
| 430 | int16 w[5]; |
---|
| 431 | int32 tmp_long; |
---|
[1225] | 432 | |
---|
[1237] | 433 | for (iy=0; iy<dimy; iy++) |
---|
| 434 | { |
---|
[1225] | 435 | alpha=R[iy]; |
---|
| 436 | delta = difz[iy]; |
---|
| 437 | |
---|
[1237] | 438 | for (j=0;j<dimx;j++) |
---|
| 439 | { |
---|
[1225] | 440 | sigma=Ch[iy*dimx+j]; |
---|
| 441 | beta=alpha; |
---|
[1294] | 442 | alpha+=((int32)sigma*sigma)>>15; |
---|
[1225] | 443 | // double ab=(double)alpha*beta/32768./32768.; |
---|
| 444 | // double s_ab=sqrt(ab); |
---|
[1294] | 445 | gamma=(int16)(sqrt((double)((int32)alpha*beta))+0.5); // predelat v DSP |
---|
[1225] | 446 | //gamma = round(s_ab*(1<<15)); |
---|
[1237] | 447 | // eta=((long)beta<<15) / gamma; |
---|
[1225] | 448 | //zeta=(long(sigma)<<15)/ gamma; |
---|
| 449 | w[j]=0; |
---|
| 450 | for (i=0;i<=j;i++) { |
---|
| 451 | tau=Ch[i*dimx+j]; |
---|
[1294] | 452 | tmp_long=((int32)beta*Ch[i*dimx+j] -(int32)sigma*w[i])/gamma; |
---|
[1326] | 453 | /* if (tmp_long>32767) { |
---|
[1241] | 454 | Ch[i*dimx+j]=32767; |
---|
| 455 | } else { |
---|
| 456 | if (tmp_long<-32767){ |
---|
| 457 | Ch[i*dimx+j]=-32767; |
---|
| 458 | } else { |
---|
[1326] | 459 | */ Ch[i*dimx+j]=tmp_long; |
---|
| 460 | /* } |
---|
[1241] | 461 | } |
---|
[1326] | 462 | */ |
---|
[1294] | 463 | w[i]+=((int32)tau*sigma)>>15; |
---|
[1225] | 464 | } |
---|
| 465 | } |
---|
| 466 | |
---|
| 467 | //epsilon=(long(difz)<<15) / (alpha); // q15*q13/q13 = q15 |
---|
| 468 | for (i=0;i<dimx;i++) { |
---|
[1294] | 469 | xp[i]+=((int32)w[i]*delta)/alpha; |
---|
[1225] | 470 | } |
---|
| 471 | } |
---|
| 472 | } |
---|
[1230] | 473 | |
---|
| 474 | /* perform Householder update of Ch matrix using PSI*Ch , Q, */ |
---|
[1241] | 475 | extern void givens(int16 *Ch /*= int16 *PSICh*/, int16 *Q, unsigned int16 dimx){ |
---|
| 476 | int16 i,j,k; |
---|
| 477 | int16 rho,s,c,tau; |
---|
| 478 | int32 tmp_long; |
---|
[1237] | 479 | |
---|
[1241] | 480 | int16 A[25];//beware |
---|
[1230] | 481 | // copy Q to A |
---|
| 482 | for (i=0;i<dimx*dimx;i++) { |
---|
[1241] | 483 | A[i]=Q[i]>>(15-qCh); |
---|
[1230] | 484 | } |
---|
[1237] | 485 | |
---|
| 486 | |
---|
[1230] | 487 | for (i=dimx-1; i>=0; i--){ |
---|
| 488 | for (j=0; j<dimx; j++) { |
---|
[1294] | 489 | tmp_long=(int32)Ch[i*dimx+i]*Ch[i*dimx+i]+int32(A[i*dimx+j])*A[i*dimx+j]; |
---|
[1241] | 490 | if (tmp_long>0){ |
---|
| 491 | rho=sqrt(double(tmp_long)); |
---|
[1294] | 492 | s=(int32(A[i*dimx+j])<<15)/rho; |
---|
| 493 | c=(int32(Ch[i*dimx+i])<<15)/rho; |
---|
[1241] | 494 | for (k=0;k<=i; k++){ |
---|
[1294] | 495 | tau=(int32(c)*A[k*dimx+j]-int32(s)*Ch[k*dimx+i])>>15; |
---|
| 496 | Ch[k*dimx +i]=(int32(s)*A[k*dimx+j]+int32(c)*Ch[k*dimx+i])>>15; |
---|
[1241] | 497 | A[k*dimx +j]=tau; |
---|
| 498 | } |
---|
[1230] | 499 | } |
---|
| 500 | } |
---|
[1245] | 501 | |
---|
| 502 | for (j=0; j<i; j++){ |
---|
[1294] | 503 | tmp_long=(int32(Ch[i*dimx+i])*Ch[i*dimx+i]+int32(Ch[i*dimx+j])*Ch[i*dimx+j]); |
---|
[1245] | 504 | if (tmp_long>0){ |
---|
| 505 | rho=sqrt((double)(tmp_long)); |
---|
[1294] | 506 | s=(int32(Ch[i*dimx+j])<<15)/rho; |
---|
| 507 | c=(int32(Ch[i*dimx+i])<<15)/rho; |
---|
[1245] | 508 | for (k=0; k<=i; k++){ |
---|
[1294] | 509 | tau=(int32(c)*Ch[k*dimx+j]-int32(s)*Ch[k*dimx+i])>>15; |
---|
| 510 | Ch[k*dimx+i]=(int32(s)*Ch[k*dimx+j]+int32(c)*Ch[k*dimx+i])>>15; |
---|
[1245] | 511 | Ch[k*dimx+j]=tau; |
---|
| 512 | } |
---|
[1241] | 513 | } |
---|
[1230] | 514 | } |
---|
[1245] | 515 | |
---|
[1230] | 516 | } |
---|
| 517 | } |
---|
[1241] | 518 | |
---|
| 519 | /* Matrix multiply Full matrix by upper diagonal matrix; */ |
---|
| 520 | void mmultACh(int16 *m1, int16 *up, int16 *result, unsigned int16 rows, unsigned int16 columns) { |
---|
| 521 | unsigned int16 i, j, k; |
---|
| 522 | int32 tmp_sum=0L; |
---|
| 523 | int16 *m2pom; |
---|
| 524 | int16 *m1pom=m1; |
---|
| 525 | int16 *respom=result; |
---|
| 526 | |
---|
| 527 | for (i=0; i<rows; i++) //rows of result |
---|
| 528 | { |
---|
| 529 | for (j=0; j<columns; j++) //columns of result |
---|
| 530 | { |
---|
| 531 | m2pom=up+j;//?? |
---|
| 532 | |
---|
| 533 | for (k=0; k<=j; k++) //inner loop up to "j" - U(j,j)==1; |
---|
| 534 | { |
---|
| 535 | tmp_sum+=(int32)(*(m1pom++))**m2pom; |
---|
| 536 | m2pom+=columns; |
---|
| 537 | } |
---|
| 538 | m1pom-=(j+1); // shift back to first element |
---|
[1326] | 539 | |
---|
| 540 | /* if (tmp_sum>(1<<29)-1) |
---|
| 541 | *respom++=(1<<14); |
---|
| 542 | else |
---|
| 543 | */ |
---|
[1329] | 544 | *respom++=(tmp_sum+(1<<14))>>15; |
---|
[1326] | 545 | |
---|
[1241] | 546 | tmp_sum=0; |
---|
| 547 | } |
---|
| 548 | m1pom+=(columns); |
---|
| 549 | } |
---|
| 550 | } |
---|
[1294] | 551 | |
---|
| 552 | |
---|
| 553 | void givens_fast(int16 *Ch, int16 *Q, unsigned int16 dimx) |
---|
| 554 | { |
---|
| 555 | int16 i,j,k; |
---|
| 556 | int16 rho,s,c,tau; |
---|
| 557 | int32 tmp_long; |
---|
| 558 | |
---|
[1326] | 559 | //c,s in q14!! |
---|
| 560 | |
---|
[1294] | 561 | int16 A[25];//beware |
---|
| 562 | |
---|
| 563 | int16 *A_ij, *Q_i, *Ch_ki, *Ch_kj, *Ch_ii, *Ch_ij, *A_kj; |
---|
| 564 | |
---|
| 565 | A_ij=A; |
---|
| 566 | Q_i=Q; |
---|
| 567 | // copy Q to A |
---|
| 568 | for (i=0;i<dimx*dimx;i++) |
---|
| 569 | { |
---|
| 570 | // A[i]=Q[i]>>(15-qCh); |
---|
| 571 | *A_ij++=(*Q_i++)>>(15-qCh); |
---|
| 572 | } |
---|
| 573 | |
---|
| 574 | for (i=dimx-1; i>=0; i--) |
---|
| 575 | { |
---|
| 576 | Ch_ii=Ch+i*dimx+i; |
---|
| 577 | A_ij=A+i*dimx; |
---|
| 578 | |
---|
| 579 | for (j=0; j<dimx; j++) |
---|
| 580 | { |
---|
[1326] | 581 | if (*A_ij!=0) |
---|
[1294] | 582 | { |
---|
[1326] | 583 | tmp_long=(int32)*Ch_ii**Ch_ii+(int32)*A_ij**A_ij; |
---|
[1294] | 584 | // rho=qsqrt(tmp_long); // verze pro DSP |
---|
| 585 | rho=(int16)(sqrt((double)tmp_long)); // verze pro PC |
---|
[1326] | 586 | s=(((int32)*A_ij)<<14)/rho; |
---|
| 587 | c=(((int32)*Ch_ii)<<14)/rho; |
---|
[1294] | 588 | |
---|
| 589 | Ch_ki=Ch+i; |
---|
| 590 | A_kj=A+j; |
---|
| 591 | |
---|
| 592 | for (k=0;k<=i; k++) |
---|
| 593 | { |
---|
[1326] | 594 | tau=((int32)c**A_kj-(int32)s**Ch_ki)>>14; |
---|
| 595 | tmp_long=(int32)s**A_kj+(int32)c**Ch_ki; |
---|
[1329] | 596 | if (tmp_long>(1<<29)) //q14 + q14 |
---|
| 597 | *Ch_ki = (1<<15)-1; |
---|
[1326] | 598 | else |
---|
| 599 | *Ch_ki=tmp_long>>14; |
---|
[1294] | 600 | *A_kj=tau; |
---|
| 601 | |
---|
| 602 | Ch_ki+=dimx; |
---|
| 603 | A_kj+=dimx; |
---|
| 604 | } |
---|
| 605 | } |
---|
| 606 | A_ij++; |
---|
| 607 | } |
---|
| 608 | |
---|
| 609 | Ch_ij = Ch+i*dimx; |
---|
| 610 | |
---|
| 611 | for (j=0; j<i; j++) |
---|
| 612 | { |
---|
| 613 | |
---|
[1326] | 614 | if (*Ch_ij>0) |
---|
[1294] | 615 | { |
---|
[1326] | 616 | tmp_long=(int32)*Ch_ii**Ch_ii+(int32)*Ch_ij**Ch_ij; |
---|
[1294] | 617 | // rho=qsqrt(tmp_long); // verze pro DSP |
---|
[1326] | 618 | if (tmp_long>(1<<30)-1) |
---|
| 619 | rho=(1<<15)-1; |
---|
| 620 | else |
---|
| 621 | rho=(int16)(sqrt((double)tmp_long)); // verze pro PC |
---|
| 622 | |
---|
| 623 | s=(((int32)*Ch_ij)<<14)/rho; |
---|
| 624 | c=(((int32)*Ch_ii)<<14)/rho; |
---|
[1294] | 625 | |
---|
| 626 | Ch_kj = Ch + j; |
---|
| 627 | Ch_ki = Ch + i; |
---|
| 628 | |
---|
| 629 | for (k=0; k<=i; k++) |
---|
| 630 | { |
---|
[1326] | 631 | tau=((int32)c**Ch_kj-(int32)s**Ch_ki)>>14; |
---|
| 632 | tmp_long =((int32)s**Ch_kj+(int32)c**Ch_ki); |
---|
[1333] | 633 | if (tmp_long>(1<<29)) |
---|
| 634 | *Ch_ki = (1<<15)-1; |
---|
[1326] | 635 | else |
---|
| 636 | *Ch_ki=tmp_long>>14; |
---|
[1294] | 637 | *Ch_kj=tau; |
---|
| 638 | |
---|
| 639 | Ch_kj += dimx; |
---|
| 640 | Ch_ki += dimx; |
---|
| 641 | } |
---|
| 642 | } |
---|
| 643 | Ch_ij++; |
---|
| 644 | } |
---|
| 645 | } |
---|
| 646 | } |
---|
| 647 | |
---|
| 648 | void carlson_fast(int16 *difz, int16 *xp, int16 *Ch, int16 *R, unsigned int16 dimy, unsigned int16 dimx ) { |
---|
| 649 | int16 alpha,beta,gamma; |
---|
| 650 | int16 delta, eta,epsilon,zeta,sigma,tau; |
---|
| 651 | int16 i,j,iy; |
---|
| 652 | int16 w[5]; |
---|
| 653 | int32 tmp_long; |
---|
| 654 | |
---|
| 655 | int16 *Ch_ij, *w_i, *x_i; |
---|
| 656 | |
---|
| 657 | |
---|
| 658 | for (iy=0; iy<dimy; iy++) |
---|
| 659 | { |
---|
| 660 | alpha=R[iy]; |
---|
| 661 | delta = difz[iy]; |
---|
| 662 | |
---|
| 663 | for (j=0;j<dimx;j++) |
---|
| 664 | { |
---|
| 665 | sigma=Ch[iy*dimx+j]; |
---|
| 666 | beta=alpha; |
---|
| 667 | // alpha+=((long)sigma*sigma)>>15; |
---|
| 668 | alpha=(((int32)alpha<<15)+(int32)sigma*sigma)>>15; // vyssi presnost |
---|
| 669 | // gamma= qsqrt(((long)alpha*beta)); // verze pro DSP |
---|
| 670 | gamma= (int16)(sqrt((double)((int32)alpha*beta))); // verze pro PC |
---|
| 671 | |
---|
| 672 | w[j]=0; |
---|
| 673 | |
---|
| 674 | Ch_ij=Ch+j; |
---|
| 675 | w_i=w; |
---|
| 676 | |
---|
| 677 | for (i=0;i<=j;i++) |
---|
| 678 | { |
---|
| 679 | // tau=Ch[i*dimx+j]; |
---|
| 680 | tau=*Ch_ij; |
---|
| 681 | // tmp_long=((long)beta*Ch[i*dimx+j] -(long)sigma*w[i])/gamma; |
---|
| 682 | tmp_long=((int32)beta**Ch_ij -(int32)sigma**w_i)/gamma; |
---|
| 683 | |
---|
| 684 | if (tmp_long>32767) |
---|
| 685 | tmp_long=32767; |
---|
| 686 | if (tmp_long<-32768) |
---|
| 687 | tmp_long=-32768; |
---|
| 688 | *Ch_ij=tmp_long; |
---|
| 689 | |
---|
| 690 | // w_i+=((long)tau*sigma)>>15; |
---|
| 691 | *w_i=(((int32)*w_i<<15)+(int32)tau*sigma)>>15; |
---|
| 692 | |
---|
| 693 | w_i++; |
---|
| 694 | Ch_ij+=dimx; |
---|
| 695 | } |
---|
| 696 | } |
---|
| 697 | |
---|
| 698 | x_i=xp; |
---|
| 699 | w_i=w; |
---|
| 700 | for (i=0;i<dimx;i++) { |
---|
| 701 | // xp[i]+=((long)w[i]*delta)/alpha; |
---|
| 702 | // *x_i+=((long)*w_i*delta)/alpha; |
---|
| 703 | *x_i=((int32)*x_i*alpha+(int32)*w_i*delta)/alpha; // vyssi presnost |
---|
| 704 | x_i++; |
---|
| 705 | w_i++; |
---|
| 706 | } |
---|
| 707 | } |
---|
| 708 | } |
---|
| 709 | |
---|
[1321] | 710 | void carlson_fastC(int16 *difz, int16 *xp, int16 *Ch, int16 *C, int16 *R, unsigned int16 dimy, unsigned int16 dimx ) { |
---|
| 711 | int16 alpha,beta,gamma; |
---|
| 712 | int16 delta, eta,epsilon,zeta,sigma,tau; |
---|
| 713 | int16 i,j,iy; |
---|
| 714 | int16 w[5]; |
---|
| 715 | int32 tmp_long; |
---|
| 716 | |
---|
| 717 | int16 *Ch_ij, *w_i, *x_i, *C_yi; |
---|
| 718 | |
---|
| 719 | |
---|
| 720 | for (iy=0; iy<dimy; iy++) |
---|
| 721 | { |
---|
| 722 | alpha=R[iy]; |
---|
| 723 | delta = difz[iy]; |
---|
| 724 | |
---|
| 725 | for (j=0;j<dimx;j++) |
---|
| 726 | { |
---|
| 727 | C_yi = C+iy*dimx; |
---|
| 728 | sigma = 0; |
---|
| 729 | Ch_ij=Ch+j; |
---|
| 730 | for (i=0;i<=j;i++){ |
---|
| 731 | sigma += ((int32)*Ch_ij**C_yi)>>15; //sigma in qCh |
---|
| 732 | Ch_ij+=dimx; |
---|
| 733 | C_yi++; |
---|
| 734 | } |
---|
| 735 | |
---|
| 736 | //sigma=Ch[iy*dimx+j]; |
---|
[1326] | 737 | beta=alpha; // in q15 |
---|
[1321] | 738 | // alpha+=((long)sigma*sigma)>>15; |
---|
[1326] | 739 | tmp_long=((int32)alpha<<15)+(((int32)sigma*sigma)<<(30-2*qCh)); |
---|
[1329] | 740 | alpha=(tmp_long+(1<<14))>>15; // vyssi presnost |
---|
[1326] | 741 | |
---|
[1321] | 742 | // gamma= qsqrt(((long)alpha*beta)); // verze pro DSP |
---|
| 743 | gamma= (int16)(sqrt((double)((int32)alpha*beta))); // verze pro PC |
---|
[1326] | 744 | // in q15 |
---|
[1321] | 745 | w[j]=0; |
---|
| 746 | |
---|
| 747 | Ch_ij=Ch+j; |
---|
[1326] | 748 | w_i=w; // in q15 |
---|
[1321] | 749 | |
---|
| 750 | for (i=0;i<=j;i++) |
---|
| 751 | { |
---|
| 752 | // tau=Ch[i*dimx+j]; |
---|
| 753 | tau=*Ch_ij; |
---|
| 754 | // tmp_long=((long)beta*Ch[i*dimx+j] -(long)sigma*w[i])/gamma; |
---|
[1326] | 755 | tmp_long=((int32)beta**Ch_ij -(int32)sigma**w_i)/gamma; // in qCh |
---|
[1321] | 756 | |
---|
[1326] | 757 | /* if (tmp_long>32767) |
---|
[1321] | 758 | tmp_long=32767; |
---|
| 759 | if (tmp_long<-32768) |
---|
[1326] | 760 | tmp_long=-32768;*/ |
---|
[1321] | 761 | *Ch_ij=tmp_long; |
---|
| 762 | |
---|
| 763 | // w_i+=((long)tau*sigma)>>15; |
---|
[1326] | 764 | tmp_long = ((int32)*w_i<<15)+((int32)tau*sigma<<(30-2*qCh)); |
---|
[1329] | 765 | *w_i=(tmp_long+(1<<14))>>15; |
---|
[1321] | 766 | |
---|
| 767 | w_i++; |
---|
| 768 | Ch_ij+=dimx; |
---|
| 769 | } |
---|
| 770 | } |
---|
| 771 | |
---|
| 772 | x_i=xp; |
---|
| 773 | w_i=w; |
---|
| 774 | for (i=0;i<dimx;i++) { |
---|
| 775 | // xp[i]+=((long)w[i]*delta)/alpha; |
---|
| 776 | // *x_i+=((long)*w_i*delta)/alpha; |
---|
| 777 | *x_i=((int32)*x_i*alpha+(int32)*w_i*delta)/alpha; // vyssi presnost |
---|
| 778 | x_i++; |
---|
| 779 | w_i++; |
---|
| 780 | } |
---|
| 781 | } |
---|
| 782 | } |
---|