Skip to content

Instantly share code, notes, and snippets.

@zsunberg
Last active June 19, 2019 18:01
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 zsunberg/4e0512cd88b56e95e028d46ded307390 to your computer and use it in GitHub Desktop.
Save zsunberg/4e0512cd88b56e95e028d46ded307390 to your computer and use it in GitHub Desktop.
Sketch of Extended Kalman Filter package usage
using ExtendedKalmanFilters
using Distributions
using DelimitedFiles
# We may also want to look at DynamicalSystems.jl
# The package should accept AbstractArrays wherever possible so people can use StaticArrays
# Model semantics
# x_{t+1} = f(x_t, u_t) + w_t
# y_t = h(x_t) + v_t # should the control be an argument of h?
# w_t ~ N(0,W)
# v_t ~ N(0,V)
# van der pol oscillator with Euler integration
function f(x, u)
xdot = [x[2], mu*(1-x[1]^2)*x[2] - x[1] + u]
return x + dt*xdot
end
h(x) = x[2]
W = [1.0 0.0; 0.0 1.0]
V = 1.0
dmodel = DiscreteTimeDynamics(f, W) # maybe something from DynamicSystems.jl
omodel = NonlinearObservationModel(h, V)
ekf = ExtendedKalmanFilter(dmodel, omodel)
b0 = MvNormal([1.0 0.0; 0.0 1.0]) # initial belief
u = [1.0] # example control
y = [1.1] # example observation
### Usage option 1: single step updating ###
# b_{k+1|k}
bkp1k = predict(ekf, b0, u)
# b_{k+1|k+1}
b = measure(ekf, bkp1k, y) # could also just be called update
### Usage option 2: with POMDPs.jl ###
# does both predict and measurement in one shot
b = POMDPs.update(ekf, b0, u, y)
### Usage Option 3: post-processing data ###
# filter a whole time series
ys = vec(readdlm("y.txt")) # vector of measurements
us = vec(readdlm("u.txt")) # vector of controls
bs = runfilter(ekf, b0, us, ys) # return a vector of normal distributions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment