yaflpy_compile.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 Bierman as KF
  31. #from yaflpy import Joseph as KF
  32. #from yaflpy import AdaptiveBierman as KF
  33. #from yaflpy import AdaptiveJoseph as KF
  34. from yaflpy import DoNotUseThisFilter 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. def _zrf(a,b):
  59. return a - b
  60. STD = 100.
  61. #kf = KF(4, 2, 1., _fx, _jfx, _hx, _jhx, residual_z=_zrf)
  62. kf = KF(4, 2, 1., _fx, _jfx, _hx, _jhx)
  63. kf.x[0] = 0.
  64. kf.x[1] = 0.3
  65. kf.Dp *= .00001
  66. kf.Dq *= 1.0e-8
  67. #This is robust filter, so no square here
  68. kf.Dr *= STD*STD
  69. kf.Dr[0] *= .75
  70. kf.Ur += 0.5
  71. N = 6000
  72. #kf.chi2 = 8.807468393511947
  73. clean = np.zeros((N, 2))
  74. noisy = np.zeros((N, 2))
  75. t = np.zeros((N,), dtype=np.float)
  76. # for i in range(1, len(clean)//2):
  77. # clean[i] = clean[i-1] + np.array([1.5,1.])
  78. # noisy[i] = clean[i] + np.random.normal(scale=STD, size=2)
  79. # t[i] = i
  80. # for i in range(i, len(clean)):
  81. # clean[i] = clean[i-1] + np.array([1.,10.])
  82. # noisy[i] = clean[i] + np.random.normal(scale=STD, size=2)
  83. # t[i] = i
  84. for i in range(1, len(clean)):
  85. clean[i] = clean[i-1] + np.array([1.,1.])
  86. noisy[i] = clean[i] + np.random.normal(scale=STD, size=2)
  87. t[i] = i
  88. kf_out = np.zeros((N, 2))
  89. start = time.time()
  90. for i, z in enumerate(noisy):
  91. kf.predict()
  92. kf.update(z)
  93. kf_out[i] = kf.x[::2]
  94. end = time.time()
  95. print(end - start)
  96. plt.plot(t, noisy - kf_out)
  97. plt.show()
  98. plt.plot(t, clean - kf_out)
  99. plt.show()
  100. plt.plot(clean[:,0], clean[:,1], kf_out[:,0], kf_out[:,1])
  101. plt.show()
  102. plt.plot(noisy[:,0], noisy[:,1], kf_out[:,0], kf_out[:,1])
  103. plt.show()
  104. plt.plot(t, noisy[:,1], t, kf_out[:,1], t, clean[:,1])
  105. plt.show()
  106. plt.plot(t, noisy[:,0], t, kf_out[:,0], t, clean[:,0])
  107. plt.show()
  108. print('Done!')