Skip to content

Instantly share code, notes, and snippets.

@charles-l
Created May 13, 2018 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charles-l/f506baa3f686b3ae59b5a87422e777a5 to your computer and use it in GitHub Desktop.
Save charles-l/f506baa3f686b3ae59b5a87422e777a5 to your computer and use it in GitHub Desktop.
Kalman filter implementation in the J Language
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