yaflpy_compile_unscented.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Copyright 2021 anonimous <shkolnick-kun@gmail.com> and contributors.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing,
  10. software distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions
  13. """
  14. import matplotlib.pyplot as plt
  15. import numpy as np
  16. import pyximport
  17. import scipy.stats
  18. import sys
  19. import time
  20. """
  21. sys.path.insert(0,'../../src/yaflpy')
  22. pyximport.install(
  23. build_dir='../projects/obj',
  24. pyimport=True,
  25. reload_support=True,
  26. language_level=3,
  27. setup_args={
  28. 'include_dirs' : [np.get_include(), '../../src', '../../src/yaflpy'],
  29. 'define_macros':[("NPY_NO_DEPRECATED_API", "NPY_1_7_API_VERSION")]
  30. }
  31. )
  32. """
  33. from yaflpy import MerweSigmaPoints as SP
  34. #from yaflpy import Unscented as KF
  35. #from yaflpy import UnscentedAdaptive as KF
  36. #from yaflpy import UnscentedBierman as KF
  37. from yaflpy import UnscentedAdaptiveBierman as KF
  38. def _fx(x, dt, **fx_args):
  39. x = x.copy()
  40. x[0] += x[1] * dt
  41. x[2] += x[3] * dt
  42. return x
  43. def _hx(x, **hx_args):
  44. if hx_args:
  45. print(hx_args)
  46. return np.array([x[0], x[2]])
  47. def _zrf(a,b):
  48. #print(a - b)
  49. return a - b
  50. STD = 100.
  51. sp = SP(4, 0.1, 2., 0)
  52. kf = KF(4, 2, 1., _hx, _fx, sp)
  53. #kf = KF(4, 2, 1., _hx, _fx, sp, residual_z=_zrf)
  54. kf.x[0] = 0.
  55. kf.x[1] = 0.3
  56. kf.Dp *= .00001
  57. kf.Dq *= 1.0e-8
  58. #This is robust filter, so no square here
  59. kf.Dr *= STD*STD
  60. kf.Dr[0] *= .75
  61. kf.Ur += .5
  62. #kf.chi2 = 8.807468393511947
  63. # sp = MerweScaledSigmaPoints(4, alpha=.1, beta=2., kappa=0)
  64. # kf = UnscentedKalmanFilter(4, 2, dt=1., fx=_fx, hx=_hx, points=sp)
  65. # kf.P *= .1
  66. # kf.Q *= 1e-8
  67. # kf.R *= STD*STD
  68. print(kf.x)
  69. N = 6000
  70. clean = np.zeros((N, 2))
  71. noisy = np.zeros((N, 2))
  72. t = np.zeros((N,), dtype=np.float)
  73. # for i in range(1, len(clean)//2):
  74. # clean[i] = clean[i-1] + np.array([1.5,1.])
  75. # noisy[i] = clean[i] + np.random.normal(scale=STD, size=2)
  76. # t[i] = i
  77. # for i in range(i, len(clean)):
  78. # clean[i] = clean[i-1] + np.array([1.,1.5])
  79. # noisy[i] = clean[i] + np.random.normal(scale=STD, size=2)
  80. # t[i] = i
  81. for i in range(1, len(clean)):
  82. clean[i] = clean[i-1] + np.array([1.,1.])
  83. noisy[i] = clean[i] + np.random.normal(scale=STD, size=2)
  84. t[i] = i
  85. kf_out = np.zeros((N, 2))
  86. start = time.time()
  87. for i, z in enumerate(noisy):
  88. kf.predict()
  89. kf.update(z)
  90. kf_out[i] = kf.zp
  91. end = time.time()
  92. print(end - start)
  93. plt.plot(t, noisy - kf_out)
  94. plt.show()
  95. plt.plot(t, clean - kf_out)
  96. plt.show()
  97. plt.plot(clean[:,0], clean[:,1], kf_out[:,0], kf_out[:,1])
  98. plt.show()
  99. plt.plot(noisy[:,0], noisy[:,1], kf_out[:,0], kf_out[:,1])
  100. plt.show()
  101. plt.plot(t, noisy[:,1], t, kf_out[:,1], t, clean[:,1])
  102. plt.show()
  103. plt.plot(t, noisy[:,0], t, kf_out[:,0], t, clean[:,0])
  104. plt.show()
  105. print('Done!')