@@ -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"""
300300function 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. R̂
303- M = (P̂ * Ĉm' ) / (Ĉm * P̂ * Ĉm' + R̂)
304- K = Â * M
305- x̂[:] = Â * x̂ + B̂u * u + B̂d * d + K * (ym - Ĉm * x̂ - D̂dm * d)
306- P̂. data[:] = Â * (P̂ - M * Ĉm * P̂) * Â' + Q̂ # .data is necessary for Hermitian matrices
307- return x̂, P̂
301+ return update_state_kf! (estim, estim. Â, estim. Ĉm, u, ym, d)
308302end
309303
310304struct UnscentedKalmanFilter{M<: SimModel } <: StateEstimator
@@ -668,16 +662,10 @@ automatically computes the Jacobians:
668662The matrix ``\m athbf{Ĥ^m}`` is the rows of ``\m athbf{Ĥ}`` that are measured outputs.
669663"""
670664function update_estimate! (estim:: ExtendedKalmanFilter , u, ym, d= Float64[])
671- x̂, P̂, Q̂, R̂ = estim. x̂, estim. P̂, estim. Q̂, estim. R̂
672- F̂ = ForwardDiff. jacobian (x̂ -> f̂ (estim, x̂, u, d), x̂)
673- Ĥ = ForwardDiff. jacobian (x̂ -> ĥ (estim, x̂, d), x̂)
665+ F̂ = ForwardDiff. jacobian (x̂ -> f̂ (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 * P̂ * Ĥm' + R̂)
676- K = F̂ * M
677- ŷm = ĥ (estim, x̂, d)[estim. i_ym]
678- x̂[:] = f̂ (estim, x̂, u, d) + K * (ym - ŷm)
679- P̂. data[:] = F̂ * (P̂ - M * Ĥm * P̂) * F̂' + Q̂ # .data is necessary for Hermitian matrices
680- return x̂, P̂
668+ return update_state_kf! (estim, F̂, Ĥm, u, ym, d)
681669end
682670
683671" Initialize the covariance estimate `P̂` for the time-varying Kalman Filters"
691679"""
692680 validate_kfcov(nym, nx̂, Q̂, R̂, P̂0=nothing)
693681
694- Validate sizes of process Q̂ and sensor R̂ noises covariance matrices.
682+ Validate sizes of process `Q̂`` and sensor `R̂` noises covariance matrices.
695683
696684Also validate initial estimate covariance size, if provided.
697685"""
@@ -703,5 +691,21 @@ function validate_kfcov(nym, nx̂, Q̂, R̂, P̂0=nothing)
703691 end
704692end
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. R̂
705+ M = (P̂ * Ĉm' ) / (Ĉm * P̂ * Ĉm' + R̂)
706+ K = Â * M
707+ ŷm = ĥ (estim, x̂, d)[estim. i_ym]
708+ x̂[:] = f̂ (estim, x̂, u, d) + K * (ym - ŷm)
709+ P̂. data[:] = Â * (P̂ - M * Ĉm * P̂) * Â' + Q̂ # .data is necessary for Hermitian matrices
710+ return x̂, P̂
711+ end
0 commit comments