Skip to content

Commit 316cd67

Browse files
committed
added: reduce allocation ExtendedKalmanFilter
1 parent 593c613 commit 316cd67

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

src/estimator/kalman.jl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,9 @@ struct ExtendedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
841841
::Hermitian{NT, Matrix{NT}}
842842
::Matrix{NT}
843843
F̂_û::Matrix{NT}
844+
::Matrix{NT}
844845
::Matrix{NT}
846+
Ĥm ::Matrix{NT}
845847
direct::Bool
846848
corrected::Vector{Bool}
847849
buffer::StateEstimatorBuffer{NT}
@@ -863,7 +865,8 @@ struct ExtendedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
863865
= Hermitian(R̂, :L)
864866
= copy(P̂_0)
865867
= zeros(NT, nx̂, nym)
866-
F̂_û, Ĥ = zeros(NT, nx̂+nu, nx̂), zeros(NT, ny, nx̂)
868+
F̂_û, F̂ = zeros(NT, nx̂, nx̂+nu), zeros(NT, nx̂, nx̂)
869+
Ĥ, Ĥm = zeros(NT, ny, nx̂), zeros(NT, nym, nx̂)
867870
corrected = [false]
868871
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd)
869872
return new{NT, SM}(
@@ -874,7 +877,7 @@ struct ExtendedKalmanFilter{NT<:Real, SM<:SimModel} <: StateEstimator{NT}
874877
Â, B̂u, Ĉ, B̂d, D̂d, Ĉm, D̂dm,
875878
P̂_0, Q̂, R̂,
876879
K̂,
877-
F̂_û, ,
880+
F̂_û, F̂, Ĥ, Ĥm,
878881
direct, corrected,
879882
buffer
880883
)
@@ -965,8 +968,8 @@ function correct_estimate!(estim::ExtendedKalmanFilter, y0m, d0)
965968
ŷ0 = estim.buffer.
966969
ĥAD! = (ŷ0, x̂0) -> ĥ!(ŷ0, estim, model, x̂0, d0)
967970
ForwardDiff.jacobian!(estim.Ĥ, ĥAD!, ŷ0, x̂0)
968-
Ĥm = @views estim.Ĥ[estim.i_ym, :]
969-
return correct_estimate_kf!(estim, y0m, d0, Ĥm)
971+
estim.Ĥm .= @views estim.Ĥ[estim.i_ym, :]
972+
return correct_estimate_kf!(estim, y0m, d0, estim.Ĥm)
970973
end
971974

972975

@@ -1011,13 +1014,12 @@ The correction step is skipped if `estim.direct == true` since it's already done
10111014
function update_estimate!(estim::ExtendedKalmanFilter{NT}, y0m, d0, u0) where NT<:Real
10121015
model, x̂0 = estim.model, estim.x̂0
10131016
nx̂, nu = estim.nx̂, model.nu
1014-
= estim.
10151017
if !estim.direct
10161018
ŷ0 = estim.buffer.
10171019
ĥAD! = (ŷ0, x̂0) -> ĥ!(ŷ0, estim, model, x̂0, d0)
1018-
ForwardDiff.jacobian!(Ĥ, ĥAD!, ŷ0, x̂0)
1019-
Ĥm = @views Ĥ[estim.i_ym, :]
1020-
correct_estimate_kf!(estim, y0m, d0, Ĥm)
1020+
ForwardDiff.jacobian!(estim.Ĥ, ĥAD!, ŷ0, x̂0)
1021+
estim.Ĥm .= @views estim.Ĥ[estim.i_ym, :]
1022+
correct_estimate_kf!(estim, y0m, d0, estim.Ĥm)
10211023
end
10221024
x̂0corr = estim.x̂0
10231025
# concatenate x̂0next and û0 vectors to allows û0 vector with dual numbers for AD:
@@ -1027,8 +1029,8 @@ function update_estimate!(estim::ExtendedKalmanFilter{NT}, y0m, d0, u0) where NT
10271029
x̂0nextû[1:nx̂], x̂0nextû[nx̂+1:end], estim, model, x̂0corr, u0, d0
10281030
)
10291031
ForwardDiff.jacobian!(estim.F̂_û, f̂AD!, x̂0nextû, x̂0corr)
1030-
= @views estim.F̂_û[1:estim.nx̂, :]
1031-
return predict_estimate_kf!(estim, u0, d0, F̂)
1032+
estim..= @views estim.F̂_û[1:estim.nx̂, :]
1033+
return predict_estimate_kf!(estim, u0, d0, estim.F̂)
10321034
end
10331035

10341036
"Set `estim.P̂` to `estim.P̂_0` for the time-varying Kalman Filters."

0 commit comments

Comments
 (0)