yaflpy_compile_unscented.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Copyright 2020 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. sys.path.insert(0,'../../src/yaflpy')
  21. pyximport.install(
  22. build_dir='../projects/obj',
  23. pyimport=True,
  24. reload_support=True,
  25. language_level=3,
  26. setup_args={
  27. 'include_dirs': [np.get_include(), '../../src', '../../src/yaflpy'],
  28. }
  29. )
  30. from yaflpy import MerweSigmaPoints as SP
  31. #from yaflpy import Unscented as KF
  32. #from yaflpy import UnscentedAdaptive as KF
  33. #from yaflpy import UnscentedBierman as KF
  34. from yaflpy import UnscentedAdaptiveBierman as KF
  35. def _fx(x, dt, **fx_args):
  36. x = x.copy()
  37. x[0] += x[1] * dt
  38. x[2] += x[3] * dt
  39. return x
  40. def _hx(x, **hx_args):
  41. if hx_args:
  42. print(hx_args)
  43. return np.array([x[0], x[2]])
  44. def _zrf(a,b):
  45. #print(a - b)
  46. return a - b
  47. STD = 100.
  48. sp = SP(4, 0.1, 2., 0)
  49. kf = KF(4, 2, 1., _hx, _fx, sp)
  50. #kf = KF(4, 2, 1., _hx, _fx, sp, residual_z=_zrf)
  51. kf.x[0] = 0.
  52. kf.x[1] = 0.3
  53. kf.Dp *= .00001
  54. kf.Dq *= 1.0e-8
  55. #This is robust filter, so no square here
  56. kf.Dr *= STD*STD
  57. kf.Dr[0] *= .75
  58. kf.Ur += .5
  59. #kf.chi2 = 8.807468393511947
  60. # sp = MerweScaledSigmaPoints(4, alpha=.1, beta=2., kappa=0)
  61. # kf = UnscentedKalmanFilter(4, 2, dt=1., fx=_fx, hx=_hx, points=sp)
  62. # kf.P *= .1
  63. # kf.Q *= 1e-8
  64. # kf.R *= STD*STD
  65. print(kf.x)
  66. N = 6000
  67. clean = np.zeros((N, 2))
  68. noisy = np.zeros((N, 2))
  69. t = np.zeros((N,), dtype=np.float)
  70. # for i in range(1, len(clean)//2):
  71. # clean[i] = clean[i-1] + np.array([1.5,1.])
  72. # noisy[i] = clean[i] + np.random.normal(scale=STD, size=2)
  73. # t[i] = i
  74. # for i in range(i, len(clean)):
  75. # clean[i] = clean[i-1] + np.array([1.,1.5])
  76. # noisy[i] = clean[i] + np.random.normal(scale=STD, size=2)
  77. # t[i] = i
  78. for i in range(1, len(clean)):
  79. clean[i] = clean[i-1] + np.array([1.,1.])
  80. noisy[i] = clean[i] + np.random.normal(scale=STD, size=2)
  81. t[i] = i
  82. kf_out = np.zeros((N, 2))
  83. start = time.time()
  84. for i, z in enumerate(noisy):
  85. kf.predict()
  86. kf.update(z)
  87. kf_out[i] = kf.zp
  88. end = time.time()
  89. print(end - start)
  90. plt.plot(t, noisy - kf_out)
  91. plt.show()
  92. plt.plot(t, clean - kf_out)
  93. plt.show()
  94. plt.plot(clean[:,0], clean[:,1], kf_out[:,0], kf_out[:,1])
  95. plt.show()
  96. plt.plot(noisy[:,0], noisy[:,1], kf_out[:,0], kf_out[:,1])
  97. plt.show()
  98. plt.plot(t, noisy[:,1], t, kf_out[:,1], t, clean[:,1])
  99. plt.show()
  100. plt.plot(t, noisy[:,0], t, kf_out[:,0], t, clean[:,0])
  101. plt.show()
  102. print('Done!')