yaflpy_compile_robust.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 RobustJoseph as KF
  31. #from yaflpy import RobustBierman as KF
  32. #from yaflpy import AdaptiveRobustJoseph as KF
  33. from yaflpy import AdaptiveRobustBierman as KF
  34. def _fx(x, dt, **fx_args):
  35. x = x.copy()
  36. x[0] += x[1] * dt
  37. x[2] += x[3] * dt
  38. return x
  39. def _jfx(x, dt, **fx_args):
  40. F = np.array([
  41. [1., dt, 0., 0.],
  42. [0., 1., 0., 0.],
  43. [0., 0., 1., dt],
  44. [0., 0., 0., 1.],
  45. ])
  46. return F
  47. def _hx(x, **hx_args):
  48. if hx_args:
  49. print(hx_args)
  50. return np.array([x[0], x[2]])
  51. def _jhx(x, **hx_args):
  52. H = np.array([
  53. [1., 0., 0., 0.],
  54. [0., 0., 1., 0.],
  55. ])
  56. return H
  57. #Default noice model is Normal with Poisson outliers
  58. def _gz(beta):
  59. # +- 3*sigma
  60. if 3.0 >= np.abs(beta):
  61. return float(beta)
  62. # +- 6*sigma - uncertain measurements
  63. if 6.0 >= np.abs(beta):
  64. return float(beta/3.0)
  65. # outliers
  66. return float(np.sign(beta))
  67. def _gdotz(beta):
  68. # +- 3*sigma
  69. if 3.0 >= np.abs(beta):
  70. return 1.0
  71. # +- 6*sigma - uncertain measurements
  72. if 6.0 >= np.abs(beta):
  73. return 1.0/3.0
  74. # outliers
  75. return 0.0
  76. def _zrf(a,b):
  77. return a - b
  78. STD = 100.
  79. #kf = KF(4, 2, 1., _fx, _jfx, _hx, _jhx, residual_z=_zrf)
  80. #kf = KF(4, 2, 1., _fx, _jfx, _hx, _jhx, gz=_gz, gdotz=_gdotz)
  81. kf = KF(4, 2, 1., _fx, _jfx, _hx, _jhx)
  82. kf.x[0] = 1000.
  83. kf.x[1] = -0.5
  84. kf.Dp *= .00001
  85. kf.Dq *= 1.0e-8
  86. #This is robust filter, so no square here
  87. kf.Dr *= STD
  88. kf.Dr[0] *= .87
  89. kf.Ur += 0.5
  90. #kf.chi2 = 8.807468393511947
  91. N = 12000
  92. clean = np.zeros((N, 2))
  93. noisy = np.zeros((N, 2))
  94. t = np.zeros((N,), dtype=np.float)
  95. st = np.zeros((N,), dtype=np.int)
  96. for i in range(1, len(clean)//2):
  97. clean[i] = clean[i-1] + np.array([1.5,1.])
  98. noisy[i] = clean[i] + np.random.normal(scale=STD, size=2)
  99. t[i] = i
  100. for i in range(i, len(clean)):
  101. clean[i] = clean[i-1] + np.array([1.,3.])
  102. noisy[i] = clean[i] + np.random.normal(scale=STD, size=2)
  103. t[i] = i
  104. kf_out = np.zeros((N, 2))
  105. start = time.time()
  106. for i, z in enumerate(noisy):
  107. kf.predict()
  108. st[i] = kf.update(z)
  109. kf_out[i] = kf.x[::2]
  110. end = time.time()
  111. print(end - start)
  112. plt.plot(t, noisy - kf_out)
  113. plt.show()
  114. plt.plot(t, clean - kf_out)
  115. plt.show()
  116. plt.plot(clean[:,0], clean[:,1], kf_out[:,0], kf_out[:,1])
  117. plt.show()
  118. plt.plot(noisy[:,0], noisy[:,1], kf_out[:,0], kf_out[:,1])
  119. plt.show()
  120. plt.plot(t, noisy[:,1], t, kf_out[:,1], t, clean[:,1])
  121. plt.show()
  122. plt.plot(t, noisy[:,0], t, kf_out[:,0], t, clean[:,0])
  123. plt.show()
  124. plt.plot(t, [st[i] & yaflpy.ST_MSK_ANOMALY for i in range(N)])
  125. plt.show()
  126. plt.plot(t, [st[i] & yaflpy.ST_MSK_REGULARIZED for i in range(N)])
  127. plt.show()
  128. plt.plot(t, [st[i] & yaflpy.ST_MSK_GLITCH_SMALL for i in range(N)])
  129. plt.show()
  130. plt.plot(t, [st[i] & yaflpy.ST_MSK_GLITCH_LARGE for i in range(N)])
  131. plt.show()
  132. print('Done!')