@@ -567,8 +567,9 @@ function update_estimate!(estim::UnscentedKalmanFilter{NT}, u, ym, d) where NT<:
567567 nym, nx̂, nσ = estim. nym, estim. nx̂, estim. nσ
568568 γ, m̂, Ŝ = estim. γ, estim. m̂, estim. Ŝ
569569 # --- initialize matrices ---
570- X̂ = Matrix {NT} (undef, nx̂, nσ)
570+ X̂, X̂_next = Matrix {NT} (undef, nx̂, nσ), Matrix {NT} (undef, nx̂, nσ)
571571 ŷm = Vector {NT} (undef, nym)
572+ ŷ = Vector {NT} (undef, estim. model. ny)
572573 Ŷm = Matrix {NT} (undef, nym, nσ)
573574 sqrt_P̂ = LowerTriangular {NT, Matrix{NT}} (Matrix {NT} (undef, nx̂, nx̂))
574575 # --- correction step ---
@@ -578,7 +579,8 @@ function update_estimate!(estim::UnscentedKalmanFilter{NT}, u, ym, d) where NT<:
578579 X̂[:, 2 : nx̂+ 1 ] .+ = γ_sqrt_P̂
579580 X̂[:, nx̂+ 2 : end ] .- = γ_sqrt_P̂
580581 for j in axes (Ŷm, 2 )
581- Ŷm[:, j] = @views ĥ (estim, estim. model, X̂[:, j], d)[estim. i_ym]
582+ @views ĥ! (ŷ, estim, estim. model, X̂[:, j], d)
583+ @views Ŷm[:, j] .= ŷ[estim. i_ym]
582584 end
583585 mul! (ŷm, Ŷm, m̂)
584586 X̄, Ȳm = X̂, Ŷm
@@ -598,9 +600,9 @@ function update_estimate!(estim::UnscentedKalmanFilter{NT}, u, ym, d) where NT<:
598600 X̂_cor .= x̂_cor
599601 X̂_cor[:, 2 : nx̂+ 1 ] .+ = γ_sqrt_P̂_cor
600602 X̂_cor[:, nx̂+ 2 : end ] .- = γ_sqrt_P̂_cor
601- X̂_next = X̂_cor
603+ X̂_next = similar ( X̂_cor)
602604 for j in axes (X̂_next, 2 )
603- X̂_next[:, j] = @views f̂ ( estim, estim. model, X̂_cor[:, j], u, d)
605+ @views f̂! ( X̂_next[:, j], estim, estim. model, X̂_cor[:, j], u, d)
604606 end
605607 x̂_next = mul! (x̂, X̂_next, m̂)
606608 X̄_next = X̂_next
@@ -757,9 +759,13 @@ automatically computes the Jacobians:
757759```
758760The matrix ``\m athbf{Ĥ^m}`` is the rows of ``\m athbf{Ĥ}`` that are measured outputs.
759761"""
760- function update_estimate! (estim:: ExtendedKalmanFilter , u, ym, d= empty (estim. x̂))
761- F̂ = ForwardDiff. jacobian (x̂ -> f̂ (estim, estim. model, x̂, u, d), estim. x̂)
762- Ĥ = ForwardDiff. jacobian (x̂ -> ĥ (estim, estim. model, x̂, d), estim. x̂)
762+ function update_estimate! (
763+ estim:: ExtendedKalmanFilter{NT} , u, ym, d= empty (estim. x̂)
764+ ) where NT<: Real
765+ model = estim. model
766+ x̂next, ŷ = Vector {NT} (undef, estim. nx̂), Vector {NT} (undef, model. ny)
767+ F̂ = ForwardDiff. jacobian ((x̂next, x̂) -> f̂! (x̂next, estim, model, x̂, u, d), x̂next, estim. x̂)
768+ Ĥ = ForwardDiff. jacobian ((ŷ, x̂) -> ĥ! (ŷ, estim, model, x̂, d), ŷ, estim. x̂)
763769 return update_estimate_kf! (estim, u, ym, d, F̂, Ĥ[estim. i_ym, :], estim. P̂, estim. x̂)
764770end
765771
@@ -790,7 +796,7 @@ function validate_kfcov(nym, nx̂, Q̂, R̂, P̂0=nothing)
790796end
791797
792798"""
793- update_estimate_kf!(estim, u, ym, d, Â, Ĉm, P̂, x̂=nothing)
799+ update_estimate_kf!(estim::StateEstimator , u, ym, d, Â, Ĉm, P̂, x̂=nothing)
794800
795801Update time-varying/extended Kalman Filter estimates with augmented `Â` and `Ĉm` matrices.
796802
@@ -801,16 +807,22 @@ The implementation uses in-place operations and explicit factorization to reduce
801807allocations. See e.g. [`KalmanFilter`](@ref) docstring for the equations. If `isnothing(x̂)`,
802808only the covariance `P̂` is updated.
803809"""
804- function update_estimate_kf! (estim, u, ym, d, Â, Ĉm, P̂, x̂= nothing )
810+ function update_estimate_kf! (
811+ estim:: StateEstimator{NT} , u, ym, d, Â, Ĉm, P̂, x̂= nothing
812+ ) where NT<: Real
805813 Q̂, R̂, M̂ = estim. Q̂, estim. R̂, estim. M̂
806814 mul! (M̂, P̂, Ĉm' )
807815 rdiv! (M̂, cholesky! (Hermitian (Ĉm * P̂ * Ĉm' .+ R̂)))
808816 if ! isnothing (x̂)
809817 mul! (estim. K̂, Â, M̂)
810- ŷm = @views ĥ (estim, estim. model, x̂, d)[estim. i_ym]
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]
811821 v̂ = ŷm
812822 v̂ .= ym .- ŷm
813- x̂ .= f̂ (estim, estim. model, x̂, u, d) .+ mul! (x̂, estim. K̂, v̂)
823+ f̂! (x̂next, estim, estim. model, x̂, u, d)
824+ mul! (x̂next, estim. K̂, v̂, 1 , 1 )
825+ estim. x̂ .= x̂next
814826 end
815827 P̂. data .= Â * (P̂ .- M̂ * Ĉm * P̂) * Â' .+ Q̂ # .data is necessary for Hermitians
816828 return nothing
0 commit comments