| NB. Port of https://scipy-cookbook.readthedocs.io/items/KalmanFiltering.html | |
| require 'stats/distribs/normal' | |
| n =: 50 NB. number of steps to compute | |
| x =: _0.37227 NB. real value | |
| z =: (x , 0.1) rnorm n NB. readings (normal distribution) | |
| Q =: 1e_5 NB. process variance | |
| R =: 0.1^2 NB. estimate of variance | |
| NB. function to compute gain from previous P value | |
| Kk =: ((Q + {:) % (R + (Q + {:))) | |
| NB. error estimate | |
| P =: ($:@(, ((1 - Kk)*(Q + {:)) ) ` [ @. (n&=@#)) 1 | |
| NB. Kalman gain (i.e. how much readings are weighted) | |
| K =: (Q + (0,P)) % ((Q + (0,P)) + R) | |
| NB. estimate of x | |
| xhat =: ($:@(, ({: + ({&K@# * ({&z@# - {:)))) ` [ @.(n&=@#)) 0 | |
| NB. plot that sucker | |
| pd 'reset' | |
| pd 'type line' | |
| pd xhat | |
| pd (n $ x) | |
| pd 'type dot' | |
| pd 'pensize 2' | |
| pd z | |
| pd 'show' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment