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