yaflpy_compile_robust.py 3.8 KB

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