Extended Kalman Filter — Control Systems/State Estimation
Control_Systems/State_Estimation/Extended_Kalman_Filter · 2 input / 1 output port(s) at insert · exports to Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text
Description#
The block's own DESCRIPTION_HTML, rendered verbatim — the same text the config dialog's info panel and the library navigator show. Fix a wrong sentence in the block's .cpp (R-D9), never here.
Extended Kalman Filter
Control Systems / State Estimation
Estimates the states of a nonlinear discrete-time plant from a noisy measurement. The plant is
x[k+1] = f(x[k], u[k]) + w[k], y[k] = h(x[k], u[k]) + v[k], with cov(w) = Q and cov(v) = R,
and f and h are expressions you type over the state and input
variables you name – [x1 + 0.05*x2; x2 + 0.05*((1 - x1^2)*x2 - x1 +
u)], say. Each sample the filter linearizes them at the current estimate,
corrects the estimate with the measurement, and predicts the next one.
It is MATLAB's own extended Kalman filter
(extendedKalmanFilter, which Simulink's block runs) in its
square-root form: the covariance is carried as a factor S with
P = S·S', and both the correction and the prediction update S by a QR
factorization rather than forming P. That form keeps P symmetric and positive
semidefinite however long the filter runs.
The factory setting is a forced Van der Pol oscillator, stepped by forward Euler at 0.05 s, whose position x1 is measured: feed the oscillator's input on u and its noisy position on y, and xhat recovers the velocity nobody measured.
Ports
- u – the known plant input, a column [p,1] in the order of Input Variables. With no input variables it must be a scalar, and it is ignored.
- y – the measurement, a column [m,1] (m = the entries of Measurement h).
- xhat – the state estimate, a column [n,1] in the order of State Variables.
Parameters
- State Variables – the n state names, separated by spaces: 1 to 6 of them.
- Input Variables – the p input names, 0 to 6 of them; leave it empty for a plant with no input.
- State Transition f – the next state f(x, u): a column of n expressions over the state and input variables.
- Measurement h – the measurement h(x, u): a column of 1 to 6 expressions.
- Jacobians – Numerical (the default, as in MATLAB) linearizes f and h by forward differences with MATLAB's own step, max(√eps, √eps·|xj|); Exact differentiates the expressions symbolically, which is what supplying the Jacobian functions gives MATLAB.
- Process Noise Q – [n,n], symmetric positive semidefinite; a scalar is taken as that value times the identity.
- Measurement Noise R – [m,m], symmetric positive definite (the gain divides by it); a scalar expands the same way.
- Initial State – x[0], n entries.
- Initial State Covariance – P[0], [n,n] symmetric positive semidefinite, or a scalar.
- Estimator Type – Current publishes x̂[k|k], corrected with the measurement of the same sample (Simulink's default); Delayed publishes x̂[k|k−1], the prediction made before it.
- Sampling Time (s) – zero or less inherits the solver's rate; a positive value runs the block at that period.
Code export
All ten targets: Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog and PLC Structured Text. Each prints the same description of the filter that the simulation itself runs – f, h and their Jacobians written in the target's own syntax, the two QR factorizations and the triangular solves unrolled – so every target does the same arithmetic in the same order. The noise factors and the initial state are baked in at export time.
⚠ The three HDL targets run the filter in simulation-only real arithmetic, quantizing only at the port boundaries: a covariance factor and a triangular solve span many decades, which a Q16.16 datapath does not carry.
⚠ VHDL linearizes with the exact Jacobian even when Jacobians is
Numerical (whenever f and h can be differentiated). VHDL's
math_real functions – SIN, ARCTAN
and the rest – are accurate to about 1e-8 in GHDL, and a forward difference
over a step of 1.5e-8 turns that into an error of order one in the Jacobian. The
exact Jacobian differs from the difference quotient by about 1e-8 relative.
Simulink bridge
None (Support::None). Simulink's block (Control System
Toolbox and System Identification Toolbox, cstblocks/State
Estimation/Extended Kalman Filter) names its state transition and
measurement as MATLAB functions on the path, not as expressions, so there
is nothing a model could carry across; the bridge reports this block rather than
dropping it silently. Code export verification still covers it across all ten
languages.
Notes
- Discrete only, and stateful: the estimate and the covariance factor persist from sample to sample.
- ⚠ Numerical Jacobians agree with MATLAB to about 1e-8, not bit for
bit. A forward difference amplifies the last bit of f and h, and MATLAB
evaluates
x^2asx*xwhere this block uses a power. With Exact Jacobians the two agree to 1e-13. - Carries no state space: the filter is nonlinear, and f and h describe the PLANT, not this block.
Code facts#
| Fact | Value |
|---|---|
| registered type | Control_Systems/State_Estimation/Extended_Kalman_Filter |
| family | Control_Systems/State_Estimation |
| solver environment class | ICoreBlock_0_Control_Systems_1_State_Estimation_2_Extended_Kalman_Filter |
| source | src/ICoreBlocks/ICoreBlockLibrary/Blocks/Control_Systems/State_Estimation/Extended_Kalman_Filter/ICoreBlock_0_Control_Systems_1_State_Estimation_2_Extended_Kalman_Filter.cpp |
| header | src/ICoreBlocks/ICoreBlockLibrary/Blocks/Control_Systems/State_Estimation/Extended_Kalman_Filter/ICoreBlock_0_Control_Systems_1_State_Estimation_2_Extended_Kalman_Filter.h |
| default size on canvas | 150 × 90 px |
| ports at insert | 2 in, 1 out |
| code generators implemented | Python, MATLAB, Java, Rust, C, C++, VHDL, Verilog, SystemVerilog, PLC Structured Text |
Ports#
| # | Direction | Signal type | Description label |
|---|---|---|---|
| 1 | in | ICoreDouble | u |
| 2 | in | ICoreDouble | y |
| 3 | out | ICoreDouble | xhat |
Ports the constructor creates. A block whose port list changes with its configuration adds or removes ports at load time; the count above is the one a freshly inserted block has.
Configuration variables#
| Config variable | Default | Simulink parameter |
|---|---|---|
State Variables | x1 x2 | — |
Input Variables | u | — |
State Transition f | [x1 + 0.05*x2; x2 + 0.05*((1 - x1^2)*x2 - x1 + u)] | — |
Measurement h | x1 | — |
Jacobians | Numerical%~%Exact~~Numerical | — |
Process Noise Q | [0.001 0; 0 0.01] | — |
Measurement Noise R | 0.2 | — |
Initial State | [2; 0] | — |
Initial State Covariance | 1 | — |
Estimator Type | Current%~%Delayed~~Current | — |
Every block also carries Sampling Time (s) from ICoreBlockSolverEnvironment: zero or less inherits the solver's rate, a positive value runs the block at that period.
Simulink bridge#
| support | Support::None |
| Simulink path | — |
| port-count rule | PortsParam::None |
SampleTime parameter | yes |
Caveat (shown to the user): Simulink's Extended Kalman Filter block names its state transition and measurement as MATLAB functions on the path, and ICore's are typed expressions, so nothing a model could carry maps onto it. The block is reported rather than dropped when a model crosses
Catalog contract: src/ICoreBlocks/ICoreCoder/ICoreCommandSystem/SimulinkBridge/ICoreSimulinkBlockCatalog.h
Description vs code#
The checker has a blind spot here — it could not resolve something (a grouped port bullet, a computed config name), which is reported and never counted as a pass. A reader has to settle it:
B0no sample under docs/generated/samples/ — nothing to cross-check (P8.1)
The verdict above is
tools/docs/check_block_descriptions.py(P7.1), which compares LISTS. It cannot read a sentence: "stateless" on a block with a state, an initial-value semantic the recursion does not implement, a "not synthesizable" caveat the HDL banner contradicts. That is the agent audit (P7.3) on BLOCK_DESCRIPTION_AUDIT.md, and this tool's green is not a substitute for one.
File banner (developer view)#
The top comment of the block's .cpp — the maths, the realization and the export strategy, addressed to whoever changes it. It must not contradict the description above (P7.5).
Extended Kalman Filter -- MATLAB's square-root EKF over typed f(x, u) and h(x, u) correct H = dh/dx; K from Sy = qrFactor(H, S, Rs); x = x + K (y - h(x, u)) S = qrFactor(I - K H, S, K Rs) output x[k|k] (current estimator) or x[k|k-1] (delayed) predict F = df/dx; x = f(x, u); S = qrFactor(F, S, Qs)
The filter is ICoreExtendedKalmanSupport's, written once for the live run and once as statements for the ten exports. This file holds the configuration, the ports, the state (x and S, zero until the first sample takes them from the configuration) and the per-target wrappers around the statement program.
⚠ MEASURED AGAINST R2026a's extendedKalmanFilter (the object the Simulink block runs) on 200 random problems of 1 to 4 states, 1 to 3 measurements and 0 to 2 inputs, 60 samples each:
exact Jacobians 196 / 196 finite runs within 5.2e-14 relative, 21 bit-identical numerical Jacobians (default) 196 / 199 within 1e-6, median 6.8e-9
The numerical mode's residue is the forward difference amplifying last-ulp differences in f and h themselves -- MATLAB evaluates x^2 as x*x, the symbolic engine as pow(x, 2) -- and every one-state problem agrees bit for bit. The emitted Python matches the live run bit for bit, 120 / 120 samples.
Sample results#
No sample run is committed for this block. Samples come from the headless harness (DOCS_PLAN.md P8.1) into docs/generated/samples/; until one exists this block's behaviour is witnessed by the parity and export-verification suites, not by a plot here.