@@ -368,9 +368,12 @@ struct UnscentedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
368368 B̂d:: Matrix{NT}
369369 D̂d:: Matrix{NT}
370370 P̂0:: Hermitian{NT, Matrix{NT}}
371- Q̂:: Hermitian{NT, Matrix{NT}}
372- R̂:: Hermitian{NT, Matrix{NT}}
371+ Q̂ :: Hermitian{NT, Matrix{NT}}
372+ R̂ :: Hermitian{NT, Matrix{NT}}
373373 K̂:: Matrix{NT}
374+ M̂:: Hermitian{NT, Matrix{NT}}
375+ X̂ :: Matrix{NT}
376+ Ŷm:: Matrix{NT}
374377 nσ:: Int
375378 γ:: NT
376379 m̂:: Vector{NT}
@@ -391,14 +394,16 @@ struct UnscentedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
391394 P̂0 = Hermitian (P̂0, :L )
392395 P̂ = copy (P̂0)
393396 K̂ = zeros (NT, nx̂, nym)
397+ M̂ = Hermitian (zeros (NT, nym, nym), :L )
398+ X̂, Ŷm = zeros (NT, nx̂, nσ), zeros (NT, nym, nσ)
394399 return new {NT, SM} (
395400 model,
396401 lastu0, x̂, P̂,
397402 i_ym, nx̂, nym, nyu, nxs,
398403 As, Cs_u, Cs_y, nint_u, nint_ym,
399404 Â, B̂u, Ĉ, B̂d, D̂d,
400405 P̂0, Q̂, R̂,
401- K̂,
406+ K̂, M̂, X̂, Ŷm,
402407 nσ, γ, m̂, Ŝ
403408 )
404409 end
@@ -565,18 +570,18 @@ noise, respectively.
565570 ISBN9780470045343.
566571"""
567572function update_estimate! (estim:: UnscentedKalmanFilter{NT} , u, ym, d) where NT<: Real
568- x̂, P̂, Q̂, R̂, K̂ = estim. x̂, estim. P̂, estim. Q̂, estim. R̂, estim. K̂
573+ x̂, P̂, Q̂, R̂, K̂, M̂ = estim. x̂, estim. P̂, estim. Q̂, estim. R̂, estim. K̂, estim. M̂
574+ X̂, Ŷm = estim. X̂, estim. Ŷm
569575 nym, nx̂, nσ = estim. nym, estim. nx̂, estim. nσ
570576 γ, m̂, Ŝ = estim. γ, estim. m̂, estim. Ŝ
571577 # --- initialize matrices ---
572- X̂, X̂_next = Matrix {NT} (undef, nx̂, nσ), Matrix {NT} (undef, nx̂, nσ)
578+ X̂_next = Matrix {NT} (undef, nx̂, nσ)
573579 û = Vector {NT} (undef, estim. model. nu)
574580 ŷm = Vector {NT} (undef, nym)
575581 ŷ = Vector {NT} (undef, estim. model. ny)
576- Ŷm = Matrix {NT} (undef, nym, nσ)
577582 sqrt_P̂ = LowerTriangular {NT, Matrix{NT}} (Matrix {NT} (undef, nx̂, nx̂))
578583 # --- correction step ---
579- sqrt_P̂. data .= cholesky (P̂). L
584+ sqrt_P̂ .= cholesky (P̂). L
580585 γ_sqrt_P̂ = lmul! (γ, sqrt_P̂)
581586 X̂ .= x̂
582587 X̂[:, 2 : nx̂+ 1 ] .+ = γ_sqrt_P̂
@@ -589,7 +594,7 @@ function update_estimate!(estim::UnscentedKalmanFilter{NT}, u, ym, d) where NT<:
589594 X̄, Ȳm = X̂, Ŷm
590595 X̄ .= X̂ .- x̂
591596 Ȳm .= Ŷm .- ŷm
592- M̂ = Hermitian (Ȳm * Ŝ * Ȳm' + R̂, :L )
597+ M̂ . = Hermitian (Ȳm * Ŝ * Ȳm' . + R̂)
593598 mul! (K̂, X̄, lmul! (Ŝ, Ȳm' ))
594599 rdiv! (K̂, cholesky (M̂))
595600 v̂ = ŷm
@@ -598,19 +603,18 @@ function update_estimate!(estim::UnscentedKalmanFilter{NT}, u, ym, d) where NT<:
598603 P̂_cor = P̂ - Hermitian (K̂ * M̂ * K̂' , :L )
599604 # --- prediction step ---
600605 X̂_cor, sqrt_P̂_cor = X̂, sqrt_P̂
601- sqrt_P̂_cor. data .= cholesky (P̂_cor). L
606+ sqrt_P̂_cor .= cholesky (P̂_cor). L
602607 γ_sqrt_P̂_cor = lmul! (γ, sqrt_P̂_cor)
603608 X̂_cor .= x̂_cor
604609 X̂_cor[:, 2 : nx̂+ 1 ] .+ = γ_sqrt_P̂_cor
605610 X̂_cor[:, nx̂+ 2 : end ] .- = γ_sqrt_P̂_cor
606- X̂_next = similar (X̂_cor)
607611 for j in axes (X̂_next, 2 )
608612 @views f̂! (X̂_next[:, j], û, estim, estim. model, X̂_cor[:, j], u, d)
609613 end
610614 x̂_next = mul! (x̂, X̂_next, m̂)
611615 X̄_next = X̂_next
612616 X̄_next .= X̂_next .- x̂_next
613- P̂. data .= X̄_next * Ŝ * X̄_next' .+ Q̂ # .data is necessary for Hermitians
617+ P̂ .= Hermitian ( X̄_next * Ŝ * X̄_next' .+ Q̂)
614618 return nothing
615619end
616620
781785function init_estimate_cov! (
782786 estim:: Union{KalmanFilter, UnscentedKalmanFilter, ExtendedKalmanFilter} , _ , _ , _
783787)
784- estim. P̂. data .= estim. P̂0 # .data is necessary for Hermitians
788+ estim. P̂ .= estim. P̂0
785789 return nothing
786790end
787791
@@ -818,6 +822,7 @@ function update_estimate_kf!(estim::StateEstimator{NT}, u, ym, d, Â, Ĉm, P̂
818822 Q̂, R̂, M̂, K̂ = estim. Q̂, estim. R̂, estim. M̂, estim. K̂
819823 nx̂, nu, ny = estim. nx̂, estim. model. nu, estim. model. ny
820824 x̂next, û, ŷ = Vector {NT} (undef, nx̂), Vector {NT} (undef, nu), Vector {NT} (undef, ny)
825+ P̂next = similar (P̂)
821826 mul! (M̂, P̂. data, Ĉm' ) # the ".data" weirdly removes a type instability in mul!
822827 rdiv! (M̂, cholesky! (Hermitian (Ĉm * P̂ * Ĉm' .+ R̂)))
823828 mul! (K̂, Â, M̂)
@@ -828,6 +833,6 @@ function update_estimate_kf!(estim::StateEstimator{NT}, u, ym, d, Â, Ĉm, P̂
828833 f̂! (x̂next, û, estim, estim. model, x̂, u, d)
829834 mul! (x̂next, K̂, v̂, 1 , 1 )
830835 estim. x̂ .= x̂next
831- P̂. data .= Â * (P̂ .- M̂ * Ĉm * P̂) * Â' .+ Q̂ # .data is necessary for Hermitians
836+ P̂ .= Hermitian ( Â * (P̂ .- M̂ * Ĉm * P̂) * Â' .+ Q̂)
832837 return nothing
833838end
0 commit comments