Created
May 13, 2018 19:51
-
-
Save charles-l/f506baa3f686b3ae59b5a87422e777a5 to your computer and use it in GitHub Desktop.
Kalman filter implementation in the J Language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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