yafl.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  1. /*******************************************************************************
  2. Copyright 2021 anonimous <shkolnick-kun@gmail.com> and contributors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing,
  8. software distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions
  11. and limitations under the License.
  12. ******************************************************************************/
  13. #ifndef YAFL_H
  14. #define YAFL_H
  15. #include <yafl_config.h>
  16. #include "yafl_math.h"
  17. /*=============================================================================
  18. Basic UD-factorized Kalman filter Definitions
  19. =============================================================================*/
  20. typedef struct _yaflKalmanBaseSt yaflKalmanBaseSt;
  21. typedef yaflStatusEn (* yaflKalmanFuncP)(yaflKalmanBaseSt *, yaflFloat *, \
  22. yaflFloat *);
  23. typedef yaflStatusEn (* yaflKalmanResFuncP)(yaflKalmanBaseSt *, yaflFloat *, \
  24. yaflFloat *, yaflFloat *);
  25. typedef yaflStatusEn (* yaflKalmanScalarUpdateP)(yaflKalmanBaseSt *, yaflInt);
  26. /*
  27. Function pointer for robust versions of filters.
  28. Based on:
  29. 1. West M., "Robust Sequential Approximate Bayesian Estimation",
  30. J. R. Statist. Soc. B (1981), 43, No. 2, pp. 157-166
  31. 2. Gaver, Donald Paul; Jacobs, Patricia A., "Robustifying the Kalman filter",
  32. Naval Postgraduate School technical report. 1987
  33. http://hdl.handle.net/10945/30147
  34. */
  35. typedef yaflFloat (* yaflKalmanRobFuncP)(yaflKalmanBaseSt *, yaflFloat);
  36. struct _yaflKalmanBaseSt {
  37. yaflKalmanFuncP f; /*A state transition function*/
  38. yaflKalmanFuncP h; /*A measurement function*/
  39. yaflKalmanResFuncP zrf; /*Measurement residual function function*/
  40. yaflFloat * x; /*State vector*/
  41. yaflFloat * y; /*Innovation vector*/
  42. yaflFloat * Up; /*Upper triangular part of P*/
  43. yaflFloat * Dp; /*Diagonal part of P*/
  44. yaflFloat * Uq; /*Upper triangular part of Q*/
  45. yaflFloat * Dq; /*Diagonal part of Q*/
  46. yaflFloat * Ur; /*Upper triangular part of R*/
  47. yaflFloat * Dr; /*Diagonal part of R*/
  48. yaflFloat * l; /*likelihood*/
  49. yaflFloat rff; /*R forgetting factor*/
  50. yaflInt Nx; /*State vector size*/
  51. yaflInt Nz; /*Measurement vector size*/
  52. };
  53. /*---------------------------------------------------------------------------*/
  54. #define YAFL_KALMAN_BASE_MEMORY_MIXIN(nx, nz) \
  55. yaflFloat x[nx]; \
  56. yaflFloat y[nz]; \
  57. \
  58. yaflFloat Up[((nx - 1) * nx)/2]; \
  59. yaflFloat Dp[nx]; \
  60. \
  61. yaflFloat Uq[((nx - 1) * nx)/2]; \
  62. yaflFloat Dq[nx]; \
  63. \
  64. yaflFloat Ur[((nz - 1) * nz)/2]; \
  65. yaflFloat Dr[nz]; \
  66. yaflFloat l
  67. /*---------------------------------------------------------------------------*/
  68. /*TODO: make qff and rff parameters*/
  69. #define YAFL_KALMAN_BASE_INITIALIZER(_f, _h, _zrf, _nx, _nz, _rff, _mem) \
  70. { \
  71. .f = (yaflKalmanFuncP)_f, \
  72. .h = (yaflKalmanFuncP)_h, \
  73. .zrf = (yaflKalmanResFuncP)_zrf, \
  74. \
  75. .x = _mem.x, \
  76. .y = _mem.y, \
  77. \
  78. .Up = _mem.Up, \
  79. .Dp = _mem.Dp, \
  80. \
  81. .Uq = _mem.Uq, \
  82. .Dq = _mem.Dq, \
  83. \
  84. .Ur = _mem.Ur, \
  85. .Dr = _mem.Dr, \
  86. \
  87. .rff = _rff, \
  88. \
  89. .l = &_mem.l, \
  90. \
  91. .Nx = _nx, \
  92. .Nz = _nz \
  93. }
  94. /*---------------------------------------------------------------------------*/
  95. #define YAFL_KALMAN_PREDICT_WRAPPER(predict, base_type, func, self_type) \
  96. static inline yaflStatusEn func(self_type * self) \
  97. { \
  98. return predict((base_type *)self); \
  99. }
  100. /*---------------------------------------------------------------------------*/
  101. #define YAFL_KALMAN_UPDATE_IMPL(update, base_type, func, self_type) \
  102. extern yaflStatusEn func##_scalar(yaflKalmanBaseSt * self, yaflInt i); \
  103. static inline yaflStatusEn func(self_type * self, yaflFloat * z) \
  104. { \
  105. return update((base_type *)self, z, func##_scalar); \
  106. }
  107. /*=============================================================================
  108. Basic UD-factorized EKF definitions
  109. =============================================================================*/
  110. typedef struct _yaflEKFBaseSt yaflEKFBaseSt;
  111. struct _yaflEKFBaseSt {
  112. yaflKalmanBaseSt base; /*Base type*/
  113. yaflKalmanFuncP jf; /*Jacobian of a state transition function*/
  114. yaflKalmanFuncP jh; /*Jacobian of a measurement function*/
  115. yaflFloat * H; /*Measurement Jacobian values*/
  116. yaflFloat * W; /*Scratchpad memory block matrix*/
  117. yaflFloat * D; /*Scratchpad memory diagonal matrix*/
  118. yaflFloat qff; /*Q forgetting factor*/
  119. };
  120. /*---------------------------------------------------------------------------*/
  121. #define YAFL_EKF_BASE_MEMORY_MIXIN(nx, nz) \
  122. YAFL_KALMAN_BASE_MEMORY_MIXIN(nx, nz); \
  123. \
  124. yaflFloat H[nz * nx]; \
  125. union { \
  126. yaflFloat x[2 * nx * nx]; \
  127. yaflFloat z[(nx + nz) * nz]; \
  128. } W; \
  129. union { \
  130. yaflFloat x[2 * nx]; \
  131. yaflFloat z[nx + nz]; \
  132. } D
  133. /*---------------------------------------------------------------------------*/
  134. #define YAFL_EKF_BASE_INITIALIZER(_f, _jf, _h, _jh, _zrf, _nx, _nz, \
  135. _rff, _qff, _mem) \
  136. { \
  137. .base = YAFL_KALMAN_BASE_INITIALIZER(_f, _h, _zrf, _nx, _nz, _rff, _mem), \
  138. \
  139. .jf = (yaflKalmanFuncP)_jf, \
  140. .jh = (yaflKalmanFuncP)_jh, \
  141. \
  142. .H = _mem.H, \
  143. .W = _mem.W.x, \
  144. .D = _mem.D.x, \
  145. \
  146. .qff = _qff \
  147. }
  148. /*---------------------------------------------------------------------------*/
  149. yaflStatusEn yafl_ekf_base_predict(yaflKalmanBaseSt * self);
  150. yaflStatusEn yafl_ekf_base_update(yaflKalmanBaseSt * self, yaflFloat * z, \
  151. yaflKalmanScalarUpdateP scalar_update);
  152. /*---------------------------------------------------------------------------*/
  153. #define YAFL_EKF_PREDICT_WRAPPER(func, self_type) \
  154. YAFL_KALMAN_PREDICT_WRAPPER(yafl_ekf_base_predict, yaflKalmanBaseSt, \
  155. func, self_type)
  156. /*---------------------------------------------------------------------------*/
  157. #define YAFL_EKF_UPDATE_IMPL(func, self_type) \
  158. YAFL_KALMAN_UPDATE_IMPL(yafl_ekf_base_update, yaflKalmanBaseSt, \
  159. func, self_type)
  160. /*---------------------------------------------------------------------------*/
  161. YAFL_EKF_PREDICT_WRAPPER(_yafl_ekf_predict_wrapper, yaflEKFBaseSt)
  162. /*-----------------------------------------------------------------------------
  163. Bierman filter
  164. -----------------------------------------------------------------------------*/
  165. #define YAFL_EKF_BIERMAN_PREDICT _yafl_ekf_predict_wrapper
  166. YAFL_EKF_UPDATE_IMPL(yafl_ekf_bierman_update, yaflEKFBaseSt)
  167. /*-----------------------------------------------------------------------------
  168. Joseph filter
  169. -----------------------------------------------------------------------------*/
  170. #define YAFL_EKF_JOSEPH_PREDICT _yafl_ekf_predict_wrapper
  171. YAFL_EKF_UPDATE_IMPL(yafl_ekf_joseph_update, yaflEKFBaseSt)
  172. /*=============================================================================
  173. Adaptive UD-factorized EKF definitions
  174. =============================================================================*/
  175. typedef struct {
  176. yaflEKFBaseSt base;
  177. yaflFloat chi2; /*Divergence test threshold (chi-squared criteria)*/
  178. } yaflEKFAdaptiveSt; /*Adaptive kKalman-Hinfinity filter structure*/
  179. /*---------------------------------------------------------------------------*/
  180. #define YAFL_EKF_ADAPTIVE_INITIALIZER(_f, _jf, _h, _jh, _zrf, _nx, _nz, \
  181. _rff, _qff, _chi2, _mem) \
  182. { \
  183. .base = YAFL_EKF_BASE_INITIALIZER(_f, _jf, _h, _jh, _zrf, _nx, _nz, \
  184. _rff, _qff, _mem), \
  185. .chi2 = _chi2 \
  186. }
  187. /*
  188. A good value of chi2 for yaflEKFAdaptiveSt is:
  189. scipy.stats.chi2.ppf(0.999, 1) == 10.827566170662733
  190. */
  191. /*---------------------------------------------------------------------------*/
  192. YAFL_EKF_PREDICT_WRAPPER(_yafl_ada_ekf_predict_wrapper, yaflEKFAdaptiveSt)
  193. /*-----------------------------------------------------------------------------
  194. Adaptive Bierman filter
  195. -----------------------------------------------------------------------------*/
  196. #define YAFL_EKF_ADAPTIVE_BIERAMN_PREDICT _yafl_ada_ekf_predict_wrapper
  197. YAFL_EKF_UPDATE_IMPL(yafl_ekf_adaptive_bierman_update, yaflEKFAdaptiveSt)
  198. /*-----------------------------------------------------------------------------
  199. Adaptive Joseph filter
  200. -----------------------------------------------------------------------------*/
  201. #define YAFL_EKF_ADAPTIVE_JOSEPH_PREDICT _yafl_ada_ekf_predict_wrapper
  202. YAFL_EKF_UPDATE_IMPL(yafl_ekf_adaptive_joseph_update, yaflEKFAdaptiveSt)
  203. /*-----------------------------------------------------------------------------
  204. WARNING!!!
  205. DO NOT USE THIS variant of Adaptive Joseph filter !!!
  206. It was implemented to show some flaws of the corresponding algorithm!
  207. -----------------------------------------------------------------------------*/
  208. yaflStatusEn \
  209. yafl_ekf_do_not_use_this_update_scalar(yaflKalmanBaseSt * self, yaflInt i);
  210. static inline yaflStatusEn \
  211. yafl_ekf_do_not_use_this_update(yaflEKFAdaptiveSt * self, yaflFloat * z)
  212. {
  213. return yafl_ekf_base_update((yaflKalmanBaseSt *)self, z, \
  214. yafl_ekf_do_not_use_this_update_scalar);
  215. }
  216. /*=============================================================================
  217. Robust UD-factorized EKF definitions
  218. =============================================================================*/
  219. /*
  220. Based on:
  221. 1. West M., "Robust Sequential Approximate Bayesian Estimation",
  222. J. R. Statist. Soc. B (1981), 43, No. 2, pp. 157-166
  223. 2. Gaver, Donald Paul; Jacobs, Patricia A., "Robustifying the Kalman filter",
  224. Naval Postgraduate School technical report. 1987
  225. http://hdl.handle.net/10945/30147
  226. */
  227. typedef struct {
  228. yaflEKFBaseSt base;
  229. yaflKalmanRobFuncP g; /* g = -d(ln(pdf(y))) / dy */
  230. yaflKalmanRobFuncP gdot; /* gdot = G = d(g) / dy */
  231. } yaflEKFRobustSt; /*Robust EKF*/
  232. /*---------------------------------------------------------------------------*/
  233. #define YAFL_EKF_ROBUST_INITIALIZER(_f, _jf, _h, _jh, _zrf, _g, _gdot, \
  234. _nx, _nz, _rff, _qff, _mem) \
  235. { \
  236. .base = YAFL_EKF_BASE_INITIALIZER(_f, _jf, _h, _jh, _zrf, _nx, _nz, \
  237. _rff, _qff, _mem), \
  238. .g = _g, \
  239. .gdot = _gdot \
  240. }
  241. /*---------------------------------------------------------------------------*/
  242. YAFL_EKF_PREDICT_WRAPPER(_yafl_rob_ekf_predict_wrapper, yaflEKFRobustSt)
  243. /*-----------------------------------------------------------------------------
  244. Robust Bierman filter
  245. -----------------------------------------------------------------------------*/
  246. #define YAFL_EKF_ROBUST_BIERAMN_PREDICT _yafl_rob_ekf_predict_wrapper
  247. YAFL_EKF_UPDATE_IMPL(yafl_ekf_robust_bierman_update, yaflEKFRobustSt)
  248. /*-----------------------------------------------------------------------------
  249. Robust Joseph filter
  250. -----------------------------------------------------------------------------*/
  251. #define YAFL_EKF_ROBUST_JOSEPH_PREDICT _yafl_rob_ekf_predict_wrapper
  252. YAFL_EKF_UPDATE_IMPL(yafl_ekf_robust_joseph_update, yaflEKFRobustSt)
  253. /*=============================================================================
  254. Adaptive robust UD-factorized EKF definitions
  255. =============================================================================*/
  256. typedef struct {
  257. yaflEKFRobustSt base;
  258. yaflFloat chi2; /*Divergence test threshold (chi-squared criteria)*/
  259. } yaflEKFAdaptiveRobustSt; /*Robust EKF*/
  260. /*---------------------------------------------------------------------------*/
  261. #define YAFL_EKF_ADAPTIVE_ROBUST_INITIALIZER(_f, _jf, _h, _jh, _zrf, _g, _gdot, \
  262. _nx, _nz, _rff, _qff, _chi2, _mem) \
  263. { \
  264. .base = YAFL_EKF_ROBUST_INITIALIZER(_f, _jf, _h, _jh, _zrf, _g, _gdot, \
  265. _nx, _nz, _rff, _qff, _mem), \
  266. .chi2 = _chi2 \
  267. }
  268. /*
  269. A good value of chi2 for yaflEKFAdaptiveRobustSt is:
  270. scipy.stats.chi2.ppf(0.997, 1) == 8.807468393511947
  271. */
  272. /*---------------------------------------------------------------------------*/
  273. YAFL_EKF_PREDICT_WRAPPER(_yafl_ada_rob_predict_wrapper, \
  274. yaflEKFAdaptiveRobustSt)
  275. /*-----------------------------------------------------------------------------
  276. Adaptive Bierman filter
  277. -----------------------------------------------------------------------------*/
  278. #define YAFL_EKF_ADAPTIVE_ROBUST_BIERAMN_PREDICT _yafl_ada_rob_predict_wrapper
  279. YAFL_EKF_UPDATE_IMPL(yafl_ekf_adaptive_robust_bierman_update, \
  280. yaflEKFAdaptiveRobustSt)
  281. /*-----------------------------------------------------------------------------
  282. Adaptive Joseph filter
  283. -----------------------------------------------------------------------------*/
  284. #define YAFL_EKF_ADAPTIVE_ROBUST_JOSEPH_PREDICT \
  285. _yafl_ada_rob_predict_wrapper
  286. YAFL_EKF_UPDATE_IMPL(yafl_ekf_adaptive_robust_joseph_update, \
  287. yaflEKFAdaptiveRobustSt)
  288. /*=============================================================================
  289. Basic UD-factorized UKF definitions
  290. =============================================================================*/
  291. typedef struct _yaflUKFBaseSt yaflUKFBaseSt; /* The UKF base type */
  292. /*
  293. Used to add delta vectors to initial point in sigma point generation.
  294. Parameters:
  295. yaflFloat * x0
  296. yaflFloat * delta_x
  297. yaflFloat factor
  298. Does:
  299. delta_x = x0 + factor * delta_x
  300. */
  301. typedef yaflStatusEn (* yaflUKFSigmaAddP)(yaflUKFBaseSt *, yaflFloat *, \
  302. yaflFloat *, yaflFloat);
  303. /*Sigma point generator info base type*/
  304. typedef struct _yaflUKFSigmaSt {
  305. yaflInt np; /* The number of sigma points */
  306. yaflUKFSigmaAddP addf; /* Sigma point addition function */
  307. } yaflUKFSigmaSt;
  308. /*---------------------------------------------------------------------------*/
  309. #define YAFL_UKF_SIGMA_BASE_INITIALIZER(_np, _addf, _mem) \
  310. { \
  311. .np = (yaflInt)_np, \
  312. .addf = (yaflUKFSigmaAddP)_addf, \
  313. }
  314. /*---------------------------------------------------------------------------*/
  315. /* Computes sigma points weights */
  316. typedef yaflStatusEn (* yafkUKFSigmaGenWeigthsP)(yaflUKFBaseSt *);
  317. /* Generates sigma points */
  318. typedef yaflStatusEn (* yaflUKFSigmaGenSigmasP)(yaflUKFBaseSt *);
  319. typedef struct _yaflUKFSigmaMethodsSt {
  320. yafkUKFSigmaGenWeigthsP wf; /* Weight function */
  321. yaflUKFSigmaGenSigmasP spgf; /* Sigma point generator function */
  322. } yaflUKFSigmaMethodsSt;
  323. /*---------------------------------------------------------------------------*/
  324. struct _yaflUKFBaseSt {
  325. yaflKalmanBaseSt base;
  326. /* A pointer to the sigma point generator structure */
  327. yaflUKFSigmaSt * sp_info;
  328. /* A sigma point generator method table pointer */
  329. const yaflUKFSigmaMethodsSt * sp_meth;
  330. yaflKalmanFuncP xmf; /* State mean function */
  331. yaflKalmanResFuncP xrf; /* State residual function function */
  332. yaflKalmanFuncP zmf; /* Measurement mean function function */
  333. yaflFloat * zp; /* Predicted measurement vector */
  334. /*Scratchpad memory*/
  335. yaflFloat * Sx; /* State */
  336. yaflFloat * Sz; /* Measurement */
  337. yaflFloat * Pzx; /* Pzx cross covariance matrix */
  338. yaflFloat * sigmas_x; /* State sigma points */
  339. yaflFloat * sigmas_z; /* Measurement sigma points */
  340. yaflFloat * wm; /* Weights for mean calculations */
  341. yaflFloat * wc; /* Weights for covariance calculations */
  342. };
  343. /*---------------------------------------------------------------------------*/
  344. /*
  345. Warning: sigmas_x and _sigmas_z aren't defined in this mixin, see
  346. sigma points generators mixins!!!
  347. */
  348. #define YAFL_UKF_BASE_MEMORY_MIXIN(nx, nz) \
  349. YAFL_KALMAN_BASE_MEMORY_MIXIN(nx, nz); \
  350. yaflFloat zp[nz]; \
  351. yaflFloat Sx[nx]; \
  352. yaflFloat Sz[nz]; \
  353. yaflFloat Pzx[nz * nx]
  354. /*---------------------------------------------------------------------------*/
  355. /*
  356. Sigma point memory mixin.
  357. WARNING:
  358. 1. _SIGMAS_X and _SIGMAS_Z must be the parts of some larger
  359. memory pool which has minimum size of (nx + nz) * (nz + 1)
  360. or np * (nx + nz) where np is number of sigma points
  361. 2. _SIGMAS_X must be at start of this pool
  362. */
  363. #define YAFL_UKF_SP_MEMORY_MIXIN(np, nx, nz) \
  364. yaflFloat wm[np]; \
  365. yaflFloat wc[np]; \
  366. union{ \
  367. struct { \
  368. yaflFloat x[(np) * nx]; \
  369. yaflFloat z[(np) * nz]; \
  370. } sigmas; \
  371. yaflFloat r_update_buf[(nx + nz) * (nz + 1)]; \
  372. } pool
  373. /*---------------------------------------------------------------------------*/
  374. #define YAFL_UKF_BASE_INITIALIZER(_p, _pm, _f, _xmf, _xrf, _h, _zmf, _zrf, \
  375. _nx, _nz, _rff, _mem) \
  376. { \
  377. .base = YAFL_KALMAN_BASE_INITIALIZER(_f, _h, _zrf, _nx, _nz, _rff, _mem), \
  378. \
  379. .sp_info = _p, \
  380. .sp_meth = _pm, \
  381. \
  382. .xmf = (yaflKalmanFuncP)_xmf, \
  383. .xrf = (yaflKalmanResFuncP)_xrf, \
  384. \
  385. .zmf = (yaflKalmanFuncP)_zmf, \
  386. \
  387. .zp = _mem.zp, \
  388. \
  389. .Sx = _mem.Sx, \
  390. .Sz = _mem.Sz, \
  391. .Pzx = _mem.Pzx, \
  392. \
  393. .sigmas_x = _mem.pool.sigmas.x, \
  394. .sigmas_z = _mem.pool.sigmas.z, \
  395. \
  396. .wm = _mem.wm, \
  397. .wc = _mem.wc \
  398. }
  399. /*---------------------------------------------------------------------------*/
  400. static inline yaflStatusEn yafl_ukf_post_init(yaflUKFBaseSt * self)
  401. {
  402. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  403. YAFL_CHECK(self->sp_meth, YAFL_ST_INV_ARG_1);
  404. YAFL_CHECK(self->sp_meth->wf, YAFL_ST_INV_ARG_1);
  405. return self->sp_meth->wf(self); /*Need to compute weights before start*/
  406. }
  407. static inline yaflStatusEn yafl_ukf_gen_sigmas(yaflUKFBaseSt * self)
  408. {
  409. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  410. YAFL_CHECK(self->sp_meth, YAFL_ST_INV_ARG_1);
  411. YAFL_CHECK(self->sp_meth->spgf, YAFL_ST_INV_ARG_1);
  412. self->sp_meth->spgf(self);
  413. return self->sp_meth->wf(self);
  414. }
  415. yaflStatusEn yafl_ukf_base_predict(yaflUKFBaseSt * self);
  416. yaflStatusEn yafl_ukf_base_update(yaflUKFBaseSt * self, yaflFloat * z, \
  417. yaflKalmanScalarUpdateP scalar_update);
  418. /*---------------------------------------------------------------------------*/
  419. #define YAFL_UKF_PREDICT_WRAPPER(func, self_type) \
  420. YAFL_KALMAN_PREDICT_WRAPPER(yafl_ukf_base_predict, yaflUKFBaseSt, \
  421. func, self_type)
  422. /*---------------------------------------------------------------------------*/
  423. #define YAFL_UKF_UPDATE_IMPL(func, self_type) \
  424. YAFL_KALMAN_UPDATE_IMPL(yafl_ukf_base_update, yaflUKFBaseSt, \
  425. func, self_type)
  426. /*=============================================================================
  427. Bierman UKF
  428. =============================================================================*/
  429. YAFL_UKF_PREDICT_WRAPPER(yafl_ukf_bierman_predict, yaflUKFBaseSt)
  430. YAFL_UKF_UPDATE_IMPL(yafl_ukf_bierman_update, yaflUKFBaseSt)
  431. /*=============================================================================
  432. Adaptive Bierman UKF
  433. =============================================================================*/
  434. typedef struct {
  435. yaflUKFBaseSt base;
  436. yaflFloat chi2;
  437. } yaflUKFAdaptivedSt;
  438. /*---------------------------------------------------------------------------*/
  439. #define YAFL_UKF_ADAPTIVE_INITIALIZER(_p, _pm, _f, _xmf, _xrf, _h, _zmf, \
  440. _zrf, _nx, _nz, _rff, _chi2, _mem) \
  441. { \
  442. .base = YAFL_UKF_BASE_INITIALIZER(_p, _pm, _f, _xmf, _xrf, _h, \
  443. _zmf, _zrf, _nx, _nz, _rff, _mem) , \
  444. .chi2 = _chi2 \
  445. }
  446. /*
  447. A good value of chi2 for yaflUKFAdaptivedSt is:
  448. scipy.stats.chi2.ppf(0.999, 1) == 10.827566170662733
  449. */
  450. /*---------------------------------------------------------------------------*/
  451. YAFL_UKF_PREDICT_WRAPPER(yafl_ukf_adaptive_bierman_predict, yaflUKFAdaptivedSt)
  452. YAFL_UKF_UPDATE_IMPL(yafl_ukf_adaptive_bierman_update, yaflUKFAdaptivedSt)
  453. /*=============================================================================
  454. Robust Bierman UKF
  455. =============================================================================*/
  456. typedef yaflFloat (* yaflUKFRobFuncP)(yaflUKFBaseSt *, yaflFloat);
  457. typedef struct {
  458. yaflUKFBaseSt base;
  459. yaflKalmanRobFuncP g; /* g = -d(ln(pdf(y))) / dy */
  460. yaflKalmanRobFuncP gdot; /* gdot = G = d(g) / dy */
  461. } yaflUKFRobustSt; /*Robust UKF*/
  462. /*---------------------------------------------------------------------------*/
  463. #define YAFL_UKF_ROBUST_INITIALIZER(_p, _pm, _f, _xmf, _xrf, _h, _zmf, _zrf, \
  464. _g, _gdot, _nx, _nz, _rff, _mem) \
  465. { \
  466. .base = YAFL_UKF_BASE_INITIALIZER(_p, _pm, _f, _xmf, _xrf, _h, _zmf, \
  467. _zrf, _nx, _nz, _rff, _mem) , \
  468. .g = _g, \
  469. .gdot = _gdot \
  470. }
  471. /*---------------------------------------------------------------------------*/
  472. YAFL_UKF_PREDICT_WRAPPER(yafl_ukf_robust_bierman_predict, yaflUKFRobustSt)
  473. YAFL_UKF_UPDATE_IMPL(yafl_ukf_robust_bierman_update, yaflUKFRobustSt)
  474. /*=============================================================================
  475. Adaptive robust Bierman UKF
  476. =============================================================================*/
  477. typedef struct {
  478. yaflUKFRobustSt base;
  479. yaflFloat chi2; /*Divergence test threshold (chi-squared criteria)*/
  480. } yaflUKFAdaptiveRobustSt; /*Robust EKF*/
  481. /*---------------------------------------------------------------------------*/
  482. #define YAFL_UKF_ADAPTIVE_ROBUST_INITIALIZER(_p, _pm, _f, _xmf, _xrf, _h, \
  483. _zmf, _zrf, _g, _gdot, _nx, \
  484. _nz, _rff, _chi2, _mem) \
  485. { \
  486. .base = YAFL_UKF_ROBUST_INITIALIZER(_p, _pm, _f, _xmf, _xrf, _h, _zmf, _zrf, \
  487. _g, _gdot, _nx, _nz, _rff, _mem), \
  488. .chi2 = _chi2 \
  489. }
  490. /*
  491. A good value of chi2 for yaflUKFAdaptiveRobustSt is:
  492. scipy.stats.chi2.ppf(0.997, 1) == 8.807468393511947
  493. */
  494. /*---------------------------------------------------------------------------*/
  495. YAFL_UKF_PREDICT_WRAPPER(yafl_ukf_adaptive_robust_bierman_predict, \
  496. yaflUKFAdaptiveRobustSt)
  497. YAFL_UKF_UPDATE_IMPL(yafl_ukf_adaptive_robust_bierman_update, \
  498. yaflUKFAdaptiveRobustSt)
  499. /*=============================================================================
  500. Full UKF, not sequential square root version of UKF
  501. =============================================================================*/
  502. typedef struct {
  503. yaflUKFBaseSt base; /* Base type */
  504. yaflFloat * Us; /* Upper triangular part of S */
  505. yaflFloat * Ds; /* Diagonal part of S */
  506. } yaflUKFSt;
  507. /*---------------------------------------------------------------------------*/
  508. /*
  509. Warning: sigmas_x and _sigmas_z aren't defined in this mixin, see
  510. sigma points generators mixins!!!
  511. */
  512. #define YAFL_UKF_MEMORY_MIXIN(nx, nz) \
  513. YAFL_UKF_BASE_MEMORY_MIXIN(nx, nz); \
  514. yaflFloat Us[((nz - 1) * nz)/2]; \
  515. yaflFloat Ds[nz]
  516. /*---------------------------------------------------------------------------*/
  517. #define YAFL_UKF_INITIALIZER(_p, _pm, _f, _xmf, _xrf, _h, _zmf, \
  518. _zrf, _nx, _nz, _rff, _mem) \
  519. { \
  520. .base = YAFL_UKF_BASE_INITIALIZER(_p, _pm, _f, _xmf, _xrf, _h, _zmf, \
  521. _zrf, _nx, _nz, _rff, _mem), \
  522. .Us = _mem.Us, \
  523. .Ds = _mem.Ds \
  524. }
  525. /*---------------------------------------------------------------------------*/
  526. YAFL_UKF_PREDICT_WRAPPER(yafl_ukf_predict, yaflUKFSt)
  527. yaflStatusEn yafl_ukf_update(yaflUKFBaseSt * self, yaflFloat * z);
  528. /*=============================================================================
  529. Full adaptive UKF, not sequential square root version of UKF
  530. =============================================================================*/
  531. typedef struct {
  532. yaflUKFSt base; /* Base type */
  533. yaflFloat chi2; /*Divergence test threshold (chi-squared criteria)*/
  534. } yaflUKFFullAdapiveSt;
  535. /*---------------------------------------------------------------------------*/
  536. #define YAFL_UKF_FULL_ADAPTIVE_INITIALIZER(_p, _pm, _f, _xmf, _xrf, _h, \
  537. _zmf, _zrf, _nx, _nz, \
  538. _rff, _chi2, _mem) \
  539. { \
  540. .base = YAFL_UKF_INITIALIZER(_p, _pm, _f, _xmf, _xrf, _h, _zmf, _zrf, \
  541. _nx, _nz, _rff, _mem), \
  542. .chi2 = _chi2 \
  543. }
  544. /*---------------------------------------------------------------------------*/
  545. static inline \
  546. yaflStatusEn yafl_ukf_adaptive_predict(yaflUKFFullAdapiveSt * self)
  547. {
  548. return yafl_ukf_predict((yaflUKFSt *)self);
  549. }
  550. yaflStatusEn yafl_ukf_adaptive_update(yaflUKFBaseSt * self, yaflFloat * z);
  551. /*=============================================================================
  552. Van der Merwe sigma point generator
  553. =============================================================================*/
  554. typedef struct _yaflUKFMerweSt {
  555. yaflUKFSigmaSt base;
  556. yaflFloat alpha;
  557. yaflFloat beta;
  558. yaflFloat kappa;
  559. } yaflUKFMerweSt;
  560. /*---------------------------------------------------------------------------*/
  561. #define YAFL_UKF_MERWE_MEMORY_MIXIN(nx, nz) \
  562. YAFL_UKF_SP_MEMORY_MIXIN(2 * nx + 1, nx, nz)
  563. /*---------------------------------------------------------------------------*/
  564. #define YAFL_UKF_MERWE_INITIALIZER(_nx, _addf, _alpha, _beta, _kappa, _mem) \
  565. { \
  566. .base = YAFL_UKF_SIGMA_BASE_INITIALIZER((2 * _nx + 1), _addf, _mem), \
  567. .alpha = _alpha, \
  568. .beta = _beta, \
  569. .kappa = _kappa \
  570. }
  571. /*---------------------------------------------------------------------------*/
  572. extern const yaflUKFSigmaMethodsSt yafl_ukf_merwe_spm;
  573. /*=============================================================================
  574. Julier sigma point generator
  575. =============================================================================*/
  576. typedef struct _yaflUKFJulierSt {
  577. yaflUKFSigmaSt base;
  578. yaflFloat kappa;
  579. } yaflUKFJulierSt;
  580. /*---------------------------------------------------------------------------*/
  581. #define YAFL_UKF_JULIER_MEMORY_MIXIN(nx, nz) \
  582. YAFL_UKF_SP_MEMORY_MIXIN(2 * nx + 1, nx, nz)
  583. /*---------------------------------------------------------------------------*/
  584. #define YAFL_UKF_JULIER_INITIALIZER(_nx, _addf, _kappa, _mem) \
  585. { \
  586. .base = YAFL_UKF_SIGMA_BASE_INITIALIZER((2 * _nx + 1), _addf, _mem), \
  587. .kappa = _kappa \
  588. }
  589. /*---------------------------------------------------------------------------*/
  590. extern const yaflUKFSigmaMethodsSt yafl_ukf_julier_spm;
  591. #endif // YAFL_H