-
Notifications
You must be signed in to change notification settings - Fork 10
/
kalman.jl
70 lines (63 loc) · 1.81 KB
/
kalman.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Kalman, GaussianDistributions, LinearAlgebra
using GaussianDistributions: ⊕ # independent sum of Gaussian r.v.
using Statistics
using GLMakie
using StaticArrays
function onestep(p, v)
v += @SVector randn(2)
w = 0.1 * @SVector randn(2)
p += v + w
p, v
end
x0 = zero(SVector{4, Float64}) #start point
P = SMatrix{4,4,Float64}(I)
# H: observation matrix
H = SMatrix{2,4,Float64}(I) # as only location is observed, not speed
# F: state-trasition
F = SMatrix{4,4,Float64}([
1 0 1 0
0 1 0 1
0 0 1 0
0 0 0 1
])
# Q: the covariance of the process noise
Q = SMatrix{4,4}(Diagonal([0.01, 0.01, 0.1, 0.1]))
# R: the covariance of the observation noise
R = SMatrix{2,2,Float64}(I)
p = @SVector zeros(2) # initial poistion
v = @SVector ones(2) # initial velocity
n = 100
pp = Gaussian(x0, P)
ps = [pp] # vector of filtered Gaussians
obs = Point2f0[]
for i in 1:n
global pp, p, v
# predict
pp = F*pp ⊕ Gaussian(zero(x0), Q) #same as Gaussian(Φ*p.μ, Φ*p.Σ*Φ' + Q)
# observe
p, v = onestep(p, v)
xobs = p + @SVector randn(2)
push!(obs, xobs)
# correct
pp, yres, _ = Kalman.correct(Kalman.JosephForm(), pp, (Gaussian(xobs, R), H))
push!(ps, pp) # save filtered density
end
path = [Point2f0(mean(p)[1:2]) for p in ps]
vel = [Point2f0(mean(p)[3:4]) for p in ps]
fig = Figure()
ax = Axis(fig[1, 1], aspect = DataAspect())
scatter!(obs)
lines!(obs)
sl = Slider(fig[2, 1], range = 1:n, startvalue = 1)
p = @lift([Point2f0(path[$(sl.value)])])
d = @lift([Point2f0(vel[$(sl.value)])])
arrows!(p, d, arrowcolor = :red, linecolor = :red)#, linewidth = 3, )
w = 0.01only(diff([extrema(vcat(obs...))...]))
on(p) do pp
pp = p[]
ps = [pp; pp .+ d[]]
xlim = extrema(first, ps) .+ (-w, w)
ylim = extrema(last, ps) .+ (-w, w)
limits!(ax, xlim..., ylim...)
end
fig