@@ -163,7 +163,7 @@ This syntax allows nonzero off-diagonal elements in ``\mathbf{Q̂, R̂}``.
163163"""
164164function SteadyKalmanFilter (model:: SM , i_ym, nint_u, nint_ym, Q̂, R̂) where {NT<: Real , SM<: LinModel{NT} }
165165 Q̂, R̂ = to_mat (Q̂), to_mat (R̂)
166- return SteadyKalmanFilter {NT, SM} (model, i_ym, nint_u, nint_ym, Q̂ , R̂)
166+ return SteadyKalmanFilter {NT, SM} (model, i_ym, nint_u, nint_ym, Q̂, R̂)
167167end
168168
169169
@@ -315,7 +315,7 @@ This syntax allows nonzero off-diagonal elements in ``\mathbf{P̂}_{-1}(0), \mat
315315"""
316316function KalmanFilter (model:: SM , i_ym, nint_u, nint_ym, P̂0, Q̂, R̂) where {NT<: Real , SM<: LinModel{NT} }
317317 P̂0, Q̂, R̂ = to_mat (P̂0), to_mat (Q̂), to_mat (R̂)
318- return KalmanFilter {NT, SM} (model, i_ym, nint_u, nint_ym, P̂0, Q̂ , R̂)
318+ return KalmanFilter {NT, SM} (model, i_ym, nint_u, nint_ym, P̂0, Q̂, R̂)
319319end
320320
321321@doc raw """
@@ -428,7 +428,7 @@ represents the measured outputs of ``\mathbf{ĥ}`` function (and unmeasured one
428428
429429# Arguments
430430- `model::SimModel` : (deterministic) model for the estimations.
431- - `α=1e-3` : alpha parameter, spread of the state distribution ``(0 ≤ α ≤ 1)``.
431+ - `α=1e-3` : alpha parameter, spread of the state distribution ``(0 < α ≤ 1)``.
432432- `β=2` : beta parameter, skewness and kurtosis of the states distribution ``(β ≥ 0)``.
433433- `κ=0` : kappa parameter, another spread parameter ``(0 ≤ κ ≤ 3)``.
434434- `<keyword arguments>` of [`SteadyKalmanFilter`](@ref) constructor.
@@ -478,14 +478,14 @@ function UnscentedKalmanFilter(
478478end
479479
480480@doc raw """
481- UnscentedKalmanFilter(model, i_ym, nint_u, nint_ym, P̂0, Q̂, R̂, α, β, κ)
481+ UnscentedKalmanFilter(model, i_ym, nint_u, nint_ym, P̂0, Q̂, R̂, α=1e-3 , β=2 , κ=0 )
482482
483483Construct the estimator from the augmented covariance matrices `P̂0`, `Q̂` and `R̂`.
484484
485485This syntax allows nonzero off-diagonal elements in ``\m athbf{P̂}_{-1}(0), \m athbf{Q̂, R̂}``.
486486"""
487487function UnscentedKalmanFilter (
488- model:: SM , i_ym, nint_u, nint_ym, P̂0, Q̂, R̂, α, β, κ
488+ model:: SM , i_ym, nint_u, nint_ym, P̂0, Q̂, R̂, α= 1e-3 , β= 2 , κ= 0
489489) where {NT<: Real , SM<: SimModel{NT} }
490490 P̂0, Q̂, R̂ = to_mat (P̂0), to_mat (Q̂), to_mat (R̂)
491491 return UnscentedKalmanFilter {NT, SM} (model, i_ym, nint_u, nint_ym, P̂0, Q̂ , R̂, α, β, κ)
@@ -565,22 +565,7 @@ noise, respectively.
565565 ISBN9780470045343.
566566"""
567567function update_estimate! (estim:: UnscentedKalmanFilter{NT} , u, ym, d) where NT<: Real
568- return update_estimate_ukf! (estim, u, ym, d, estim. P̂, estim. x̂)
569- end
570-
571- """
572- update_estimate_ukf!(estim::StateEstimator, u, ym, d, P̂, x̂=nothing)
573-
574- Update Unscented Kalman Filter estimates and covariance matrices.
575-
576- Allows code reuse for [`UnscentedKalmanFilter`](@ref) and [`MovingHorizonEstimator`](@ref).
577- See [`update_estimate!(::UnscentedKalmanFilter, ::Any, ::Any, ::Any)`(@ref) docstring
578- for the equations. If `isnothing(x̂)`, only the covariance `P̂` is updated.
579- """
580- function update_estimate_ukf! (
581- estim:: StateEstimator{NT} , u, ym, d, P̂, x̂= nothing
582- ) where NT<: Real
583- Q̂, R̂, K̂ = estim. Q̂, estim. R̂, estim. K̂
568+ x̂, P̂, Q̂, R̂, K̂ = estim. x̂, estim. P̂, estim. Q̂, estim. R̂, estim. K̂
584569 nym, nx̂, nσ = estim. nym, estim. nx̂, estim. nσ
585570 γ, m̂, Ŝ = estim. γ, estim. m̂, estim. Ŝ
586571 # --- initialize matrices ---
@@ -742,7 +727,7 @@ This syntax allows nonzero off-diagonal elements in ``\mathbf{P̂}_{-1}(0), \mat
742727"""
743728function ExtendedKalmanFilter (model:: SM , i_ym, nint_u, nint_ym,P̂0, Q̂, R̂) where {NT<: Real , SM<: SimModel{NT} }
744729 P̂0, Q̂, R̂ = to_mat (P̂0), to_mat (Q̂), to_mat (R̂)
745- return ExtendedKalmanFilter {NT, SM} (model, i_ym, nint_u, nint_ym, P̂0, Q̂ , R̂)
730+ return ExtendedKalmanFilter {NT, SM} (model, i_ym, nint_u, nint_ym, P̂0, Q̂, R̂)
746731end
747732
748733
@@ -759,7 +744,8 @@ substitutions ``\mathbf{Â = F̂}(k)`` and ``\mathbf{Ĉ^m = Ĥ^m}(k)``:
759744 [\m athbf{Ĥ^m}(k)\m athbf{P̂}_{k-1}(k)\m athbf{Ĥ^m}'(k) + \m athbf{R̂}]^{-1} \\
760745 \m athbf{K̂}(k) &= \m athbf{F̂}(k) \m athbf{M̂}(k) \\
761746 \m athbf{ŷ^m}(k) &= \m athbf{ĥ^m}\B ig( \m athbf{x̂}_{k-1}(k), \m athbf{d}(k) \B ig) \\
762- \m athbf{x̂}_{k}(k+1) &= \m athbf{f̂}\B ig( \m athbf{x̂}_{k-1}(k), \m athbf{u}(k), \m athbf{d}(k) \B ig)
747+ \m athbf{x̂}_{k}(k+1) &= \m ath, covestim::CE,
748+ ) bf{f̂}\B ig( \m athbf{x̂}_{k-1}(k), \m athbf{u}(k), \m athbf{d}(k) \B ig)
763749 + \m athbf{K̂}(k)[\m athbf{y^m}(k) - \m athbf{ŷ^m}(k)] \\
764750 \m athbf{P̂}_{k}(k+1) &= \m athbf{F̂}(k)[\m athbf{P̂}_{k-1}(k)
765751 - \m athbf{M̂}(k)\m athbf{Ĥ^m}(k)\m athbf{P̂}_{k-1}(k)]\m athbf{F̂}'(k)
@@ -770,7 +756,8 @@ substitutions ``\mathbf{Â = F̂}(k)`` and ``\mathbf{Ĉ^m = Ĥ^m}(k)``:
770756automatically computes the Jacobians:
771757```math
772758\b egin{aligned}
773- \m athbf{F̂}(k) &= \l eft. \f rac{∂\m athbf{f̂}(\m athbf{x̂}, \m athbf{u}, \m athbf{d})}{∂\m athbf{x̂}} \r ight|_{\m athbf{x̂ = x̂}_{k-1}(k),\, \m athbf{u = u}(k),\, \m athbf{d = d}(k)} \\
759+ \m athbf{F̂}(k) &= \l eft. \f ra, covestim::CE,
760+ ) c{∂\m athbf{f̂}(\m athbf{x̂}, \m athbf{u}, \m athbf{d})}{∂\m athbf{x̂}} \r ight|_{\m athbf{x̂ = x̂}_{k-1}(k),\, \m athbf{u = u}(k),\, \m athbf{d = d}(k)} \\
774761 \m athbf{Ĥ}(k) &= \l eft. \f rac{∂\m athbf{ĥ}(\m athbf{x̂}, \m athbf{d})}{∂\m athbf{x̂}} \r ight|_{\m athbf{x̂ = x̂}_{k-1}(k),\, \m athbf{d = d}(k)}
775762\e nd{aligned}
776763```
@@ -813,34 +800,29 @@ function validate_kfcov(nym, nx̂, Q̂, R̂, P̂0=nothing)
813800end
814801
815802"""
816- update_estimate_kf!(estim::StateEstimator, u, ym, d, Â, Ĉm, P̂, x̂=nothing )
803+ update_estimate_kf!(estim::StateEstimator, u, ym, d, Â, Ĉm, P̂, x̂)
817804
818805Update time-varying/extended Kalman Filter estimates with augmented `Â` and `Ĉm` matrices.
819806
820807Allows code reuse for [`KalmanFilter`](@ref), [`ExtendedKalmanFilterKalmanFilter`](@ref).
821808They update the state `x̂` and covariance `P̂` with the same equations. The extended filter
822809substitutes the augmented model matrices with its Jacobians (`Â = F̂` and `Ĉm = Ĥm`).
823810The implementation uses in-place operations and explicit factorization to reduce
824- allocations. See e.g. [`KalmanFilter`](@ref) docstring for the equations. If `isnothing(x̂)`,
825- only the covariance `P̂` is updated.
811+ allocations. See e.g. [`KalmanFilter`](@ref) docstring for the equations.
826812"""
827- function update_estimate_kf! (
828- estim:: StateEstimator{NT} , u, ym, d, Â, Ĉm, P̂, x̂= nothing
829- ) where NT<: Real
830- Q̂, R̂, M̂ = estim. Q̂, estim. R̂, estim. M̂
813+ function update_estimate_kf! (estim:: StateEstimator{NT} , u, ym, d, Â, Ĉm, P̂, x̂) where NT<: Real
814+ Q̂, R̂, M̂, K̂ = estim. Q̂, estim. R̂, estim. M̂, estim. K̂
831815 mul! (M̂, P̂, Ĉm' )
832816 rdiv! (M̂, cholesky! (Hermitian (Ĉm * P̂ * Ĉm' .+ R̂)))
833- if ! isnothing (x̂)
834- mul! (estim. K̂, Â, M̂)
835- x̂next, ŷ = Vector {NT} (undef, estim. nx̂), Vector {NT} (undef, estim. model. ny)
836- ĥ! (ŷ, estim, estim. model, x̂, d)
837- ŷm = @views ŷ[estim. i_ym]
838- v̂ = ŷm
839- v̂ .= ym .- ŷm
840- f̂! (x̂next, estim, estim. model, x̂, u, d)
841- mul! (x̂next, estim. K̂, v̂, 1 , 1 )
842- estim. x̂ .= x̂next
843- end
817+ mul! (K̂, Â, M̂)
818+ x̂next, ŷ = Vector {NT} (undef, estim. nx̂), Vector {NT} (undef, estim. model. ny)
819+ ĥ! (ŷ, estim, estim. model, x̂, d)
820+ ŷm = @views ŷ[estim. i_ym]
821+ v̂ = ŷm
822+ v̂ .= ym .- ŷm
823+ f̂! (x̂next, estim, estim. model, x̂, u, d)
824+ mul! (x̂next, K̂, v̂, 1 , 1 )
825+ estim. x̂ .= x̂next
844826 P̂. data .= Â * (P̂ .- M̂ * Ĉm * P̂) * Â' .+ Q̂ # .data is necessary for Hermitians
845827 return nothing
846828end
0 commit comments