Skip to content

Commit cac9419

Browse files
committed
code reuse for KalmanFilter and ExtendedKalmanFilter
1 parent d213756 commit cac9419

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/estimator/kalman.jl

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -298,13 +298,7 @@ control period ``k-1``. See [^2] for details.
298298
Linear Dynamical Systems*, https://web.stanford.edu/class/ee363/lectures/kf.pdf.
299299
"""
300300
function update_estimate!(estim::KalmanFilter, u, ym, d)
301-
Â, B̂u, B̂d, Ĉm, D̂dm = estim.Â, estim.B̂u, estim.B̂d, estim.Ĉm, estim.D̂dm
302-
x̂, P̂, Q̂, R̂ = estim.x̂, estim.P̂, estim.Q̂, estim.
303-
M = (P̂ * Ĉm') / (Ĉm ** Ĉm' + R̂)
304-
K =* M
305-
x̂[:] =*+ B̂u * u + B̂d * d + K * (ym - Ĉm *- D̂dm * d)
306-
.data[:] =* (P̂ - M * Ĉm * P̂) *' +# .data is necessary for Hermitian matrices
307-
return x̂, P̂
301+
return update_state_kf!(estim, estim.Â, estim.Ĉm, u, ym, d)
308302
end
309303

310304
struct UnscentedKalmanFilter{M<:SimModel} <: StateEstimator
@@ -668,16 +662,10 @@ automatically computes the Jacobians:
668662
The matrix ``\mathbf{Ĥ^m}`` is the rows of ``\mathbf{Ĥ}`` that are measured outputs.
669663
"""
670664
function update_estimate!(estim::ExtendedKalmanFilter, u, ym, d=Float64[])
671-
x̂, P̂, Q̂, R̂ = estim.x̂, estim.P̂, estim.Q̂, estim.
672-
= ForwardDiff.jacobian(x̂ -> (estim, x̂, u, d), x̂)
673-
= ForwardDiff.jacobian(x̂ -> (estim, x̂, d), x̂)
665+
= ForwardDiff.jacobian(x̂ -> (estim, x̂, u, d), estim.x̂)
666+
= ForwardDiff.jacobian(x̂ -> (estim, x̂, d), estim.x̂)
674667
Ĥm = Ĥ[estim.i_ym, :]
675-
M = (P̂ * Ĥm') / (Ĥm ** Ĥm' + R̂)
676-
K =* M
677-
ŷm = (estim, x̂, d)[estim.i_ym]
678-
x̂[:] = (estim, x̂, u, d) + K * (ym - ŷm)
679-
.data[:] =* (P̂ - M * Ĥm * P̂) *' +# .data is necessary for Hermitian matrices
680-
return x̂, P̂
668+
return update_state_kf!(estim, F̂, Ĥm, u, ym, d)
681669
end
682670

683671
"Initialize the covariance estimate `P̂` for the time-varying Kalman Filters"
@@ -691,7 +679,7 @@ end
691679
"""
692680
validate_kfcov(nym, nx̂, Q̂, R̂, P̂0=nothing)
693681
694-
Validate sizes of process and sensor noises covariance matrices.
682+
Validate sizes of process `Q̂`` and sensor `R̂` noises covariance matrices.
695683
696684
Also validate initial estimate covariance size, if provided.
697685
"""
@@ -703,5 +691,21 @@ function validate_kfcov(nym, nx̂, Q̂, R̂, P̂0=nothing)
703691
end
704692
end
705693

694+
"""
695+
update_state_kf!(estim, Â, Ĉm, u, ym, d)
706696
697+
Update time-varying/extended Kalman Filter estimates with augmented `Â` and `Ĉm` matrices.
707698
699+
Allows code reuse for the time-varying and extended Kalman filters. They update the state
700+
`x̂` and covariance `P̂` with the same equations. The extended filter substitutes the
701+
augmented model matrices with its Jacobians (`Â = F̂` and `Ĉm = Ĥm`).
702+
"""
703+
function update_state_kf!(estim, Â, Ĉm, u, ym, d)
704+
x̂, P̂, Q̂, R̂ = estim.x̂, estim.P̂, estim.Q̂, estim.
705+
M = (P̂ * Ĉm') / (Ĉm ** Ĉm' + R̂)
706+
K =* M
707+
ŷm = (estim, x̂, d)[estim.i_ym]
708+
x̂[:] = (estim, x̂, u, d) + K * (ym - ŷm)
709+
.data[:] =* (P̂ - M * Ĉm * P̂) *' +# .data is necessary for Hermitian matrices
710+
return x̂, P̂
711+
end

0 commit comments

Comments
 (0)