-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathST_Kalman_S.jl
72 lines (51 loc) · 1.8 KB
/
ST_Kalman_S.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
71
72
module KalmanFilter
using LinearAlgebra
export Kalman, update, predict, K, next
const Matrix = AbstractArray{T, N} where {N, T <: Real}
const Vector = AbstractArray{T, N} where {N, T <: Real}
"""
Defines the Kalman Filter object.
"""
mutable struct Kalman
A # State transition matrix
Q # Process covariance matrix
H # Measurement mapping
R # Measurement covariance matrix
m # Current estimate mean
P # State covariance matrix. Current estimate uncertianty
function Kalman(A::Matrix, Q::Matrix, H::Matrix, R::Matrix, m::Vector, P::Matrix)
new(A, Q, H, R, m, P)
end
# Creates a Kalman-Filter object for the scalar case.
Kalman(A::Real, Q::Real, H::Real, R::Real, m::Real, P::Real) = new(A, Q, H, R, m, P)
end
# K(Kalman): Computes the Kalman Gain based on a model.
function K(k::Kalman)
K(k.P, k.H, k.R)
end
# K(P, H, R): Computes the Kalman Gain based the matrices.
function K(P, H, R)
P * transpose(H) * (H * P * transpose(H) + R)^-1
end
# predict(k::Kalman): Predict next state based on the dynamic process model.
function predict(k::Kalman)
k.m = k.A * k.m
k.P = k.A * k.P * transpose(k.A) + k.Q
(state=k.m, cov=k.P)
end
# update(k::Kalman, y): Compute the filtered distribution.
function update(k::Kalman, y)
kalman_gain = K(k.P, k.H, k.R)
k.m = k.m + kalman_gain * (y - k.H * k.m)
k.P = (I - kalman_gain * k.H) * k.P * transpose(I - kalman_gain * k.H) +
kalman_gain * k.R * transpose(kalman_gain)
(state=k.m, cov=k.P, gain=kalman_gain)
end
# next(k::Kalman, y): Compute a complete Kalman step.
function next(k::Kalman, y)
newInstance = deepcopy(k)
p = predict(newInstance)
up = update(newInstance, y)
(filter = newInstance, updated = up.state, predicted = p.state, cov = up.cov, gain = up.gain)
end
end