yaflpy_compile_unscented_robust.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 UnscentedRobustBierman as KF
  32. from yaflpy import UnscentedAdaptiveRobustBierman as KF
  33. def _fx(x, dt, **fx_args):
  34. x = x.copy()
  35. x[0] += x[1] * dt
  36. x[2] += x[3] * dt
  37. return x
  38. def _hx(x, **hx_args):
  39. if hx_args:
  40. print(hx_args)
  41. return np.array([x[0], x[2]])
  42. def _gz(beta):
  43. # +- 3*sigma
  44. if 3.0 >= np.abs(beta):
  45. return float(beta)
  46. # +- 6*sigma - uncertain measurements
  47. if 6.0 >= np.abs(beta):
  48. return float(beta/3.0)
  49. # outliers
  50. return float(np.sign(beta))
  51. def _gdotz(beta):
  52. # +- 3*sigma
  53. if 3.0 >= np.abs(beta):
  54. return 1.0
  55. # +- 6*sigma - uncertain measurements
  56. if 6.0 >= np.abs(beta):
  57. return 1.0/3.0
  58. # outliers
  59. return 0.0
  60. def _zrf(a,b):
  61. #print(a - b)
  62. return a - b
  63. STD = 100.
  64. sp = SP(4, 0.1, 2., 0)
  65. kf = KF(4, 2, 1., _hx, _fx, sp, gz=_gz, gdotz=_gdotz)
  66. #kf = KF(4, 2, 1., _hx, _fx, sp, residual_z=_zrf)
  67. kf.x[0] = 0.
  68. kf.x[1] = 0.3
  69. kf.Dp *= .00001
  70. kf.Dq *= 1.0e-8
  71. #This is robust filter, so no square here
  72. kf.Dr *= STD
  73. kf.Dr[0] *= .87
  74. kf.Ur += .5
  75. # sp = MerweScaledSigmaPoints(4, alpha=.1, beta=2., kappa=0)
  76. # kf = UnscentedKalmanFilter(4, 2, dt=1., fx=_fx, hx=_hx, points=sp)
  77. # kf.P *= .1
  78. # kf.Q *= 1e-8
  79. # kf.R *= STD*STD
  80. print(kf.x)
  81. N = 6000
  82. clean = np.zeros((N, 2))
  83. noisy = np.zeros((N, 2))
  84. t = np.zeros((N,), dtype=np.float)
  85. # for i in range(1, len(clean)//2):
  86. # clean[i] = clean[i-1] + np.array([1.5,1.])
  87. # noisy[i] = clean[i] + np.random.normal(scale=STD, size=2)
  88. # t[i] = i
  89. # for i in range(i, len(clean)):
  90. # clean[i] = clean[i-1] + np.array([1.,1.5])
  91. # noisy[i] = clean[i] + np.random.normal(scale=STD, size=2)
  92. # t[i] = i
  93. for i in range(1, len(clean)):
  94. clean[i] = clean[i-1] + np.array([1.,1.])
  95. noisy[i] = clean[i] + np.random.normal(scale=STD, size=2)
  96. t[i] = i
  97. kf_out = np.zeros((N, 2))
  98. start = time.time()
  99. for i, z in enumerate(noisy):
  100. kf.predict()
  101. kf.update(z)
  102. kf_out[i] = kf.zp
  103. end = time.time()
  104. print(end - start)
  105. plt.plot(t, noisy - kf_out)
  106. plt.show()
  107. plt.plot(t, clean - kf_out)
  108. plt.show()
  109. plt.plot(clean[:,0], clean[:,1], kf_out[:,0], kf_out[:,1])
  110. plt.show()
  111. plt.plot(noisy[:,0], noisy[:,1], kf_out[:,0], kf_out[:,1])
  112. plt.show()
  113. plt.plot(t, noisy[:,1], t, kf_out[:,1], t, clean[:,1])
  114. plt.show()
  115. plt.plot(t, noisy[:,0], t, kf_out[:,0], t, clean[:,0])
  116. plt.show()
  117. print('Done!')