yafl.c 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296
  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. #include <string.h>
  14. #include "yafl.h"
  15. /*np.log(2. * np.pi)*/
  16. #define YAFL_L2PI (1.8378770664093453)
  17. #define _fx (self->f)
  18. #define _hx (self->h)
  19. #define _zrf (self->zrf)
  20. #define _x (self->x)
  21. #define _y (self->y)
  22. #define _up (self->Up)
  23. #define _dp (self->Dp)
  24. #define _uq (self->Uq)
  25. #define _dq (self->Dq)
  26. #define _ur (self->Ur)
  27. #define _dr (self->Dr)
  28. #define _l (self->l)
  29. #define _nx (self->Nx)
  30. #define _nz (self->Nz)
  31. /*=============================================================================
  32. Base UDEKF
  33. =============================================================================*/
  34. #define _jfx (((yaflEKFBaseSt *)self)->jf)
  35. #define _jhx (((yaflEKFBaseSt *)self)->jh)
  36. #define _hy (((yaflEKFBaseSt *)self)->H)
  37. #define _w (((yaflEKFBaseSt *)self)->W)
  38. #define _d (((yaflEKFBaseSt *)self)->D)
  39. #define _qff (((yaflEKFBaseSt *)self)->qff)
  40. yaflStatusEn yafl_ekf_base_predict(yaflKalmanBaseSt * self)
  41. {
  42. yaflStatusEn status = YAFL_ST_OK;
  43. yaflInt i;
  44. yaflInt nx2;
  45. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  46. YAFL_CHECK(_up, YAFL_ST_INV_ARG_1);
  47. YAFL_CHECK(_dp, YAFL_ST_INV_ARG_1);
  48. YAFL_CHECK(_uq, YAFL_ST_INV_ARG_1);
  49. YAFL_CHECK(_dq, YAFL_ST_INV_ARG_1);
  50. YAFL_CHECK(_nx > 0, YAFL_ST_INV_ARG_1);
  51. YAFL_CHECK(_w, YAFL_ST_INV_ARG_1);
  52. YAFL_CHECK(_d, YAFL_ST_INV_ARG_1);
  53. nx2 = _nx * 2;
  54. /*Default f(x) = x*/
  55. if (0 == _fx)
  56. {
  57. YAFL_CHECK(0 == _jfx, YAFL_ST_INV_ARG_1);
  58. for (i = 0; i < _nx; i++)
  59. {
  60. yaflInt j;
  61. yaflInt nci;
  62. nci = nx2 * i;
  63. for (j = 0; j < _nx; j++)
  64. {
  65. _w[nci + j] = (i != j) ? 0.0 : 1.0;
  66. }
  67. }
  68. }
  69. else
  70. {
  71. //yaflFloat * x;
  72. /*Must have some Jacobian function*/
  73. YAFL_CHECK(_jfx, YAFL_ST_INV_ARG_1);
  74. //x = self->x;
  75. YAFL_TRY(status, _fx(self, _x, _x)); /* x = f(x_old, ...) */
  76. YAFL_TRY(status, _jfx(self, _w, _x)); /* Place F(x, ...)=df/dx to W */
  77. }
  78. /* Now W = (F|***) */
  79. YAFL_TRY(status, \
  80. YAFL_MATH_BSET_BU(nx2, 0, _nx, _w, _nx, _nx, nx2, 0, 0, _w, _up));
  81. /* Now W = (F|FUp) */
  82. YAFL_TRY(status, yafl_math_bset_u(nx2, _w, _nx, _uq));
  83. /* Now W = (Uq|FUp) */
  84. /* D = concatenate([Dq, Dp]) */
  85. i = _nx*sizeof(yaflFloat);
  86. memcpy((void *) _d, (void *)_dq, i);
  87. memcpy((void *)(_d + _nx), (void *)_dp, i);
  88. /* Up, Dp = MWGSU(w, d)*/
  89. YAFL_TRY(status, yafl_math_mwgsu(_nx, nx2, _up, _dp, _w, _d));
  90. return status;
  91. }
  92. /*---------------------------------------------------------------------------*/
  93. static inline yaflStatusEn \
  94. _yafl_r_update(yaflInt nx, yaflInt nz, yaflFloat rff, yaflFloat * dp, \
  95. yaflFloat * ur, yaflFloat * dr, yaflFloat * w, yaflFloat * d, \
  96. yaflFloat * res_z)
  97. {
  98. yaflStatusEn status = YAFL_ST_OK;
  99. yaflInt nxz = nx + nz;
  100. YAFL_CHECK(nx > 0, YAFL_ST_INV_ARG_1);
  101. YAFL_CHECK(nz > 0, YAFL_ST_INV_ARG_2);
  102. YAFL_CHECK(rff > 0.0, YAFL_ST_INV_ARG_3);
  103. YAFL_CHECK(rff < 1.0, YAFL_ST_INV_ARG_3);
  104. YAFL_CHECK(dp, YAFL_ST_INV_ARG_4);
  105. YAFL_CHECK(ur, YAFL_ST_INV_ARG_5);
  106. YAFL_CHECK(dr, YAFL_ST_INV_ARG_6);
  107. YAFL_CHECK(w, YAFL_ST_INV_ARG_7);
  108. YAFL_CHECK(d, YAFL_ST_INV_ARG_8);
  109. YAFL_CHECK(res_z, YAFL_ST_INV_ARG_9);
  110. /*Will end R update, now W = (HUp|***) */
  111. YAFL_TRY(status, YAFL_MATH_BSET_U(nxz, 0, nx, w, nz, ur));
  112. /*Now W = (HUp|Ur) */
  113. /*Now D=(***)*/
  114. YAFL_TRY(status, yafl_math_set_vxn(nx, d, dp, rff ));
  115. /*Now D=(rff*Dp|***)*/
  116. YAFL_TRY(status, yafl_math_set_vxn(nz, d + nx, dr, 1.0 - rff));
  117. /* Now D=(rff * Dp|(1-rff) * dr) */
  118. /* Ur, Dr = MWGSU(w, d)*/
  119. YAFL_TRY(status, yafl_math_mwgsu(nz, nxz, ur, dr, w, d));
  120. /*Now R+ = (1.0 - rff) * R- + rff * H.dot(P.dot(H.T))*/
  121. YAFL_TRY(status, yafl_math_udu_up(nz, ur, dr, rff, res_z));
  122. /*Now R+ = (1.0 - rff) * R- + rff * (res_z.dot(res_z.T) + H.dot(P.dot(H.T)))*/
  123. return status;
  124. }
  125. /*---------------------------------------------------------------------------*/
  126. static yaflStatusEn _yafl_ekf_compute_error(yaflKalmanBaseSt * self, yaflFloat * z)
  127. {
  128. yaflStatusEn status = YAFL_ST_OK;
  129. yaflInt j;
  130. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  131. YAFL_CHECK(_hx, YAFL_ST_INV_ARG_1);
  132. YAFL_CHECK(_x, YAFL_ST_INV_ARG_1);
  133. YAFL_CHECK(_y, YAFL_ST_INV_ARG_1);
  134. YAFL_CHECK(_nx > 0, YAFL_ST_INV_ARG_1);
  135. YAFL_CHECK(_nz > 0, YAFL_ST_INV_ARG_1);
  136. YAFL_TRY(status, _hx(self, _y, _x)); /* self.y = h(x,...) */
  137. /*Compute measurement error*/
  138. if (0 == _zrf)
  139. {
  140. /*Default residual*/
  141. for (j = 0; j < _nz; j++)
  142. {
  143. _y[j] = z[j] - _y[j];
  144. }
  145. }
  146. else
  147. {
  148. /*zrf must be aware of self internal structure*/
  149. YAFL_TRY(status, _zrf(self, _y, z, _y)); /* self.y = zrf(z, h(x,...)) */
  150. }
  151. return status;
  152. }
  153. /*---------------------------------------------------------------------------*/
  154. yaflStatusEn yafl_ekf_base_update(yaflKalmanBaseSt * self, yaflFloat * z, yaflKalmanScalarUpdateP scalar_update)
  155. {
  156. yaflStatusEn status = YAFL_ST_OK;
  157. yaflInt j;
  158. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  159. YAFL_CHECK(_hx, YAFL_ST_INV_ARG_1);
  160. YAFL_CHECK(_x, YAFL_ST_INV_ARG_1);
  161. YAFL_CHECK(_y, YAFL_ST_INV_ARG_1);
  162. YAFL_CHECK(_ur, YAFL_ST_INV_ARG_1);
  163. YAFL_CHECK(_nx > 0, YAFL_ST_INV_ARG_1);
  164. YAFL_CHECK(_nz > 0, YAFL_ST_INV_ARG_1);
  165. YAFL_CHECK(_jhx, YAFL_ST_INV_ARG_1);
  166. YAFL_CHECK(_hy, YAFL_ST_INV_ARG_1);
  167. YAFL_CHECK(_w, YAFL_ST_INV_ARG_1);
  168. YAFL_CHECK(_d, YAFL_ST_INV_ARG_1);
  169. YAFL_CHECK(z, YAFL_ST_INV_ARG_2);
  170. YAFL_CHECK(scalar_update, YAFL_ST_INV_ARG_3);
  171. /*
  172. _y contains residual which we will need for possible R update.
  173. Will compute innovation later.
  174. */
  175. YAFL_TRY(status, _jhx(self, _hy, _x)); /* self.H = jh(x,...) */
  176. /*Update R if needed!*/
  177. if (self->rff > 0.0)
  178. {
  179. yaflStatusEn status_r = status; /*For quiet regularization*/
  180. /*Check input data*/
  181. YAFL_CHECK(_up, YAFL_ST_INV_ARG_1);
  182. YAFL_CHECK(_dp, YAFL_ST_INV_ARG_1);
  183. YAFL_CHECK(_dr, YAFL_ST_INV_ARG_1);
  184. /*Start R update*/
  185. YAFL_TRY(status_r, yafl_math_bset_mu(_nx + _nz, _w, _nz, _nx, _hy, _up));
  186. /*Now W = (HUp|***) */
  187. YAFL_TRY(status_r, \
  188. _yafl_r_update(_nx, _nz, self->rff, _dp, _ur, _dr, _w, _d, _y));
  189. /*Updated R*/
  190. }
  191. /*Now _y can be used for innovation!*/
  192. /*Compute innovation*/
  193. YAFL_TRY(status, _yafl_ekf_compute_error(self, z));
  194. /* Decorrelate measurement noise */
  195. YAFL_TRY(status, yafl_math_ruv(_nz, _y, _ur));
  196. YAFL_TRY(status, yafl_math_rum(_nz, _nx, _hy, _ur));
  197. /*Start Q update if needed!*/
  198. if (_qff > 0.0)
  199. {
  200. yaflStatusEn status_q = status; /*For quiet regularization*/
  201. YAFL_CHECK(_dq, YAFL_ST_INV_ARG_1);
  202. YAFL_TRY(status_q, yafl_math_set_vxn(_nx, _dq, _dq, 1.0 - _qff));
  203. }
  204. /*Start log likelihood computation*/
  205. *self->l = _nz * YAFL_L2PI;
  206. /* Do scalar updates */
  207. for (j = 0; j < _nz; j++)
  208. {
  209. YAFL_TRY(status, scalar_update(self, j)); /*Q is updated here if needed!*/
  210. }
  211. /*Finalize log likelihood value*/
  212. *self->l *= -0.5;
  213. if (self->rff > 0.0)
  214. {
  215. /*Compute residual if needed*/
  216. YAFL_TRY(status, _yafl_ekf_compute_error(self, z));
  217. }
  218. return status;
  219. }
  220. /*=============================================================================
  221. Bierman filter
  222. =============================================================================*/
  223. static inline yaflStatusEn \
  224. _bierman_update_body(yaflInt nx, yaflFloat * x, yaflFloat * u, \
  225. yaflFloat * d, yaflFloat * f, yaflFloat * v, \
  226. yaflFloat r, yaflFloat nu, yaflFloat ac, \
  227. yaflFloat gdot, yaflFloat * l)
  228. {
  229. yaflStatusEn status = YAFL_ST_OK;
  230. yaflInt j;
  231. yaflInt k;
  232. yaflInt nxk;
  233. YAFL_CHECK(x, YAFL_ST_INV_ARG_2);
  234. YAFL_CHECK(u, YAFL_ST_INV_ARG_3);
  235. YAFL_CHECK(d, YAFL_ST_INV_ARG_4);
  236. YAFL_CHECK(f, YAFL_ST_INV_ARG_5);
  237. YAFL_CHECK(v, YAFL_ST_INV_ARG_6);
  238. for (k = 0, nxk = 0; k < nx; nxk += k++)
  239. {
  240. yaflFloat a;
  241. yaflFloat fk;
  242. yaflFloat vk;
  243. fk = gdot * f[k];
  244. /*Correct v in place*/
  245. vk = ac * v[k];
  246. v[k] = vk;
  247. a = r + fk * vk;
  248. /*Correct d in place*/
  249. d[k] *= ac * r / a;
  250. #define p fk /*No need for separate p variable*/
  251. p = - fk / r;
  252. for (j = 0; j < k; j++)
  253. {
  254. yaflFloat ujk;
  255. yaflFloat vj;
  256. ujk = u[j + nxk];
  257. vj = v[j];
  258. u[j + nxk] = ujk + p * vj;
  259. v[j] = vj + ujk * vk;
  260. }
  261. #undef p /*Don't need p any more...*/
  262. r = a;
  263. }
  264. /*Compute log likelihood*/
  265. *l += nu * (nu / r) + YAFL_LOG(r);
  266. /*
  267. Now we must do:
  268. x += K * nu
  269. Since:
  270. r == a
  271. then we have:
  272. K == v / a == v / r
  273. and so:
  274. K * nu == (v / r) * nu == v / r * nu == v * (nu / r)
  275. Finally we get:
  276. x += v * (nu / r)
  277. */
  278. nu /= r;
  279. for (j=0; j < nx; j++)
  280. {
  281. v[j] *= nu;
  282. x[j] += v[j];
  283. }
  284. return status;
  285. }
  286. /*---------------------------------------------------------------------------*/
  287. #define YAFL_Q_SCALAR_UPDATE(qff, knu) \
  288. do { \
  289. if (qff > 0.0) \
  290. { \
  291. yaflStatusEn status_q = status; \
  292. YAFL_CHECK(_uq, YAFL_ST_INV_ARG_1); \
  293. YAFL_CHECK(_dq, YAFL_ST_INV_ARG_1); \
  294. YAFL_TRY(status_q, yafl_math_udu_up(_nx, _uq, _dq, qff, knu)); \
  295. } \
  296. } while (0)
  297. /*---------------------------------------------------------------------------*/
  298. #define YAFL_SCALAR_UPDATE_ARGS_CHECKS() \
  299. do { \
  300. YAFL_CHECK(self, YAFL_ST_INV_ARG_1); \
  301. YAFL_CHECK(self->Nz > i, YAFL_ST_INV_ARG_2); \
  302. } while (0)
  303. /*---------------------------------------------------------------------------*/
  304. #define YAFL_EKF_BIERMAN_SELF_INTERNALS_CHECKS() \
  305. do { \
  306. YAFL_CHECK(_nx > 0, YAFL_ST_INV_ARG_1); \
  307. YAFL_CHECK(_up, YAFL_ST_INV_ARG_1); \
  308. YAFL_CHECK(_dp, YAFL_ST_INV_ARG_1); \
  309. YAFL_CHECK(_hy, YAFL_ST_INV_ARG_1); \
  310. YAFL_CHECK(_y, YAFL_ST_INV_ARG_1); \
  311. YAFL_CHECK(_dr, YAFL_ST_INV_ARG_1); \
  312. YAFL_CHECK(_d, YAFL_ST_INV_ARG_1); \
  313. } while (0)
  314. /*---------------------------------------------------------------------------*/
  315. yaflStatusEn yafl_ekf_bierman_update_scalar(yaflKalmanBaseSt * self, yaflInt i)
  316. {
  317. yaflStatusEn status = YAFL_ST_OK;
  318. yaflFloat * h;
  319. YAFL_SCALAR_UPDATE_ARGS_CHECKS();
  320. YAFL_EKF_BIERMAN_SELF_INTERNALS_CHECKS();
  321. h = _hy + _nx * i;
  322. /* f = h.dot(Up) */
  323. # define f _d
  324. YAFL_TRY(status, yafl_math_set_vtu(_nx, f, h, _up));
  325. /* v = f.dot(Dp).T = Dp.dot(f.T).T */
  326. #define v h /*Don't need h any more, use it to store v*/
  327. YAFL_TRY(status, YAFL_MATH_SET_DV(_nx, v, _dp, f));
  328. YAFL_TRY(status, \
  329. _bierman_update_body(_nx, _x, _up, _dp, f, v, _dr[i], _y[i], \
  330. 1.0, 1.0, _l));
  331. /*Update Q if needed!*/
  332. YAFL_Q_SCALAR_UPDATE(_qff, v);
  333. # undef v /*Don't nee v any more*/
  334. # undef f
  335. return status;
  336. }
  337. /*=============================================================================
  338. Joseph filter
  339. =============================================================================*/
  340. static inline yaflStatusEn \
  341. _joseph_update_body(yaflInt nx, yaflFloat * x, yaflFloat * u, \
  342. yaflFloat * d, yaflFloat * f, yaflFloat * v, \
  343. yaflFloat * k, yaflFloat * w, yaflFloat nu, \
  344. yaflFloat r, yaflFloat s, yaflFloat ac, \
  345. yaflFloat gdot, yaflFloat * l)
  346. {
  347. yaflStatusEn status = YAFL_ST_OK;
  348. yaflInt nx1;
  349. YAFL_CHECK(x, YAFL_ST_INV_ARG_2);
  350. YAFL_CHECK(u, YAFL_ST_INV_ARG_3);
  351. YAFL_CHECK(d, YAFL_ST_INV_ARG_4);
  352. YAFL_CHECK(f, YAFL_ST_INV_ARG_5);
  353. YAFL_CHECK(v, YAFL_ST_INV_ARG_6);
  354. YAFL_CHECK(k, YAFL_ST_INV_ARG_7);
  355. YAFL_CHECK(w, YAFL_ST_INV_ARG_8);
  356. YAFL_CHECK(s > 0, YAFL_ST_INV_ARG_11);
  357. nx1 = nx + 1;
  358. /* k = Up.dot(v * ac / s) = Up.dot(v) * (ac / s) */
  359. /*May be used in place*/
  360. YAFL_TRY(status, yafl_math_set_vxn(nx, v, v, ac / s));
  361. YAFL_TRY(status, yafl_math_set_uv(nx, k, u, v));
  362. # define D v
  363. /*Set W and D*/
  364. /*May be used in place*/
  365. YAFL_TRY(status, yafl_math_set_vxn(nx, f, f, gdot));
  366. /*How about yafl_math_bset_vvtxn ?*/
  367. YAFL_TRY(status, yafl_math_bset_vvt(nx1, w, nx, k, f));
  368. YAFL_TRY(status, yafl_math_bsub_u(nx1, w, nx, u));
  369. /* Now w is (gdot*kf - Up|***) */
  370. YAFL_TRY(status, YAFL_MATH_BSET_V(nx1, 0, nx, w, nx, k));
  371. /* Now w is (gdot*kf - Up|k) */
  372. /* D = concatenate([ac * Dp, np.array([gdot * r])]) */
  373. YAFL_TRY(status, yafl_math_set_vxn(nx, D, d, ac));
  374. D[nx] = gdot * r;
  375. /* Up, Dp = MWGSU(W, D)*/
  376. YAFL_TRY(status, yafl_math_mwgsu(nx, nx1, u, d, w, D));
  377. /*Compute log likelihood*/
  378. *l += nu * (nu / s) + YAFL_LOG(s);
  379. /* x += k * nu */
  380. # define j nx1
  381. for (j=0; j < nx; j++)
  382. {
  383. k[j] *= nu;
  384. x[j] += k[j];
  385. }
  386. # undef j /*Don't nee j any more*/
  387. # undef D /*Don't nee D any more*/
  388. return status;
  389. }
  390. /*---------------------------------------------------------------------------*/
  391. #define YAFL_EKF_JOSEPH_SELF_INTERNALS_CHECKS() \
  392. do { \
  393. YAFL_CHECK(_nx > 0, YAFL_ST_INV_ARG_1); \
  394. YAFL_CHECK(_up, YAFL_ST_INV_ARG_1); \
  395. YAFL_CHECK(_dp, YAFL_ST_INV_ARG_1); \
  396. YAFL_CHECK(_hy, YAFL_ST_INV_ARG_1); \
  397. YAFL_CHECK(_y, YAFL_ST_INV_ARG_1); \
  398. YAFL_CHECK(_dr, YAFL_ST_INV_ARG_1); \
  399. YAFL_CHECK(_w, YAFL_ST_INV_ARG_1); \
  400. YAFL_CHECK(_d, YAFL_ST_INV_ARG_1); \
  401. } while (0)
  402. /*---------------------------------------------------------------------------*/
  403. yaflStatusEn yafl_ekf_joseph_update_scalar(yaflKalmanBaseSt * self, yaflInt i)
  404. {
  405. yaflStatusEn status = YAFL_ST_OK;
  406. yaflFloat s = 0.0;
  407. yaflFloat * f;
  408. yaflFloat * h;
  409. YAFL_SCALAR_UPDATE_ARGS_CHECKS();
  410. YAFL_EKF_JOSEPH_SELF_INTERNALS_CHECKS();
  411. # define v _d
  412. f = v + _nx;
  413. h = _hy + _nx * i;
  414. /* f = h.dot(Up) */
  415. YAFL_TRY(status, yafl_math_set_vtu(_nx, f, h, _up));
  416. /* v = f.dot(Dp).T = Dp.dot(f.T).T */
  417. YAFL_TRY(status, YAFL_MATH_SET_DV(_nx, v, _dp, f));
  418. # define r _dr[i]
  419. /* s = r + f.dot(v)*/
  420. YAFL_TRY(status, yafl_math_vtv(_nx, &s, f, v));
  421. s += r;
  422. /*K = Up.dot(v/s) = Up.dot(v)/s*/
  423. # define K h /*Don't need h any more, use it to store K*/
  424. YAFL_TRY(status, \
  425. _joseph_update_body(_nx, _x, _up, _dp, f, v, K, _w, _y[i], r, \
  426. s, 1.0, 1.0, _l));
  427. /*Update Q if needed!*/
  428. YAFL_Q_SCALAR_UPDATE(_qff, K);
  429. # undef K /*Don't nee K any more*/
  430. # undef r
  431. # undef v
  432. return status;
  433. }
  434. /*=============================================================================
  435. Adaptive Bierman filter
  436. =============================================================================*/
  437. static inline yaflStatusEn \
  438. _adaptive_correction(yaflInt nx, yaflFloat * res_ac, yaflFloat * res_s, \
  439. yaflFloat * f, yaflFloat * v, yaflFloat r, \
  440. yaflFloat nu, yaflFloat gdot, yaflFloat chi2)
  441. {
  442. yaflStatusEn status = YAFL_ST_OK;
  443. yaflFloat c = 0.0;
  444. yaflFloat ac;
  445. yaflFloat s;
  446. YAFL_CHECK(res_ac, YAFL_ST_INV_ARG_1);
  447. YAFL_CHECK(f, YAFL_ST_INV_ARG_3);
  448. YAFL_CHECK(v, YAFL_ST_INV_ARG_4);
  449. YAFL_CHECK(chi2 > 0, YAFL_ST_INV_ARG_8);
  450. /* s = alpha**2 + gdot * f.dot(v)*/
  451. YAFL_TRY(status, yafl_math_vtv(nx, &c, f, v));
  452. c *= gdot;
  453. s = r + c;
  454. /* Divergence test */
  455. ac = gdot * (nu * (nu / chi2)) - s;
  456. if (ac > 0.0)
  457. {
  458. /*Adaptive correction factor*/
  459. ac = ac / c + 1.0;
  460. /*Corrected s*/
  461. s = ac * c + r;
  462. status |= YAFL_ST_MSK_ANOMALY; /*Anomaly detected!*/
  463. }
  464. else
  465. {
  466. ac = 1.0;
  467. }
  468. *res_ac = ac;
  469. if (res_s)
  470. {
  471. *res_s = s;
  472. }
  473. return status;
  474. }
  475. /*---------------------------------------------------------------------------*/
  476. yaflStatusEn yafl_ekf_adaptive_bierman_update_scalar(yaflKalmanBaseSt * self, \
  477. yaflInt i)
  478. {
  479. yaflStatusEn status = YAFL_ST_OK;
  480. yaflFloat ac = 1.0;
  481. yaflFloat nu = 0.0;
  482. yaflFloat * h;
  483. YAFL_SCALAR_UPDATE_ARGS_CHECKS();
  484. YAFL_EKF_BIERMAN_SELF_INTERNALS_CHECKS();
  485. nu = _y[i];
  486. h = _hy + _nx * i;
  487. /* f = h.dot(Up) */
  488. # define f _d
  489. //f = self->D;
  490. YAFL_TRY(status, yafl_math_set_vtu(_nx, f, h, _up));
  491. /* v = f.dot(Dp).T = Dp.dot(f.T).T */
  492. # define v _hy /*Don't need h any more, use it to store v*/
  493. YAFL_TRY(status, YAFL_MATH_SET_DV(_nx, v, _dp, f));
  494. # define r _dr[i]
  495. YAFL_TRY(status, \
  496. _adaptive_correction(_nx, &ac, 0, f, v, r, nu, 1.0, \
  497. ((yaflEKFAdaptiveSt *)self)->chi2));
  498. YAFL_TRY(status, \
  499. _bierman_update_body(_nx, _x, _up, _dp, f, v, r, nu, ac, 1.0, _l));
  500. /*Update Q if needed!*/
  501. YAFL_Q_SCALAR_UPDATE(_qff, v);
  502. # undef r
  503. # undef v /*Don't nee v any more*/
  504. # undef f
  505. return status;
  506. }
  507. /*=============================================================================
  508. Adaptive Joseph filter
  509. =============================================================================*/
  510. yaflStatusEn \
  511. yafl_ekf_adaptive_joseph_update_scalar(yaflKalmanBaseSt * self, yaflInt i)
  512. {
  513. yaflStatusEn status = YAFL_ST_OK;
  514. yaflFloat s = 1.0;
  515. yaflFloat ac = 1.0;
  516. yaflFloat nu = 0.0;
  517. yaflFloat * h;
  518. yaflFloat * f;
  519. YAFL_SCALAR_UPDATE_ARGS_CHECKS();
  520. YAFL_EKF_JOSEPH_SELF_INTERNALS_CHECKS();
  521. h = _hy + _nx * i;
  522. # define v _d
  523. f = v + _nx;
  524. nu = _y[i];
  525. /* f = h.dot(Up) */
  526. YAFL_TRY(status, yafl_math_set_vtu(_nx, f, h, _up));
  527. /* v = f.dot(Dp).T = Dp.dot(f.T).T */
  528. YAFL_TRY(status, YAFL_MATH_SET_DV(_nx, v, _dp, f));
  529. # define r _dr[i]
  530. YAFL_TRY(status, \
  531. _adaptive_correction(_nx, &ac, &s, f, v, r, nu, 1.0, \
  532. ((yaflEKFAdaptiveSt *)self)->chi2));
  533. /* K = Up.dot(v * ac / s) = Up.dot(v) * (ac / s) */
  534. # define K h /*Don't need h any more, use it to store K*/
  535. YAFL_TRY(status, \
  536. _joseph_update_body(_nx, _x, _up, _dp, f, v, K, _w, nu, r, s, \
  537. ac, 1.0, _l));
  538. /*Update Q if needed!*/
  539. YAFL_Q_SCALAR_UPDATE(_qff, K);
  540. # undef K /*Don't nee K any more*/
  541. # undef r
  542. # undef v
  543. return status;
  544. }
  545. /*=============================================================================
  546. WARNING!!!
  547. DO NOT USE THIS variant of Adaptive Joseph filter !!!
  548. It was implemented to show some flaws of the corresponding algorithm!
  549. =============================================================================*/
  550. yaflStatusEn \
  551. yafl_ekf_do_not_use_this_update_scalar(yaflKalmanBaseSt * self, yaflInt i)
  552. {
  553. yaflStatusEn status = YAFL_ST_OK;
  554. yaflFloat c = 0.0;
  555. yaflFloat s = 0.0;
  556. yaflInt nx;
  557. yaflInt nx1;
  558. yaflFloat * d;
  559. yaflFloat * u;
  560. yaflFloat * h;
  561. yaflFloat * f;
  562. yaflFloat * v;
  563. yaflFloat * w;
  564. yaflFloat nu;
  565. yaflFloat r;
  566. yaflFloat ac;
  567. YAFL_SCALAR_UPDATE_ARGS_CHECKS();
  568. nx = _nx;
  569. YAFL_EKF_JOSEPH_SELF_INTERNALS_CHECKS();
  570. nx1 = nx + 1;
  571. d = _dp;
  572. u = _up;
  573. h = _hy + nx * i;
  574. v = _d;
  575. f = v + nx;
  576. w = _w;
  577. nu = _y[i];
  578. r = _dr[i];
  579. /* f = h.dot(Up) */
  580. YAFL_TRY(status, yafl_math_set_vtu(nx, f, h, u));
  581. /* v = f.dot(Dp).T = Dp.dot(f.T).T */
  582. YAFL_TRY(status, YAFL_MATH_SET_DV(nx, v, d, f));
  583. /* s = r + f.dot(v)*/
  584. YAFL_TRY(status, yafl_math_vtv(nx, &c, f, v));
  585. s = c + r;
  586. /* Divergence test */
  587. ac = (nu * (nu / (((yaflEKFAdaptiveSt *)self)->chi2))) - s;
  588. if (ac > 0.0)
  589. {
  590. /*Adaptive correction with no limitations approach*/
  591. YAFL_TRY(status, yafl_math_set_uv(nx, f, u, v));
  592. YAFL_TRY(status, yafl_math_udu_up(nx, u, d, (ac / c) / c, f));
  593. /*Recompute f,v,s*/
  594. /* f = h.dot(Up) */
  595. YAFL_TRY(status, yafl_math_set_vtu(nx, f, h, u));
  596. /* v = f.dot(Dp).T = Dp.dot(f.T).T */
  597. YAFL_TRY(status, YAFL_MATH_SET_DV(nx, v, d, f));
  598. /* s = r + f.dot(v)*/
  599. YAFL_TRY(status, yafl_math_vtv(nx, &c, f, v));
  600. s = c + r;
  601. }
  602. /* K = Up.dot(v * ac / s) = Up.dot(v) * (ac / s) */
  603. #define K h /*Don't need h any more, use it to store K*/
  604. #define D v
  605. YAFL_TRY(status, yafl_math_set_uv(nx, K, u, v));
  606. YAFL_TRY(status, yafl_math_set_vrn(nx, K, K, s)); /*May be used in place*/
  607. /*Set W and D*/
  608. YAFL_TRY(status, yafl_math_bset_vvt(nx1, w, nx, K, f));
  609. YAFL_TRY(status, yafl_math_bsub_u(nx1, w, nx, u));
  610. /* Now w is (Kf - Up|***) */
  611. YAFL_TRY(status, YAFL_MATH_BSET_V(nx1, 0, nx, w, nx, K));
  612. /* Now w is (Kf - Up|K) */
  613. /* D = concatenate([Dp, np.array([r])]) */
  614. memcpy((void *)D, (void *)d, nx * sizeof(yaflFloat));
  615. D[nx] = r;
  616. /* Up, Dp = MWGSU(W, D)*/
  617. YAFL_TRY(status, yafl_math_mwgsu(nx, nx1, u, d, w, D));
  618. /*Compute log likelihood*/
  619. *self->l += nu * (nu / s) + YAFL_LOG(s);
  620. /* self.x += K * nu */
  621. YAFL_TRY(status, yafl_math_add_vxn(nx, _x, K, nu));
  622. #undef D /*Don't nee D any more*/
  623. #undef K /*Don't nee K any more*/
  624. return status;
  625. }
  626. /*=============================================================================
  627. Robust Bierman filter
  628. =============================================================================*/
  629. static inline yaflStatusEn \
  630. _scalar_robustify(yaflKalmanBaseSt * self, \
  631. yaflKalmanRobFuncP g, yaflKalmanRobFuncP gdot, \
  632. yaflFloat * gdot_res, yaflFloat * nu, yaflFloat r)
  633. {
  634. yaflFloat tmp;
  635. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  636. YAFL_CHECK(gdot_res, YAFL_ST_INV_ARG_3);
  637. YAFL_CHECK(nu, YAFL_ST_INV_ARG_4);
  638. if (g)
  639. {
  640. r = YAFL_SQRT(r);/*We need sqrt here*/
  641. tmp = *nu / r;
  642. *nu = r * g(self, tmp);
  643. YAFL_CHECK(gdot, YAFL_ST_INV_ARG_1);
  644. tmp = gdot(self, tmp);
  645. }
  646. else
  647. {
  648. tmp = 1.0;
  649. }
  650. *gdot_res = tmp;
  651. /*Detect glitches and return*/
  652. if (tmp < YAFL_EPS)
  653. {
  654. return YAFL_ST_MSK_GLITCH_LARGE;
  655. }
  656. if (tmp < (1.0 - 2.0*YAFL_EPS))
  657. {
  658. return YAFL_ST_MSK_GLITCH_SMALL;
  659. }
  660. return YAFL_ST_OK;
  661. }
  662. #define YAFL_EKF_SCALAR_ROBUSTIFY(_self, _gdot_res, _nu, _r) \
  663. _scalar_robustify(_self, \
  664. ((yaflEKFRobustSt *)_self)->g, \
  665. ((yaflEKFRobustSt *)_self)->gdot, \
  666. _gdot_res, _nu, _r)
  667. /*---------------------------------------------------------------------------*/
  668. yaflStatusEn \
  669. yafl_ekf_robust_bierman_update_scalar(yaflKalmanBaseSt * self, yaflInt i)
  670. {
  671. yaflStatusEn status = YAFL_ST_OK;
  672. yaflFloat gdot = 1.0;
  673. yaflFloat nu = 0.0;
  674. yaflFloat r;
  675. yaflFloat * h;
  676. YAFL_SCALAR_UPDATE_ARGS_CHECKS();
  677. YAFL_EKF_BIERMAN_SELF_INTERNALS_CHECKS();
  678. r = _dr[i];
  679. nu = _y[i];
  680. YAFL_TRY(status, YAFL_EKF_SCALAR_ROBUSTIFY(self, &gdot, &nu, r));
  681. h = _hy + _nx * i;
  682. /* f = h.dot(Up) */
  683. # define f _d
  684. YAFL_TRY(status, yafl_math_set_vtu(_nx, f, h, _up));
  685. /* v = f.dot(Dp).T = Dp.dot(f.T).T */
  686. # define v h /*Don't need h any more, use it to store v*/
  687. YAFL_TRY(status, YAFL_MATH_SET_DV(_nx, v, _dp, f));
  688. YAFL_TRY(status, \
  689. _bierman_update_body(_nx, _x, _up, _dp, f, v, r, nu, \
  690. 1.0, gdot, _l));
  691. /*Update Q if needed!*/
  692. YAFL_Q_SCALAR_UPDATE(_qff, v);
  693. # undef v /*Don't nee v any more*/
  694. # undef f
  695. return status;
  696. }
  697. /*=============================================================================
  698. Robust Joseph filter
  699. =============================================================================*/
  700. yaflStatusEn \
  701. yafl_ekf_robust_joseph_update_scalar(yaflKalmanBaseSt * self, yaflInt i)
  702. {
  703. yaflStatusEn status = YAFL_ST_OK;
  704. yaflFloat gdot = 0.0;
  705. yaflFloat s = 0.0;
  706. yaflFloat nu = 0.0;
  707. yaflFloat r;
  708. yaflFloat * h;
  709. yaflFloat * f;
  710. YAFL_SCALAR_UPDATE_ARGS_CHECKS();
  711. YAFL_EKF_JOSEPH_SELF_INTERNALS_CHECKS();
  712. r = _dr[i];
  713. nu = _y[i];
  714. YAFL_TRY(status, YAFL_EKF_SCALAR_ROBUSTIFY(self, &gdot, &nu, r));
  715. h = _hy + _nx * i;
  716. # define v _d
  717. f = v + _nx;
  718. /* f = h.dot(Up) */
  719. YAFL_TRY(status, yafl_math_set_vtu(_nx, f, h, _up));
  720. /* v = f.dot(Dp).T = Dp.dot(f.T).T */
  721. YAFL_TRY(status, YAFL_MATH_SET_DV(_nx, v, _dp, f));
  722. /* s = r + gdot * f.dot(v)*/
  723. YAFL_TRY(status, yafl_math_vtv(_nx, &s, f, v));
  724. s = r + gdot * s;
  725. /*K = Up.dot(v/s) = Up.dot(v)/s*/
  726. # define K h /*Don't need h any more, use it to store K*/
  727. YAFL_TRY(status, \
  728. _joseph_update_body(_nx, _x, _up, _dp, f, v, K, _w, nu, r, s, \
  729. 1.0, gdot, _l));
  730. /*Update Q if needed!*/
  731. YAFL_Q_SCALAR_UPDATE(_qff, K);
  732. # undef K /*Don't nee K any more*/
  733. # undef v /*Don't nee v any more*/
  734. return status;
  735. }
  736. /*=============================================================================
  737. Adaptive robust Bierman filter
  738. =============================================================================*/
  739. yaflStatusEn \
  740. yafl_ekf_adaptive_robust_bierman_update_scalar(yaflKalmanBaseSt * self, \
  741. yaflInt i)
  742. {
  743. yaflStatusEn status = YAFL_ST_OK;
  744. yaflFloat gdot = 1.0;
  745. yaflFloat ac = 1.0;
  746. yaflFloat nu = 0.0;
  747. yaflFloat r;
  748. yaflFloat * h;
  749. YAFL_SCALAR_UPDATE_ARGS_CHECKS();
  750. YAFL_EKF_BIERMAN_SELF_INTERNALS_CHECKS();
  751. r = _dr[i];
  752. nu = _y[i];
  753. YAFL_TRY(status, YAFL_EKF_SCALAR_ROBUSTIFY(self, &gdot, &nu, r));
  754. h = _hy + _nx * i;
  755. /* f = h.dot(Up) */
  756. # define f _d
  757. YAFL_TRY(status, yafl_math_set_vtu(_nx, f, h, _up));
  758. /* v = f.dot(Dp).T = Dp.dot(f.T).T */
  759. # define v h /*Don't need h any more, use it to store v*/
  760. YAFL_TRY(status, YAFL_MATH_SET_DV(_nx, v, _dp, f));
  761. YAFL_TRY(status, \
  762. _adaptive_correction(_nx, &ac, 0, f, v, r, nu, gdot, \
  763. ((yaflEKFAdaptiveRobustSt *)self)->chi2));
  764. YAFL_TRY(status, _bierman_update_body(_nx, _x, _up, _dp, f, v, r, nu, \
  765. ac, gdot, _l));
  766. /*Update Q if needed!*/
  767. YAFL_Q_SCALAR_UPDATE(_qff, v);
  768. # undef v /*Don't nee v any more*/
  769. # undef f
  770. return status;
  771. }
  772. /*=============================================================================
  773. Adaptive robust Joseph filter
  774. =============================================================================*/
  775. yaflStatusEn \
  776. yafl_ekf_adaptive_robust_joseph_update_scalar(yaflKalmanBaseSt * self, \
  777. yaflInt i)
  778. {
  779. yaflStatusEn status = YAFL_ST_OK;
  780. yaflFloat gdot = 1.0;
  781. yaflFloat ac = 1.0;
  782. yaflFloat s = 0.0;
  783. yaflFloat * h;
  784. yaflFloat * f;
  785. yaflFloat r;
  786. yaflFloat nu;
  787. YAFL_SCALAR_UPDATE_ARGS_CHECKS();
  788. YAFL_EKF_JOSEPH_SELF_INTERNALS_CHECKS();
  789. r = _dr[i]; /* alpha = r**0.5 is stored in Dr*/
  790. nu = _y[i];
  791. YAFL_TRY(status, YAFL_EKF_SCALAR_ROBUSTIFY(self, &gdot, &nu, r));
  792. h = _hy + _nx * i;
  793. # define v _d
  794. f = v + _nx;
  795. /* f = h.dot(Up) */
  796. YAFL_TRY(status, yafl_math_set_vtu(_nx, f, h, _up));
  797. /* v = f.dot(Dp).T = Dp.dot(f.T).T */
  798. YAFL_TRY(status, YAFL_MATH_SET_DV(_nx, v, _dp, f));
  799. YAFL_TRY(status, \
  800. _adaptive_correction(_nx, &ac, &s, f, v, r, nu, gdot, \
  801. ((yaflEKFAdaptiveRobustSt *)self)->chi2));
  802. /* K = Up.dot(v * ac / s) = Up.dot(v) * (ac / s) */
  803. # define K h /*Don't need h any more, use it to store K*/
  804. YAFL_TRY(status, \
  805. _joseph_update_body(_nx, _x, _up, _dp, f, v, K, _w, nu, r, s, \
  806. ac, gdot, _l));
  807. /*Update Q if needed!*/
  808. YAFL_Q_SCALAR_UPDATE(_qff, K);
  809. # undef K /*Don't nee K any more*/
  810. # undef v
  811. return status;
  812. }
  813. /*------------------------------------------------------------------------------
  814. Undef EKF stuff
  815. ------------------------------------------------------------------------------*/
  816. #undef _jfx
  817. #undef _jhx
  818. #undef _hy
  819. #undef _w
  820. #undef _d
  821. #undef _qff
  822. /*=============================================================================
  823. Basic UD-factorized UKF functions
  824. =============================================================================*/
  825. #define _kalman_self ((yaflKalmanBaseSt *)self)
  826. #define _ufx (_kalman_self->f)
  827. #define _uhx (_kalman_self->h)
  828. #define _uzrf (_kalman_self->zrf)
  829. #define _ux (_kalman_self->x)
  830. #define _uy (_kalman_self->y)
  831. #define _uup (_kalman_self->Up)
  832. #define _udp (_kalman_self->Dp)
  833. #define _uuq (_kalman_self->Uq)
  834. #define _udq (_kalman_self->Dq)
  835. #define _uur (_kalman_self->Ur)
  836. #define _udr (_kalman_self->Dr)
  837. #define _ul (_kalman_self->l)
  838. #define _unx (_kalman_self->Nx)
  839. #define _unz (_kalman_self->Nz)
  840. /*UKF stuff*/
  841. #define _xrf (self->xrf)
  842. #define _xmf (self->xmf)
  843. #define _zmf (self->zmf)
  844. #define _zp (self->zp)
  845. #define _sx (self->Sx)
  846. #define _sz (self->Sz)
  847. #define _pzx (self->Pzx)
  848. #define _sigmas_x (self->sigmas_x)
  849. #define _sigmas_z (self->sigmas_z)
  850. #define _wm (self->wm)
  851. #define _wc (self->wc)
  852. /*---------------------------------------------------------------------------*/
  853. static inline yaflStatusEn _compute_res(yaflKalmanBaseSt * self, yaflInt sz, \
  854. yaflKalmanResFuncP rf, yaflFloat * res, \
  855. yaflFloat * sigma, yaflFloat * pivot)
  856. {
  857. yaflStatusEn status = YAFL_ST_OK;
  858. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  859. YAFL_CHECK(res, YAFL_ST_INV_ARG_4);
  860. YAFL_CHECK(sigma, YAFL_ST_INV_ARG_5);
  861. YAFL_CHECK(pivot, YAFL_ST_INV_ARG_6);
  862. if (rf)
  863. {
  864. /*rf must be aware of sp and the current transform*/
  865. /* res = self.rf(sigma, pivot) */
  866. YAFL_TRY(status, rf(self, res, sigma, pivot));
  867. }
  868. else
  869. {
  870. yaflInt j;
  871. for (j = 0; j < sz; j++)
  872. {
  873. res[j] = sigma[j] - pivot[j];
  874. }
  875. }
  876. return status;
  877. }
  878. /*---------------------------------------------------------------------------*/
  879. static inline yaflStatusEn _unscented_mean(yaflUKFBaseSt * self, \
  880. yaflInt sz, \
  881. yaflFloat * mean, \
  882. yaflInt np, \
  883. yaflFloat * sigmas, \
  884. yaflKalmanFuncP mf)
  885. {
  886. yaflStatusEn status = YAFL_ST_OK;
  887. /*Args get checked later, or don't need to be checked*/
  888. if (mf)
  889. {
  890. /*mf must be aware of the current transform details...*/
  891. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  892. YAFL_CHECK(_wm, YAFL_ST_INV_ARG_1);
  893. YAFL_CHECK(mean, YAFL_ST_INV_ARG_3);
  894. YAFL_CHECK(sigmas, YAFL_ST_INV_ARG_5);
  895. YAFL_TRY(status, mf(_kalman_self, mean, sigmas));
  896. }
  897. else
  898. {
  899. YAFL_TRY(status, yafl_math_set_vtm(np, sz, mean, _wm, sigmas));
  900. }
  901. return status;
  902. }
  903. /*---------------------------------------------------------------------------*/
  904. static inline yaflStatusEn _unscented_update(yaflUKFBaseSt * self, \
  905. yaflInt sz, \
  906. yaflFloat * u, \
  907. yaflFloat * d, \
  908. yaflFloat * pivot, \
  909. yaflInt np, \
  910. yaflFloat * sigmas, \
  911. yaflFloat * sp, \
  912. yaflKalmanResFuncP rf, \
  913. yaflFloat n)
  914. {
  915. yaflStatusEn status = YAFL_ST_OK;
  916. yaflInt i;
  917. yaflFloat * wc;
  918. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  919. wc = _wc;
  920. YAFL_CHECK(wc, YAFL_ST_INV_ARG_1);
  921. /*Other args get checked later, or don't need to be checked*/
  922. for (i = 0; i < np; i++)
  923. {
  924. yaflFloat wci;
  925. YAFL_TRY(status, _compute_res(_kalman_self, sz, rf, sp, sigmas + sz * i, pivot));
  926. /*Update res_u and res_d*/
  927. /*wc should be sorted in descending order*/
  928. wci = wc[i] * n;
  929. if (wci >= 0.0)
  930. {
  931. YAFL_TRY(status, yafl_math_udu_up(sz, u, d, wci, sp));
  932. }
  933. else
  934. {
  935. YAFL_TRY(status, yafl_math_udu_down(sz, u, d, -wci, sp));
  936. }
  937. }
  938. return status;
  939. }
  940. /*---------------------------------------------------------------------------*/
  941. static yaflStatusEn _unscented_transform(yaflUKFBaseSt * self, \
  942. yaflInt res_sz, \
  943. yaflFloat * res_v, \
  944. yaflFloat * res_u, \
  945. yaflFloat * res_d, \
  946. yaflFloat * sp, \
  947. yaflFloat * sigmas, \
  948. yaflFloat * noise_u, \
  949. yaflFloat * noise_d, \
  950. yaflKalmanFuncP mf, \
  951. yaflKalmanResFuncP rf)
  952. {
  953. yaflStatusEn status = YAFL_ST_OK;
  954. yaflInt np;
  955. yaflInt i;
  956. yaflUKFSigmaSt * sp_info;
  957. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  958. YAFL_CHECK(res_sz > 0, YAFL_ST_INV_ARG_2);
  959. YAFL_CHECK(res_u, YAFL_ST_INV_ARG_4);
  960. YAFL_CHECK(res_d, YAFL_ST_INV_ARG_5);
  961. YAFL_CHECK(sp, YAFL_ST_INV_ARG_6);
  962. if (noise_u)
  963. {
  964. YAFL_CHECK(noise_d, YAFL_ST_INV_ARG_9);
  965. }
  966. YAFL_CHECK(_wm, YAFL_ST_INV_ARG_1);
  967. YAFL_CHECK(_wc, YAFL_ST_INV_ARG_1);
  968. sp_info = self->sp_info;
  969. YAFL_CHECK(sp_info, YAFL_ST_INV_ARG_1);
  970. np = sp_info->np;
  971. YAFL_CHECK(np > 1, YAFL_ST_INV_ARG_1);
  972. YAFL_TRY(status, _unscented_mean(self, res_sz, res_v, np, sigmas, mf));
  973. if (noise_u)
  974. {
  975. YAFL_CHECK(noise_d, YAFL_ST_INV_ARG_8);
  976. /*res_u, res_d = noise_u.copy(), noise_d.copy()*/
  977. memcpy((void *)res_u, (void *)noise_u, (res_sz * (res_sz - 1)) / 2 * sizeof(yaflFloat));
  978. memcpy((void *)res_d, (void *)noise_d, res_sz * sizeof(yaflFloat));
  979. }
  980. else
  981. {
  982. /* Zero res_u and res_d */
  983. for (i = res_sz - 1; i >= 0; i--)
  984. {
  985. res_d[i] = 0.0;
  986. }
  987. for (i = ((res_sz * (res_sz - 1)) / 2) - 1; i >= 0; i--)
  988. {
  989. res_u[i] = 0.0;
  990. }
  991. }
  992. YAFL_TRY(status, _unscented_update(self, res_sz, res_u, res_d, res_v, \
  993. np, sigmas, sp, rf, 1.0));
  994. return status;
  995. }
  996. /*---------------------------------------------------------------------------*/
  997. yaflStatusEn yafl_ukf_base_predict(yaflUKFBaseSt * self)
  998. {
  999. yaflStatusEn status = YAFL_ST_OK;
  1000. yaflInt np;
  1001. yaflInt nx;
  1002. yaflInt i;
  1003. yaflUKFSigmaSt * sp_info;
  1004. /*Check some params and generate sigma points*/
  1005. YAFL_TRY(status, yafl_ukf_gen_sigmas(self)); /*Self is checked here*/
  1006. YAFL_CHECK(_unx, YAFL_ST_INV_ARG_1);
  1007. YAFL_CHECK(_sigmas_x, YAFL_ST_INV_ARG_1);
  1008. YAFL_CHECK(self->sp_info, YAFL_ST_INV_ARG_1);
  1009. sp_info = self->sp_info;
  1010. np = sp_info->np;
  1011. YAFL_CHECK(np > 1, YAFL_ST_INV_ARG_1);
  1012. nx = _unx;
  1013. YAFL_CHECK(nx > 0, YAFL_ST_INV_ARG_1);
  1014. /*Compute process sigmas*/
  1015. YAFL_CHECK(_ufx, YAFL_ST_INV_ARG_1);
  1016. if (_ufx)
  1017. {
  1018. for (i = 0; i < np; i++)
  1019. {
  1020. yaflFloat * sigmai;
  1021. sigmai = _sigmas_x + nx * i;
  1022. YAFL_TRY(status, _ufx(_kalman_self, sigmai, sigmai));
  1023. }
  1024. }
  1025. /*Predict x, Up, Dp*/
  1026. YAFL_TRY(status, \
  1027. _unscented_transform(self, nx, _ux, _uup, _udp, _sx, _sigmas_x, \
  1028. _uuq, _udq, _xmf, _xrf));
  1029. return status;
  1030. }
  1031. /*---------------------------------------------------------------------------*/
  1032. static inline yaflStatusEn _yafl_ukf_compute_sigmas_z_and_zp(yaflUKFBaseSt * self)
  1033. {
  1034. yaflStatusEn status = YAFL_ST_OK;
  1035. yaflInt i;
  1036. yaflInt nx;
  1037. yaflInt nz;
  1038. yaflInt np;
  1039. yaflUKFSigmaSt * sp_info; /*Sigma point generator info*/
  1040. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  1041. YAFL_CHECK(_zp, YAFL_ST_INV_ARG_1);
  1042. YAFL_CHECK(_sigmas_z, YAFL_ST_INV_ARG_1);
  1043. YAFL_CHECK(_wm, YAFL_ST_INV_ARG_1);
  1044. nx = _unx;
  1045. YAFL_CHECK(nx, YAFL_ST_INV_ARG_1);
  1046. nz = _unz;
  1047. YAFL_CHECK(nz, YAFL_ST_INV_ARG_1);
  1048. sp_info = self->sp_info;
  1049. YAFL_CHECK(sp_info, YAFL_ST_INV_ARG_1);
  1050. np = sp_info->np;
  1051. YAFL_CHECK(np > 1, YAFL_ST_INV_ARG_1);
  1052. for (i = 0; i < np; i++)
  1053. {
  1054. YAFL_TRY(status, _uhx(_kalman_self, _sigmas_z + nz * i, \
  1055. _sigmas_x + nx * i));
  1056. }
  1057. YAFL_TRY(status, _unscented_mean(self, nz, _zp, np, _sigmas_z, _zmf));
  1058. return status;
  1059. }
  1060. /*---------------------------------------------------------------------------*/
  1061. static inline yaflStatusEn _yafl_ukf_compute_residual(yaflUKFBaseSt * self, \
  1062. yaflFloat * z)
  1063. {
  1064. yaflStatusEn status = YAFL_ST_OK;
  1065. yaflFloat rff;
  1066. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  1067. rff = _kalman_self->rff;
  1068. if (rff > 0.0)
  1069. {
  1070. YAFL_CHECK(rff < 1.0, YAFL_ST_INV_ARG_1);
  1071. /*Compute residual if needed (need to generate new sigmas)*/
  1072. YAFL_TRY(status, yafl_ukf_gen_sigmas(self));
  1073. YAFL_TRY(status, _yafl_ukf_compute_sigmas_z_and_zp(self));
  1074. YAFL_TRY(status, _compute_res(_kalman_self, _unz, _uzrf, _uy, z, _zp));
  1075. }
  1076. return status;
  1077. }
  1078. /*---------------------------------------------------------------------------*/
  1079. static inline yaflStatusEn _yafl_ukf_compute_pzx(yaflUKFBaseSt * self)
  1080. {
  1081. yaflStatusEn status = YAFL_ST_OK;
  1082. yaflUKFSigmaSt * sp_info;
  1083. yaflKalmanResFuncP zrf;
  1084. yaflKalmanResFuncP xrf;
  1085. yaflFloat * sigmas_x;
  1086. yaflFloat * sigmas_z;
  1087. yaflFloat * pzx;
  1088. yaflFloat * sx;
  1089. yaflFloat * sz;
  1090. yaflFloat * x;
  1091. yaflFloat * zp;
  1092. yaflFloat * wc;
  1093. yaflInt np;
  1094. yaflInt nz;
  1095. yaflInt nx;
  1096. yaflInt i;
  1097. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  1098. nx = _unx;
  1099. YAFL_CHECK(nx, YAFL_ST_INV_ARG_1);
  1100. nz = _unz;
  1101. YAFL_CHECK(nz, YAFL_ST_INV_ARG_1);
  1102. sp_info = self->sp_info;
  1103. YAFL_CHECK(sp_info, YAFL_ST_INV_ARG_1);
  1104. np = sp_info->np;
  1105. YAFL_CHECK(np > 1, YAFL_ST_INV_ARG_1);
  1106. wc = _wc;
  1107. YAFL_CHECK(wc, YAFL_ST_INV_ARG_1);
  1108. /*Will be checked by _compute_res*/
  1109. zrf = _uzrf;
  1110. xrf = _xrf;
  1111. pzx = _pzx;
  1112. sx = _sx;
  1113. sz = _sz;
  1114. x = _ux;
  1115. zp = _zp;
  1116. sigmas_x = _sigmas_x;
  1117. sigmas_z = _sigmas_z;
  1118. /* Compute Pzx */
  1119. YAFL_TRY(status, _compute_res(_kalman_self, nz, zrf, sz, sigmas_z, zp));
  1120. YAFL_TRY(status, _compute_res(_kalman_self, nx, xrf, sx, sigmas_x, x));
  1121. YAFL_TRY(status, yafl_math_set_vvtxn(nz, nx, pzx, sz, sx, wc[0]));
  1122. for (i = 1; i < np; i++)
  1123. {
  1124. YAFL_TRY(status, _compute_res(_kalman_self, nz, zrf, sz, sigmas_z + nz * i, zp));
  1125. YAFL_TRY(status, _compute_res(_kalman_self, nx, xrf, sx, sigmas_x + nx * i, x));
  1126. YAFL_TRY(status, yafl_math_add_vvtxn(nz, nx, pzx, sz, sx, wc[i]));
  1127. }
  1128. return status;
  1129. }
  1130. /*---------------------------------------------------------------------------*/
  1131. yaflStatusEn yafl_ukf_base_update(yaflUKFBaseSt * self, yaflFloat * z, \
  1132. yaflKalmanScalarUpdateP scalar_update)
  1133. {
  1134. yaflStatusEn status = YAFL_ST_OK;
  1135. yaflInt np;
  1136. yaflInt nx;
  1137. yaflInt nz;
  1138. yaflInt i;
  1139. yaflUKFSigmaSt * sp_info; /*Sigma point generator info*/
  1140. YAFL_CHECK(scalar_update, YAFL_ST_INV_ARG_3);
  1141. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  1142. nx = _unx;
  1143. YAFL_CHECK(nx, YAFL_ST_INV_ARG_1);
  1144. nz = _unz;
  1145. YAFL_CHECK(nz, YAFL_ST_INV_ARG_1);
  1146. YAFL_CHECK(_pzx, YAFL_ST_INV_ARG_1);
  1147. YAFL_CHECK(_sigmas_x, YAFL_ST_INV_ARG_1);
  1148. sp_info = self->sp_info;
  1149. YAFL_CHECK(sp_info, YAFL_ST_INV_ARG_1);
  1150. np = sp_info->np;
  1151. YAFL_CHECK(np > 1, YAFL_ST_INV_ARG_1);
  1152. /* Compute measurement sigmas and zp*/
  1153. YAFL_TRY(status, _yafl_ukf_compute_sigmas_z_and_zp(self));
  1154. /* Compute Pzx = H.dot(Up.dot(Dp.dot(Up.T))*/
  1155. YAFL_TRY(status, _yafl_ukf_compute_pzx(self));
  1156. /*Now _sigmas_z and _sigmas_x may be spoiled*/
  1157. /*Compute H.dot(Up)*/
  1158. for (i = 0; i < nz; i++)
  1159. {
  1160. yaflFloat * h;
  1161. h = _pzx + nx * i;
  1162. YAFL_TRY(status, yafl_math_ruv (nx, h, _uup));
  1163. YAFL_TRY(status, YAFL_MATH_SET_RDV(nx, h, _udp, h));
  1164. }
  1165. /*Update R if needed!*/
  1166. if (_kalman_self->rff > 0.0)
  1167. {
  1168. yaflStatusEn status_r = status; /*For quiet regularization*/
  1169. yaflFloat * d;
  1170. yaflFloat * w;
  1171. /*
  1172. WARNING:
  1173. 1. _sigmas_x and _sigmas_z must be parts of the larger
  1174. memory pool which has minimum size of (nx + nz) * (nz + 1)
  1175. or np * (nx + nz) where np is number of sigma points
  1176. 2. _sigmas_x must be at start of this pool
  1177. */
  1178. d = _sigmas_x;
  1179. w = _sigmas_x + nx + nz;
  1180. /*Check input data*/
  1181. YAFL_CHECK(_udr, YAFL_ST_INV_ARG_1);
  1182. /*Start R update*/
  1183. YAFL_TRY(status_r, yafl_math_bset_m(nx + nz, w, nz, nx, _pzx));
  1184. /*Now W = (HUp|***) */
  1185. YAFL_TRY(status_r, \
  1186. _yafl_r_update(nx, nz, _kalman_self->rff, _udp, \
  1187. _uur, _udr, w, d, _uy));
  1188. /*Updated R, now _uy may be spoiled*/
  1189. }
  1190. /*Compute H*/
  1191. for (i = 0; i < nz; i++)
  1192. {
  1193. YAFL_TRY(status, yafl_math_rutv(nx, _pzx + nx * i, _uup));
  1194. }
  1195. /*Now _pzx = H*/
  1196. /*Compute innovation*/
  1197. YAFL_TRY(status, _compute_res(_kalman_self, nz, _uzrf, _uy, z, _zp));
  1198. /* Decorrelate measurements*/
  1199. YAFL_TRY(status, yafl_math_ruv(nz, _uy, _uur));
  1200. YAFL_TRY(status, yafl_math_rum(nz, nx, _pzx, _uur));
  1201. /*Start log likelihood computation*/
  1202. *_kalman_self->l = nz * YAFL_L2PI;
  1203. /*Now we can do scalar updates*/
  1204. for (i = 0; i < nz; i++)
  1205. {
  1206. YAFL_TRY(status, scalar_update(_kalman_self, i));
  1207. }
  1208. /*Finalize log likelihood value*/
  1209. *_kalman_self->l *= -0.5;
  1210. YAFL_TRY(status, _yafl_ukf_compute_residual(self, z));
  1211. return status;
  1212. }
  1213. /*=============================================================================
  1214. Bierman UKF
  1215. =============================================================================*/
  1216. #define _ukf_self ((yaflUKFBaseSt *)self)
  1217. #define _upzx (_ukf_self->Pzx)
  1218. #define _usx (_ukf_self->Sx)
  1219. #define YAFL_UKF_BIERMAN_SELF_INTERNALS_CHECKS() \
  1220. do { \
  1221. YAFL_CHECK(_nx > 0, YAFL_ST_INV_ARG_1); \
  1222. YAFL_CHECK(_up, YAFL_ST_INV_ARG_1); \
  1223. YAFL_CHECK(_dp, YAFL_ST_INV_ARG_1); \
  1224. YAFL_CHECK(_upzx, YAFL_ST_INV_ARG_1); \
  1225. YAFL_CHECK(_usx, YAFL_ST_INV_ARG_1); \
  1226. YAFL_CHECK(_y, YAFL_ST_INV_ARG_1); \
  1227. YAFL_CHECK(_dr, YAFL_ST_INV_ARG_1); \
  1228. } while (0)
  1229. /*---------------------------------------------------------------------------*/
  1230. yaflStatusEn yafl_ukf_bierman_update_scalar(yaflKalmanBaseSt * self, yaflInt i)
  1231. {
  1232. yaflStatusEn status = YAFL_ST_OK;
  1233. yaflInt nx;
  1234. yaflFloat * v;
  1235. YAFL_SCALAR_UPDATE_ARGS_CHECKS();
  1236. nx = _nx;
  1237. YAFL_UKF_BIERMAN_SELF_INTERNALS_CHECKS();
  1238. v = _upzx + nx * i;/*h is stored in v*/
  1239. # define f _usx
  1240. /* f = h.T.dot(Up) */
  1241. YAFL_TRY(status, yafl_math_set_vtu(nx, f, v, _up));
  1242. /* v = f.dot(Dp).T = Dp.dot(f.T).T */
  1243. YAFL_TRY(status, YAFL_MATH_SET_DV(nx, v, _dp, f));
  1244. YAFL_TRY(status, \
  1245. _bierman_update_body(nx, _x, _up, _dp, f, v, _dr[i], _y[i], \
  1246. 1.0, 1.0, _ul));
  1247. # undef f
  1248. return status;
  1249. }
  1250. /*=============================================================================
  1251. Adaptive Bierman UKF
  1252. =============================================================================*/
  1253. yaflStatusEn yafl_ukf_adaptive_bierman_update_scalar(yaflKalmanBaseSt * self, yaflInt i)
  1254. {
  1255. yaflStatusEn status = YAFL_ST_OK;
  1256. yaflInt nx;
  1257. yaflFloat * v;
  1258. yaflFloat nu;
  1259. yaflFloat r;
  1260. yaflFloat ac;
  1261. YAFL_SCALAR_UPDATE_ARGS_CHECKS();
  1262. nx = _nx;
  1263. YAFL_UKF_BIERMAN_SELF_INTERNALS_CHECKS();
  1264. v = _upzx + nx * i;/*h is stored in v*/
  1265. # define f _usx
  1266. /* f = h.T.dot(Up) */
  1267. YAFL_TRY(status, yafl_math_set_vtu(nx, f, v, _up));
  1268. /* v = f.dot(Dp).T = Dp.dot(f.T).T */
  1269. YAFL_TRY(status, YAFL_MATH_SET_DV(nx, v, _dp, f));
  1270. nu = self->y[i];
  1271. r = self->Dr[i];
  1272. YAFL_TRY(status, \
  1273. _adaptive_correction(nx, &ac, 0, f, v, r, nu, 1.0, \
  1274. ((yaflUKFAdaptivedSt *)self)->chi2));
  1275. YAFL_TRY(status, \
  1276. _bierman_update_body(nx, _x, _up, _dp, f, v, r, _y[i], ac, 1.0, _ul));
  1277. # undef f
  1278. return status;
  1279. }
  1280. /*=============================================================================
  1281. Robust Bierman UKF
  1282. =============================================================================*/
  1283. #define YAFL_UKF_SCALAR_ROBUSTIFY(_self, _gdot_res, _nu, _r) \
  1284. _scalar_robustify(self, \
  1285. ((yaflUKFRobustSt *)self)->g, \
  1286. ((yaflUKFRobustSt *)self)->gdot, \
  1287. _gdot_res, _nu, _r)
  1288. yaflStatusEn yafl_ukf_robust_bierman_update_scalar(yaflKalmanBaseSt * self, \
  1289. yaflInt i)
  1290. {
  1291. yaflStatusEn status = YAFL_ST_OK;
  1292. yaflInt nx;
  1293. yaflFloat gdot = 1.0;
  1294. yaflFloat nu = 0.0;
  1295. yaflFloat r;
  1296. yaflFloat * v;
  1297. YAFL_SCALAR_UPDATE_ARGS_CHECKS();
  1298. nx = _nx;
  1299. YAFL_UKF_BIERMAN_SELF_INTERNALS_CHECKS();
  1300. r = _dr[i];
  1301. nu = _y[i];
  1302. YAFL_TRY(status, YAFL_UKF_SCALAR_ROBUSTIFY(self, &gdot, &nu, r));
  1303. v = _upzx + nx * i;/*h is stored in v*/
  1304. # define f _usx
  1305. /* f = h.T.dot(Up) */
  1306. YAFL_TRY(status, yafl_math_set_vtu(nx, f, v, _up));
  1307. /* v = f.dot(Dp).T = Dp.dot(f.T).T */
  1308. YAFL_TRY(status, YAFL_MATH_SET_DV(nx, v, _dp, f));
  1309. YAFL_TRY(status, \
  1310. _bierman_update_body(nx, _x, _up, _dp, f, v, r, nu, \
  1311. 1.0, gdot, _ul));
  1312. # undef f
  1313. return status;
  1314. }
  1315. /*=============================================================================
  1316. Adaptive robust Bierman UKF
  1317. =============================================================================*/
  1318. yaflStatusEn \
  1319. yafl_ukf_adaptive_robust_bierman_update_scalar(yaflKalmanBaseSt * self, \
  1320. yaflInt i)
  1321. {
  1322. yaflStatusEn status = YAFL_ST_OK;
  1323. yaflInt nx;
  1324. yaflFloat gdot = 1.0;
  1325. yaflFloat ac = 1.0;
  1326. yaflFloat nu = 0.0;
  1327. yaflFloat r;
  1328. yaflFloat * v;
  1329. YAFL_SCALAR_UPDATE_ARGS_CHECKS();
  1330. nx = _nx;
  1331. YAFL_UKF_BIERMAN_SELF_INTERNALS_CHECKS();
  1332. r = _dr[i];
  1333. nu = _y[i];
  1334. YAFL_TRY(status, YAFL_UKF_SCALAR_ROBUSTIFY(self, &gdot, &nu, r));
  1335. v = _upzx + nx * i;/*h is stored in v*/
  1336. # define f _usx
  1337. /* f = h.T.dot(Up) */
  1338. YAFL_TRY(status, yafl_math_set_vtu(nx, f, v, _up));
  1339. /* v = f.dot(Dp).T = Dp.dot(f.T).T */
  1340. YAFL_TRY(status, YAFL_MATH_SET_DV(nx, v, _dp, f));
  1341. YAFL_TRY(status, \
  1342. _adaptive_correction(nx, &ac, 0, f, v, r, nu, gdot, \
  1343. ((yaflUKFAdaptiveRobustSt *)self)->chi2));
  1344. YAFL_TRY(status, \
  1345. _bierman_update_body(nx, _x, _up, _dp, f, v, r, nu, ac, gdot, _ul));
  1346. # undef f
  1347. return status;
  1348. }
  1349. /*=============================================================================
  1350. Full UKF, not a sequential square root version of UKF
  1351. =============================================================================*/
  1352. #define _uus (((yaflUKFSt *)self)->Us)
  1353. #define _uds (((yaflUKFSt *)self)->Ds)
  1354. static inline yaflStatusEn _yafl_ukf_compute_ms_zp_s(yaflUKFBaseSt * self)
  1355. {
  1356. yaflStatusEn status = YAFL_ST_OK;
  1357. yaflUKFSigmaSt * sp_info;
  1358. yaflFloat * sigmas_x;
  1359. yaflFloat * sigmas_z;
  1360. yaflFloat * dr;
  1361. yaflFloat * ur;
  1362. yaflFloat rff;
  1363. yaflInt np;
  1364. yaflInt nz;
  1365. yaflInt nx;
  1366. yaflInt i;
  1367. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  1368. nx = _unx;
  1369. YAFL_CHECK(nx, YAFL_ST_INV_ARG_1);
  1370. nz = _unz;
  1371. YAFL_CHECK(nz, YAFL_ST_INV_ARG_1);
  1372. sp_info = self->sp_info;
  1373. YAFL_CHECK(sp_info, YAFL_ST_INV_ARG_1);
  1374. np = sp_info->np;
  1375. YAFL_CHECK(np > 1, YAFL_ST_INV_ARG_1);
  1376. YAFL_CHECK(_uhx, YAFL_ST_INV_ARG_1);
  1377. sigmas_x = _sigmas_x;
  1378. YAFL_CHECK(sigmas_x, YAFL_ST_INV_ARG_1);
  1379. sigmas_z = _sigmas_z;
  1380. YAFL_CHECK(sigmas_z, YAFL_ST_INV_ARG_1);
  1381. /* Compute measurement sigmas */
  1382. for (i = 0; i < np; i++)
  1383. {
  1384. YAFL_TRY(status, _uhx(_kalman_self, sigmas_z + nz * i, sigmas_x + nx * i));
  1385. }
  1386. ur = _uur;
  1387. dr = _udr;
  1388. rff = _kalman_self->rff;
  1389. if (rff > 0.0)
  1390. {
  1391. /* Compute zp, update Ur, Dr, compute Us, Ds */
  1392. yaflFloat tmp;
  1393. yaflStatusEn r_status = YAFL_ST_OK; /*For silent R regularization*/
  1394. YAFL_CHECK(rff < 1.0, YAFL_ST_INV_ARG_1);
  1395. YAFL_CHECK(dr, YAFL_ST_INV_ARG_1);
  1396. YAFL_TRY(status, _unscented_mean(self, nz, _zp, np, sigmas_z, _zmf));
  1397. /*Update Ur, Dr*/
  1398. /* R *= 1.0- rff */
  1399. tmp = 1.0 - rff;
  1400. for (i=0; i < nz; i++)
  1401. {
  1402. dr[i] *= tmp;
  1403. }
  1404. /* R += rff * y.dot(y.T) */
  1405. YAFL_TRY(r_status, yafl_math_udu_up(nz, ur, dr, rff, _uy));
  1406. /* R += rff * Pzz*/
  1407. YAFL_TRY(r_status, _unscented_update(self, nz, ur, dr, _zp, \
  1408. np, sigmas_z, _sz, _uzrf, rff));
  1409. /*Compute Us, Ds*/
  1410. memcpy((void *)_uus, (void *)ur, (nz * (nz - 1)) / 2 * sizeof(yaflFloat));
  1411. memcpy((void *)_uds, (void *)dr, nz * sizeof(yaflFloat));
  1412. YAFL_TRY(status, _unscented_update(self, nz, _uus, _uds, _zp, \
  1413. np, sigmas_z, _sz, _uzrf, 1.0));
  1414. }
  1415. else
  1416. {
  1417. /* Compute zp, Us, Ds */
  1418. YAFL_TRY(status, \
  1419. _unscented_transform(self, nz, _zp, _uus, _uds, \
  1420. _sz, sigmas_z, ur, dr, _zmf, _uzrf));
  1421. }
  1422. return status;
  1423. }
  1424. /*---------------------------------------------------------------------------*/
  1425. static inline yaflStatusEn yafl_ukf_update_epilogue(yaflUKFBaseSt * self, yaflFloat * z)
  1426. {
  1427. yaflStatusEn status = YAFL_ST_OK;
  1428. yaflInt nz;
  1429. yaflInt nx;
  1430. yaflInt i;
  1431. yaflFloat * y;
  1432. yaflFloat * ds;
  1433. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  1434. y = _uy;
  1435. YAFL_CHECK(y, YAFL_ST_INV_ARG_1);
  1436. nx = _unx;
  1437. YAFL_CHECK(nx, YAFL_ST_INV_ARG_1);
  1438. nz = _unz;
  1439. YAFL_CHECK(nz, YAFL_ST_INV_ARG_1);
  1440. ds = _uds;
  1441. YAFL_CHECK(ds, YAFL_ST_INV_ARG_1);
  1442. /* Compute Pzx */
  1443. YAFL_TRY(status, _yafl_ukf_compute_pzx(self));
  1444. /* Decorrelate measurements part 2*/
  1445. YAFL_TRY(status, yafl_math_rum(nz, nx, _pzx, _uus));
  1446. /*Start log likelihood computation*/
  1447. *_kalman_self->l = nz * YAFL_L2PI;
  1448. /*Now we can do scalar updates*/
  1449. for (i = 0; i < nz; i++)
  1450. {
  1451. yaflFloat * pzxi;
  1452. pzxi = _pzx + nx * i;
  1453. /*
  1454. self.x += K * y[i]
  1455. K * y[i] = Pzx[i].T / ds[i] * y[i] = Pzx[i].T * (y[i] / ds[i])
  1456. self.x += Pzx[i].T * (y[i] / ds[i])
  1457. */
  1458. YAFL_TRY(status, yafl_math_add_vxn(nx, _ux, pzxi, y[i] / ds[i]));
  1459. /*
  1460. P -= K.dot(S.dot(K.T))
  1461. K.dot(S.dot(K.T)) = (Pzx[i].T / ds[i] * ds[i]).outer(Pzx[i] / ds[i]))
  1462. K.dot(S.dot(K.T)) = (Pzx[i].T).outer(Pzx[i]) / ds[i]
  1463. P -= (Pzx[i].T).outer(Pzx[i]) / ds[i]
  1464. Up, Dp = udu(P)
  1465. */
  1466. YAFL_TRY(status, yafl_math_udu_down(nx, _uup, _udp, 1.0 / ds[i], pzxi));
  1467. /*Compute log likelihood*/
  1468. *_kalman_self->l += y[i] * (y[i] / ds[i]) + YAFL_LOG(ds[i]);
  1469. }
  1470. /*Finalize log likelihood value*/
  1471. *_kalman_self->l *= -0.5;
  1472. YAFL_TRY(status, _yafl_ukf_compute_residual(self, z));
  1473. return status;
  1474. }
  1475. /*---------------------------------------------------------------------------*/
  1476. yaflStatusEn yafl_ukf_update(yaflUKFBaseSt * self, yaflFloat * z)
  1477. {
  1478. yaflStatusEn status = YAFL_ST_OK;
  1479. /* Compute measurement sigmas, zp, Us, Ds*/
  1480. YAFL_TRY(status, _yafl_ukf_compute_ms_zp_s(self));
  1481. /*Compute innovation*/
  1482. YAFL_TRY(status, _compute_res(_kalman_self, _unz, _uzrf, _uy, z, _zp));
  1483. /* Decorrelate measurements part 1*/
  1484. YAFL_TRY(status, yafl_math_ruv(_unz, _uy, _uus));
  1485. /*
  1486. Decorrelate measurements part 2, compute Pzx,
  1487. update P and x, compute residual if needed
  1488. */
  1489. YAFL_TRY(status, yafl_ukf_update_epilogue(self, z));
  1490. return status;
  1491. }
  1492. /*=============================================================================
  1493. Full adaptive UKF, not a sequential square root version of UKF
  1494. =============================================================================*/
  1495. static inline yaflStatusEn _ukf_compute_md(yaflUKFBaseSt * self, yaflFloat * z, yaflFloat * md)
  1496. {
  1497. yaflStatusEn status = YAFL_ST_OK;
  1498. yaflInt i;
  1499. yaflInt nz;
  1500. yaflFloat dist;
  1501. yaflFloat * y;
  1502. yaflFloat * ds;
  1503. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  1504. YAFL_CHECK(_uus, YAFL_ST_INV_ARG_1);
  1505. ds = _uds;
  1506. YAFL_CHECK(ds, YAFL_ST_INV_ARG_1);
  1507. nz = _unz;
  1508. YAFL_CHECK(nz, YAFL_ST_INV_ARG_1);
  1509. y = _uy;
  1510. YAFL_CHECK(y, YAFL_ST_INV_ARG_1);
  1511. YAFL_CHECK(z, YAFL_ST_INV_ARG_2);
  1512. YAFL_CHECK(md, YAFL_ST_INV_ARG_3);
  1513. YAFL_TRY(status, _compute_res(_kalman_self, nz, _uzrf, y, z, _zp));
  1514. YAFL_TRY(status, yafl_math_ruv(nz, y, _uus));
  1515. dist = 0;
  1516. for (i = 0; i < nz; i++)
  1517. {
  1518. /* Since ds is positive definite we won't check values. */
  1519. dist += y[i] * y[i] / ds[i];
  1520. }
  1521. *md = dist;
  1522. return status;
  1523. }
  1524. yaflStatusEn yafl_ukf_adaptive_update(yaflUKFBaseSt * self, yaflFloat * z)
  1525. {
  1526. yaflStatusEn status = YAFL_ST_OK;
  1527. yaflFloat delta;
  1528. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  1529. /* Compute measurement sigmas, zp, Us, Ds*/
  1530. YAFL_TRY(status, _yafl_ukf_compute_ms_zp_s(self));
  1531. /* Compute md, innovation, decorrelate measurements part 1*/
  1532. YAFL_TRY(status, _ukf_compute_md(self, z, &delta));
  1533. # define _chi2 (((yaflUKFFullAdapiveSt *)self)->chi2)
  1534. if (delta > _chi2)
  1535. {
  1536. yaflInt nz;
  1537. /* Adaptive correction */
  1538. yaflFloat ac;
  1539. nz = _unz;
  1540. YAFL_CHECK(nz, YAFL_ST_INV_ARG_1);
  1541. /* Compute correction factor, we don't need old _zp and _sigmas_z now*/
  1542. YAFL_TRY(status, \
  1543. _unscented_transform(self, nz, _zp, _uus, _uds, _sz, _sigmas_z, \
  1544. 0, 0, _zmf, _uzrf));
  1545. YAFL_TRY(status, _ukf_compute_md(self, z, &ac));
  1546. ac *= 1.0 / _chi2 - 1.0 / delta;
  1547. /* Correct _udp*/
  1548. YAFL_TRY(status, yafl_math_set_vxn(_unx, _udp, _udp, 1.0 + ac));
  1549. /* Generate new sigmas */
  1550. YAFL_TRY(status, yafl_ukf_gen_sigmas(self));
  1551. /* Now restart update with new sigmas */
  1552. YAFL_TRY(status, _yafl_ukf_compute_ms_zp_s(self));
  1553. /* Recompute innovation*/
  1554. YAFL_TRY(status, _compute_res(_kalman_self, nz, _uzrf, _uy, z, _zp));
  1555. /* Decorrelate measurements part 1*/
  1556. YAFL_TRY(status, yafl_math_ruv(nz, _uy, _uus));
  1557. }
  1558. # undef _chi2
  1559. /*
  1560. Decorrelate measurements part 2, compute Pzx,
  1561. update P and x, compute residual if needed
  1562. */
  1563. YAFL_TRY(status, yafl_ukf_update_epilogue(self, z));
  1564. return status;
  1565. }
  1566. /*=============================================================================
  1567. Van der Merwe sigma points generator
  1568. =============================================================================*/
  1569. static yaflStatusEn _merwe_compute_weights(yaflUKFBaseSt * self)
  1570. {
  1571. yaflStatusEn status = YAFL_ST_OK;
  1572. yaflInt np;
  1573. yaflInt nx;
  1574. yaflInt i;
  1575. yaflFloat * wc;
  1576. yaflFloat * wm;
  1577. yaflUKFSigmaSt * sp_info;
  1578. yaflFloat lambda;
  1579. yaflFloat alpha;
  1580. yaflFloat c;
  1581. yaflFloat d;
  1582. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  1583. nx = _unx;
  1584. YAFL_CHECK(_unx, YAFL_ST_INV_ARG_1);
  1585. YAFL_CHECK(_wm, YAFL_ST_INV_ARG_1);
  1586. wm = _wm;
  1587. YAFL_CHECK(_wc, YAFL_ST_INV_ARG_1);
  1588. wc = _wc;
  1589. YAFL_CHECK(self->sp_info, YAFL_ST_INV_ARG_1);
  1590. sp_info = self->sp_info;
  1591. YAFL_CHECK(sp_info->np, YAFL_ST_INV_ARG_1);
  1592. np = sp_info->np - 1; /*Achtung!!!*/
  1593. alpha = ((yaflUKFMerweSt *)sp_info)->alpha;
  1594. alpha *= alpha;
  1595. #define _alpha2 alpha
  1596. lambda = _alpha2 * (nx + ((yaflUKFMerweSt *)sp_info)->kappa) - nx;
  1597. d = lambda / (nx + lambda);
  1598. wm[np] = d;
  1599. wc[np] = d + (1.0 - _alpha2 + ((yaflUKFMerweSt *)sp_info)->beta);
  1600. c = 0.5 / (nx + lambda);
  1601. for (i = np - 1; i >= 0; i--)
  1602. {
  1603. wm[i] = c;
  1604. wc[i] = c;
  1605. }
  1606. #undef _alpha2
  1607. return status;
  1608. }
  1609. /*---------------------------------------------------------------------------*/
  1610. static inline yaflStatusEn _add_delta(yaflUKFBaseSt * self, \
  1611. yaflUKFSigmaAddP addf, yaflInt sz, \
  1612. yaflFloat * delta, yaflFloat * pivot, \
  1613. yaflFloat mult)
  1614. {
  1615. yaflStatusEn status = YAFL_ST_OK;
  1616. if (addf)
  1617. {
  1618. /* addf must be aware of self internal structure*/
  1619. /* delta = self.addf(delta, pivot, mult) */
  1620. YAFL_TRY(status, addf(self, delta, pivot, mult));
  1621. }
  1622. else
  1623. {
  1624. yaflInt j;
  1625. for (j = 0; j < sz; j++)
  1626. {
  1627. delta[j] = pivot[j] + mult * delta[j];
  1628. }
  1629. }
  1630. return status;
  1631. }
  1632. /*---------------------------------------------------------------------------*/
  1633. static yaflStatusEn _merwe_generate_points(yaflUKFBaseSt * self)
  1634. {
  1635. yaflStatusEn status = YAFL_ST_OK;
  1636. yaflInt nx;
  1637. yaflInt i;
  1638. yaflFloat * x;
  1639. yaflFloat * sigmas_x;
  1640. yaflFloat * dp;
  1641. yaflUKFSigmaSt * sp_info;
  1642. yaflUKFSigmaAddP addf;
  1643. yaflFloat lambda_p_n;
  1644. yaflFloat alpha;
  1645. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  1646. nx = _unx;
  1647. YAFL_CHECK(nx, YAFL_ST_INV_ARG_1);
  1648. x = _ux;
  1649. YAFL_CHECK(x, YAFL_ST_INV_ARG_1);
  1650. YAFL_CHECK(_uup, YAFL_ST_INV_ARG_1);
  1651. dp = _udp;
  1652. YAFL_CHECK(dp, YAFL_ST_INV_ARG_1);
  1653. sigmas_x = _sigmas_x;
  1654. YAFL_CHECK(sigmas_x, YAFL_ST_INV_ARG_1);
  1655. YAFL_CHECK(self->sp_info, YAFL_ST_INV_ARG_1);
  1656. sp_info = self->sp_info;
  1657. alpha = ((yaflUKFMerweSt *)sp_info)->alpha;
  1658. lambda_p_n = alpha * alpha * (nx + ((yaflUKFMerweSt *)sp_info)->kappa);
  1659. YAFL_TRY(status, yafl_math_bset_ut(nx, sigmas_x, nx, _uup));
  1660. memcpy((void *)(sigmas_x + nx * nx), (void *)sigmas_x, \
  1661. nx * nx * sizeof(yaflFloat));
  1662. addf = sp_info->addf;
  1663. for (i = 0; i < nx; i++)
  1664. {
  1665. yaflFloat mult;
  1666. mult = YAFL_SQRT(dp[i] * lambda_p_n);
  1667. YAFL_TRY(status, \
  1668. _add_delta(self, addf, nx, sigmas_x + nx * i, x, mult));
  1669. YAFL_TRY(status, \
  1670. _add_delta(self, addf, nx, sigmas_x + nx * (nx + i), x, - mult));
  1671. }
  1672. memcpy((void *)(sigmas_x + 2 * nx * nx), (void *)x, nx * sizeof(yaflFloat));
  1673. return status;
  1674. }
  1675. /*---------------------------------------------------------------------------*/
  1676. const yaflUKFSigmaMethodsSt yafl_ukf_merwe_spm =
  1677. {
  1678. .wf = _merwe_compute_weights,
  1679. .spgf = _merwe_generate_points
  1680. };
  1681. /*=============================================================================
  1682. Julier sigma points generator
  1683. =============================================================================*/
  1684. static yaflStatusEn _julier_compute_weights(yaflUKFBaseSt * self)
  1685. {
  1686. yaflStatusEn status = YAFL_ST_OK;
  1687. yaflInt np;
  1688. yaflInt nx;
  1689. yaflInt i;
  1690. yaflFloat * wc;
  1691. yaflFloat * wm;
  1692. yaflUKFSigmaSt * sp_info;
  1693. yaflFloat kappa;
  1694. yaflFloat c;
  1695. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  1696. nx = _unx;
  1697. YAFL_CHECK(_unx, YAFL_ST_INV_ARG_1);
  1698. YAFL_CHECK(_wm, YAFL_ST_INV_ARG_1);
  1699. wm = _wm;
  1700. YAFL_CHECK(_wc, YAFL_ST_INV_ARG_1);
  1701. wc = _wc;
  1702. YAFL_CHECK(self->sp_info, YAFL_ST_INV_ARG_1);
  1703. sp_info = self->sp_info;
  1704. YAFL_CHECK(sp_info->np, YAFL_ST_INV_ARG_1);
  1705. np = sp_info->np - 1; /*Achtung!!!*/
  1706. kappa = ((yaflUKFJulierSt *)sp_info)->kappa;
  1707. wm[np] = kappa / (nx + kappa);
  1708. wc[np] = wm[np];
  1709. c = 0.5 / (nx + kappa);
  1710. for (i = np - 1; i >= 0; i--)
  1711. {
  1712. wm[i] = c;
  1713. wc[i] = c;
  1714. }
  1715. return status;
  1716. }
  1717. /*---------------------------------------------------------------------------*/
  1718. static yaflStatusEn _julier_generate_points(yaflUKFBaseSt * self)
  1719. {
  1720. yaflStatusEn status = YAFL_ST_OK;
  1721. yaflInt nx;
  1722. yaflInt i;
  1723. yaflFloat * x;
  1724. yaflFloat * sigmas_x;
  1725. yaflFloat * dp;
  1726. yaflUKFSigmaSt * sp_info;
  1727. yaflUKFSigmaAddP addf;
  1728. yaflFloat kappa_p_n;
  1729. YAFL_CHECK(self, YAFL_ST_INV_ARG_1);
  1730. nx = _unx;
  1731. YAFL_CHECK(nx, YAFL_ST_INV_ARG_1);
  1732. x = _ux;
  1733. YAFL_CHECK(x, YAFL_ST_INV_ARG_1);
  1734. YAFL_CHECK(_uup, YAFL_ST_INV_ARG_1);
  1735. dp = _udp;
  1736. YAFL_CHECK(dp, YAFL_ST_INV_ARG_1);
  1737. sigmas_x = _sigmas_x;
  1738. YAFL_CHECK(sigmas_x, YAFL_ST_INV_ARG_1);
  1739. YAFL_CHECK(self->sp_info, YAFL_ST_INV_ARG_1);
  1740. sp_info = self->sp_info;
  1741. kappa_p_n = ((yaflUKFJulierSt *)sp_info)->kappa + nx;
  1742. YAFL_TRY(status, yafl_math_bset_ut(nx, sigmas_x, nx, _uup));
  1743. memcpy((void *)(sigmas_x + nx * nx), (void *)sigmas_x, \
  1744. nx * nx * sizeof(yaflFloat));
  1745. addf = sp_info->addf;
  1746. for (i = 0; i < nx; i++)
  1747. {
  1748. yaflFloat mult;
  1749. mult = YAFL_SQRT(dp[i] * kappa_p_n);
  1750. YAFL_TRY(status, \
  1751. _add_delta(self, addf, nx, sigmas_x + nx * i, x, mult));
  1752. YAFL_TRY(status, \
  1753. _add_delta(self, addf, nx, sigmas_x + nx * (nx + i), x, - mult));
  1754. }
  1755. memcpy((void *)(sigmas_x + 2 * nx * nx), (void *)x, nx * sizeof(yaflFloat));
  1756. return status;
  1757. }
  1758. /*---------------------------------------------------------------------------*/
  1759. const yaflUKFSigmaMethodsSt yafl_ukf_julier_spm =
  1760. {
  1761. .wf = _julier_compute_weights,
  1762. .spgf = _julier_generate_points
  1763. };
  1764. /*=============================================================================
  1765. Undef UKF stuff
  1766. =============================================================================*/
  1767. #undef _kalman_self
  1768. #undef _ufx
  1769. #undef _uhx
  1770. #undef _uzrf
  1771. #undef _ux
  1772. #undef _uy
  1773. #undef _uup
  1774. #undef _udp
  1775. #undef _uuq
  1776. #undef _udq
  1777. #undef _uur
  1778. #undef _udr
  1779. #undef _ul
  1780. #undef _unx
  1781. #undef _unz
  1782. #undef _xrf
  1783. #undef _xmf
  1784. #undef _zmf
  1785. #undef _zp
  1786. #undef _sx
  1787. #undef _sz
  1788. #undef _pzx
  1789. #undef _sigmas_x
  1790. #undef _sigmas_z
  1791. #undef _wm
  1792. #undef _wc
  1793. /*----------------------------------------------------------------------------*/
  1794. #undef _ukf_self
  1795. #undef _upzx
  1796. #undef _usx
  1797. /*----------------------------------------------------------------------------*/
  1798. #undef _uus
  1799. #undef _uds
  1800. /*=============================================================================
  1801. Undef Kalman filter stuff
  1802. =============================================================================*/
  1803. #undef _fx
  1804. #undef _hx
  1805. #undef _zrf
  1806. #undef _x
  1807. #undef _y
  1808. #undef _up
  1809. #undef _dp
  1810. #undef _uq
  1811. #undef _dq
  1812. #undef _ur
  1813. #undef _dr
  1814. #undef _l
  1815. #undef _nx
  1816. #undef _nz