[1380] | 1 | #include "math.h" |
---|
| 2 | #include <stdio.h> |
---|
[1442] | 3 | #include "fast_exp.h" |
---|
| 4 | #include "fastexp.h" |
---|
| 5 | #include "mpf_double.h" |
---|
[1380] | 6 | |
---|
[1439] | 7 | static floatx om[N]; |
---|
| 8 | static floatx Pt[N]; |
---|
| 9 | static floatx sth[N]; |
---|
| 10 | static floatx cth[N]; |
---|
| 11 | static floatx isdm[N]; |
---|
| 12 | static floatx isqm[N]; |
---|
| 13 | static floatx th[N]; |
---|
| 14 | static floatx w[N]; |
---|
| 15 | static floatx lwi[N]; |
---|
[1380] | 16 | |
---|
[1439] | 17 | static floatx qth; |
---|
| 18 | static floatx qom; |
---|
| 19 | static floatx r; |
---|
[1442] | 20 | static floatx rinv; |
---|
[1380] | 21 | |
---|
[1468] | 22 | #if 1 |
---|
| 23 | #include "RArandn.h" |
---|
[1439] | 24 | floatx qrandu() { |
---|
[1380] | 25 | if ( randu_i==randu_last ) |
---|
| 26 | randu_i =&RArandu[0]; |
---|
| 27 | else |
---|
| 28 | randu_i++; |
---|
| 29 | |
---|
| 30 | return *randu_i; |
---|
| 31 | } |
---|
| 32 | |
---|
[1439] | 33 | floatx qrandn() { |
---|
[1380] | 34 | if ( randn_i==randn_last ) |
---|
| 35 | randn_i =&RArandn[0]; |
---|
| 36 | else |
---|
| 37 | randn_i++; |
---|
| 38 | |
---|
| 39 | return *randn_i; |
---|
| 40 | } |
---|
| 41 | |
---|
[1468] | 42 | void rng_init() |
---|
| 43 | { |
---|
| 44 | randu_i=&RArandu[0]; |
---|
| 45 | randn_i=&RArandn[0]; |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | #else |
---|
| 49 | |
---|
| 50 | #include "rnor.h" |
---|
| 51 | floatx qrandu() { |
---|
| 52 | return UNI; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | floatx qrandn() { |
---|
| 56 | return PolarRNG(); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | void rng_init() |
---|
| 60 | { |
---|
| 61 | zigset(86947731 ); |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | #endif |
---|
| 66 | |
---|
| 67 | |
---|
[1380] | 68 | void resample() { |
---|
| 69 | int N_babies[N]; |
---|
[1439] | 70 | floatx cumdist; |
---|
[1380] | 71 | int i; |
---|
[1439] | 72 | floatx ui; |
---|
| 73 | int i_from=0; |
---|
| 74 | int i_to=0; |
---|
[1380] | 75 | |
---|
[1439] | 76 | floatx u0 = qrandu(); |
---|
[1380] | 77 | |
---|
| 78 | int j=0; |
---|
| 79 | N_babies[0]=0; |
---|
| 80 | cumdist = w[0]; |
---|
| 81 | for ( i = 0; i < N; i++ ) { |
---|
| 82 | ui = ( i + u0 ) / N; |
---|
| 83 | while ( ui > cumdist ) { |
---|
| 84 | j++; |
---|
| 85 | N_babies[j]=0; |
---|
| 86 | cumdist+=w[j]; |
---|
| 87 | } |
---|
| 88 | N_babies [j] ++; |
---|
| 89 | } |
---|
| 90 | while (j++<=(N-1)) { // delete all N_babies after j |
---|
| 91 | N_babies[j]=0; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | // COPY |
---|
[1439] | 95 | |
---|
[1380] | 96 | while ( i_from<N ) { |
---|
| 97 | while ( N_babies[i_from]>1 ) { // 1 baby stays where it is |
---|
| 98 | |
---|
| 99 | while ( N_babies[i_to]>=1 ) i_to++; // find first empty slot |
---|
| 100 | |
---|
| 101 | //copy it |
---|
| 102 | Pt[i_to]=Pt[i_from]; |
---|
| 103 | om[i_to]=om[i_from]; |
---|
| 104 | th[i_to]=th[i_from]; |
---|
| 105 | isdm[i_to]=isdm[i_from]; |
---|
| 106 | isqm[i_to]=isqm[i_from]; |
---|
| 107 | |
---|
| 108 | N_babies[i_to]++; |
---|
| 109 | N_babies[i_from]--; |
---|
| 110 | } |
---|
| 111 | i_from++; |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | |
---|
[1439] | 115 | void mpf_bayes ( const floatx isa, const floatx isb , const floatx usa, const floatx usb ) { |
---|
[1380] | 116 | |
---|
[1439] | 117 | floatx isd; |
---|
| 118 | floatx isq; |
---|
| 119 | floatx usd; |
---|
| 120 | floatx usq; |
---|
[1380] | 121 | |
---|
[1439] | 122 | floatx Cd; |
---|
| 123 | floatx Cq; |
---|
| 124 | floatx CC; |
---|
| 125 | floatx oCC; |
---|
[1380] | 126 | |
---|
[1439] | 127 | floatx difid; |
---|
| 128 | floatx difiq; |
---|
[1380] | 129 | |
---|
[1439] | 130 | floatx zeta; |
---|
| 131 | floatx Kd; |
---|
| 132 | floatx Kq; |
---|
| 133 | floatx ro; |
---|
| 134 | floatx ypd; |
---|
| 135 | floatx ypq; |
---|
| 136 | floatx detRy; |
---|
| 137 | floatx ydiffd; |
---|
| 138 | floatx ydiffq; |
---|
| 139 | floatx ydC; |
---|
[1380] | 140 | |
---|
[1439] | 141 | floatx maxlwi, sumlwi; |
---|
[1380] | 142 | int i; |
---|
[1439] | 143 | |
---|
| 144 | floatx *th_i, *Pt_i, *om_i, *isdm_i, *isqm_i; |
---|
| 145 | floatx *w_i, *lw_i; |
---|
| 146 | floatx *sth_i, *cth_i; |
---|
[1380] | 147 | // implementation starts here |
---|
[1439] | 148 | th_i = &th[0]; |
---|
| 149 | Pt_i = &Pt[0]; |
---|
| 150 | om_i = &om[0]; |
---|
| 151 | isdm_i = &isdm[0]; |
---|
| 152 | isqm_i = &isqm[0]; |
---|
| 153 | sth_i = &sth[0]; |
---|
| 154 | cth_i = &cth[0]; |
---|
[1380] | 155 | for ( i=0; i<N; i++ ) { |
---|
| 156 | |
---|
[1439] | 157 | // time update |
---|
| 158 | *Pt_i = 1.0*1.0*(*Pt_i)+qom; // Pt is now predictive variance |
---|
| 159 | *th_i += *om_i*_dt+qth * qrandn() ; |
---|
| 160 | while ( *th_i>M_PI ) *th_i -=2*M_PI; |
---|
| 161 | while ( *th_i<-M_PI ) *th_i+=2*M_PI; |
---|
[1380] | 162 | |
---|
[1439] | 163 | *sth_i=sin ( *th_i ); |
---|
| 164 | *cth_i=cos ( *th_i ); |
---|
| 165 | |
---|
| 166 | isd = *cth_i*isa+*sth_i*isb; |
---|
| 167 | isq = -*sth_i*isa+*cth_i*isb; |
---|
| 168 | // process old voltage |
---|
| 169 | usd = (*cth_i)*usa+(*sth_i)*usb; |
---|
| 170 | usq = -(*sth_i)*usa+(*cth_i)*usb; |
---|
[1380] | 171 | |
---|
| 172 | Cd = isq*_dt; |
---|
| 173 | Cq = -_b - isd* _dt; |
---|
| 174 | |
---|
[1439] | 175 | difid=isd- _ad * (*isdm_i) - _cd *(usd); |
---|
| 176 | difiq=isq- _aq * (*isqm_i) - _cq *(usq); |
---|
[1380] | 177 | |
---|
| 178 | CC=Cd*Cd+Cq*Cq; |
---|
[1439] | 179 | zeta = *Pt_i/ ( r+*Pt_i*CC ); |
---|
[1380] | 180 | oCC = ( 1-zeta*CC ); |
---|
[1442] | 181 | ro = oCC*rinv; |
---|
[1380] | 182 | |
---|
[1439] | 183 | Kd = *Pt_i*Cd*ro; |
---|
| 184 | Kq = *Pt_i*Cq*ro; |
---|
[1380] | 185 | |
---|
[1440] | 186 | (*Pt_i)*=( 1.0- ( Kd*Cd+Kq*Cq ) ); |
---|
[1380] | 187 | |
---|
[1439] | 188 | ypd = Cd**om_i; |
---|
| 189 | ypq = Cq**om_i; |
---|
[1380] | 190 | |
---|
[1439] | 191 | //detRy = ro/r; |
---|
| 192 | *om_i += Kd* ( difid - ypd ) +Kq* ( difiq-ypq ); |
---|
[1380] | 193 | ydiffd = ( ypd-difid ); |
---|
| 194 | ydiffq = ( ypq-difiq ); |
---|
| 195 | ydC = ydiffd*Cd + ydiffq*Cq; |
---|
| 196 | |
---|
[1439] | 197 | //lwi[i] = 0.5* ( log ( detRy ) + ( ydC*ydC*zeta -(ydiffd*ydiffd+ydiffq*ydiffq) ) /r ) ; |
---|
[1442] | 198 | lwi[i] = 0.5*( ydC*ydC*zeta -(ydiffd*ydiffd+ydiffq*ydiffq) ) *rinv ; |
---|
[1380] | 199 | |
---|
[1439] | 200 | *isdm_i=isd; |
---|
| 201 | *isqm_i=isq; |
---|
| 202 | |
---|
| 203 | // shift all counters |
---|
| 204 | th_i++; |
---|
| 205 | Pt_i++; |
---|
| 206 | om_i++; |
---|
| 207 | isdm_i++; |
---|
| 208 | isqm_i++; |
---|
| 209 | sth_i++; |
---|
| 210 | cth_i++; |
---|
| 211 | |
---|
[1380] | 212 | } |
---|
[1440] | 213 | /* maxlwi=-1e10; |
---|
[1439] | 214 | lw_i = &lwi[0]; |
---|
| 215 | for ( i=0;i<N;i++,lw_i++ ) { |
---|
| 216 | if ( *lw_i>maxlwi ) maxlwi=*lw_i; |
---|
[1440] | 217 | }*/ |
---|
[1439] | 218 | lw_i = &lwi[0]; |
---|
| 219 | w_i = &w[0]; |
---|
| 220 | for ( i=0;i<N;i++,lw_i++,w_i++ ) { |
---|
[1440] | 221 | // *lw_i-=maxlwi; |
---|
[1442] | 222 | (*w_i)=fasterexp ( *lw_i ); // always resample -> wmin=1/n; |
---|
[1439] | 223 | } |
---|
[1380] | 224 | |
---|
| 225 | sumlwi=0.0; |
---|
[1439] | 226 | w_i = &w[0]; |
---|
| 227 | for ( i=0;i<N;i++,w_i++ ) sumlwi+=*w_i; |
---|
| 228 | w_i = &w[0]; |
---|
| 229 | for ( i=0;i<N;i++,w_i++ ) *w_i/=sumlwi; |
---|
| 230 | |
---|
| 231 | // NEFF |
---|
| 232 | floatx sw2=0.0; |
---|
| 233 | for ( i=0;i<N;i++ ) sw2+=w[i]*w[i]; |
---|
| 234 | printf("%f\n",1./sw2*N); |
---|
| 235 | |
---|
[1380] | 236 | resample(); |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | |
---|
[1439] | 240 | void mpf_init(floatx qom0, floatx qth0, floatx r0) { |
---|
[1468] | 241 | |
---|
| 242 | rng_init(); |
---|
| 243 | |
---|
[1439] | 244 | int i; |
---|
[1380] | 245 | r=r0; |
---|
[1442] | 246 | rinv = 1.0/r; |
---|
[1380] | 247 | qth=qth0; |
---|
| 248 | qom=qom0; |
---|
[1439] | 249 | |
---|
[1380] | 250 | for ( i=0; i<N; i++ ) { |
---|
[1439] | 251 | th[i]=qrandu() *2*M_PI - M_PI; |
---|
| 252 | sth[i]=sin ( th[i] ); |
---|
| 253 | cth[i]=cos ( th[i] ); |
---|
| 254 | |
---|
[1380] | 255 | om[i]=0; |
---|
[1439] | 256 | Pt[i]=10.; |
---|
[1380] | 257 | isdm[i]=0; |
---|
| 258 | isqm[i]=0; |
---|
| 259 | w[i]=1.0/N; |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | } |
---|
[1439] | 263 | void mpf_mean ( floatx *Ecosth, floatx *Esinth, floatx *Eome ) { |
---|
[1380] | 264 | int i; |
---|
[1440] | 265 | floatx *w_i=&w[0]; |
---|
| 266 | floatx *cth_i=&cth[0]; |
---|
| 267 | floatx *sth_i=&sth[0]; |
---|
| 268 | floatx *om_i=&om[0]; |
---|
[1381] | 269 | *Ecosth=0.0; |
---|
| 270 | *Esinth=0.0; |
---|
| 271 | *Eome=0.0; |
---|
[1440] | 272 | for ( i=0;i<N;i++,w_i++,cth_i++,sth_i++,om_i++ ) { |
---|
| 273 | *Ecosth+=(*w_i)*(*cth_i); |
---|
| 274 | *Esinth+=(*w_i)*(*sth_i); |
---|
| 275 | *Eome+=(*w_i)*(*om_i); |
---|
[1380] | 276 | } |
---|
| 277 | |
---|
| 278 | } |
---|
[1439] | 279 | void mpf_th(floatx th1[N]){ |
---|
| 280 | int i; |
---|
| 281 | for (i=0;i<N;i++) th1[i]=th[i]; |
---|
[1380] | 282 | } |
---|
[1468] | 283 | void mpf_om(floatx om1[N]){ |
---|
| 284 | int i; |
---|
| 285 | for (i=0;i<N;i++) om1[i]=om[i]; |
---|
| 286 | } |
---|
[1438] | 287 | |
---|
[1439] | 288 | floatx kalman_om( const floatx isa, const floatx isb , const floatx usa, const floatx usb, const floatx th ) { |
---|
[1438] | 289 | |
---|
[1439] | 290 | floatx isd; |
---|
| 291 | floatx isq; |
---|
| 292 | floatx usd; |
---|
| 293 | floatx usq; |
---|
[1438] | 294 | |
---|
[1439] | 295 | floatx Cd; |
---|
| 296 | floatx Cq; |
---|
| 297 | floatx CC; |
---|
| 298 | floatx oCC; |
---|
[1438] | 299 | |
---|
[1439] | 300 | floatx difid; |
---|
| 301 | floatx difiq; |
---|
[1438] | 302 | |
---|
[1439] | 303 | floatx zeta; |
---|
| 304 | floatx Kd; |
---|
| 305 | floatx Kq; |
---|
| 306 | floatx ro; |
---|
| 307 | floatx ypd; |
---|
| 308 | floatx ypq; |
---|
| 309 | floatx detRy; |
---|
| 310 | floatx ydiffd; |
---|
| 311 | floatx ydiffq; |
---|
| 312 | floatx ydC; |
---|
[1438] | 313 | |
---|
[1439] | 314 | static floatx Pt; |
---|
| 315 | static floatx om; |
---|
| 316 | floatx cth, sth; |
---|
| 317 | static floatx isdm, isqm; |
---|
[1438] | 318 | |
---|
| 319 | |
---|
| 320 | Pt = 1.0*1.0*Pt+qom; // Pt is now predictive variance |
---|
| 321 | //while ( th>M_PI ) th=th-2*M_PI; |
---|
| 322 | //while ( th<-M_PI ) th=th+2*M_PI; |
---|
| 323 | |
---|
| 324 | sth=sin ( th ); |
---|
| 325 | cth=cos ( th ); |
---|
| 326 | |
---|
| 327 | isd = cth*isa+sth*isb; |
---|
| 328 | isq = -sth*isa+cth*isb; |
---|
| 329 | usd = cth*usa+sth*usb; |
---|
| 330 | usq = -sth*usa+cth*usb; |
---|
| 331 | |
---|
| 332 | Cd = isq*_dt; |
---|
| 333 | Cq = -_b - isd* _dt; |
---|
| 334 | |
---|
| 335 | difid=isd- _ad *isdm - _cd *usd; |
---|
| 336 | difiq=isq- _aq *isqm - _cq *usq; |
---|
| 337 | |
---|
| 338 | CC=Cd*Cd+Cq*Cq; |
---|
| 339 | zeta = Pt/ ( r+Pt*CC ); |
---|
| 340 | oCC = ( 1-zeta*CC ); |
---|
[1442] | 341 | ro = oCC*rinv; |
---|
[1438] | 342 | |
---|
| 343 | Kd = Pt*Cd*ro; |
---|
| 344 | Kq = Pt*Cq*ro; |
---|
| 345 | |
---|
| 346 | Pt=Pt* ( 1- ( Kd*Cd+Kq*Cq ) ); |
---|
| 347 | |
---|
| 348 | ypd = Cd*om; |
---|
| 349 | ypq = Cq*om; |
---|
| 350 | |
---|
[1442] | 351 | detRy = ro*rinv; |
---|
[1438] | 352 | om = om + Kd* ( difid - ypd ) +Kq* ( difiq-ypq ); |
---|
| 353 | ydiffd = ( ypd-difid ); |
---|
| 354 | ydiffq = ( ypq-difiq ); |
---|
| 355 | ydC = ydiffd*Cd + ydiffq*Cq; |
---|
| 356 | |
---|
| 357 | isdm=isd; |
---|
| 358 | isqm=isq; |
---|
| 359 | |
---|
| 360 | return om; |
---|
| 361 | } |
---|