Estimators¶
shinro.estimators
State estimation algorithms for reconstructing system state from measurements.
Provides discrete-time state estimators that combine dynamics models with sensor measurements. All estimators implement the StateEstimator ABC.
Available estimators: KalmanFilter — Optimal stochastic filter (predict-update cycle) LuenbergerObserver — Deterministic observer with fixed gain
KalmanFilter¶
Bases: StateEstimator
Discrete-time linear Kalman filter for optimal state estimation.
Implements the predict-update cycle for a system of the form:
Tracks the posterior state estimate \(\hat{x}\) and error covariance \(P\) through the standard Kalman filter equations.
Uses column vectors \((n, 1)\) throughout (not flat \((n,)\)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
State transition matrix (n_x, n_x). |
required | |
B
|
Control input matrix (n_x, n_u). |
required | |
Q
|
Process noise covariance (n_x, n_x). |
required | |
R
|
Measurement noise covariance (n_y, n_y). |
required | |
C
|
Any | None
|
Observation matrix (n_y, n_x). Defaults to identity. |
None
|
D
|
Any | None
|
Feedthrough matrix (n_y, n_u). Defaults to zeros. |
None
|
x0
|
Any | None
|
Initial state estimate (n_x, 1). Defaults to zeros. |
None
|
backend
|
ArrayBackend | None
|
Array backend. Defaults to NumpyBackend. |
None
|
Source code in src/shinro/estimators/kalman_filter.py
estimate
¶
Run one predict-update cycle and return the posterior state estimate.
Implements the standard Kalman filter equations:
-
Predict: \(x_{\text{pred}} = A \hat{x} + B u\) \(P_{\text{pred}} = A P A^T + Q\)
-
Update: \(K = P_{\text{pred}} C^T (C P_{\text{pred}} C^T + R)^{-1}\) \(\hat{x} = x_{\text{pred}} + K (y - C x_{\text{pred}} - D u)\) \(P = (I - K C) P_{\text{pred}}\)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
measurement
|
Observation vector (n_y, 1) from sensors. |
required | |
control_input
|
Control vector (n_u, 1) applied at this step. |
required |
Returns:
| Type | Description |
|---|---|
|
Posterior state estimate \(\hat{x}\) (n_x, 1). |
Source code in src/shinro/estimators/kalman_filter.py
reset
¶
Reset the filter to its initial state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x0
|
Any | None
|
Initial state estimate (n_x, 1). Defaults to zeros. |
None
|
Source code in src/shinro/estimators/kalman_filter.py
from_config
classmethod
¶
from_config(config, backend: ArrayBackend | None = None)
Create a Kalman filter from a TOML config dict or KalmanFilterConfig.
Config fields
process_noise: Diagonal Q weights (n_x,) or full Q matrix (n_x, n_x). measurement_noise: Diagonal R weights (n_y,) or full R matrix (n_y, n_y). dt: Time step — used to set B = dt * I unless B_dynamics is given. A_dynamics: Optional full A matrix (n_x, n_x). Defaults to I. B_dynamics: Optional full B matrix (n_x, n_u). Defaults to dt * I. C: Optional full C matrix (n_y, n_x). Defaults to I. D: Optional full D matrix (n_y, n_u). Defaults to zeros.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
TOML config dict or KalmanFilterConfig. |
required | |
backend
|
ArrayBackend | None
|
Array backend. Defaults to NumpyBackend. |
None
|
Returns:
| Type | Description |
|---|---|
|
KalmanFilter instance. |
Source code in src/shinro/estimators/kalman_filter.py
LuenbergerObserver¶
Bases: StateEstimator
Luenberger observer for deterministic linear state estimation.
Implements the discrete-time observer dynamics:
where L is the observer gain chosen to place the eigenvalues of \((A - LC)\) inside the unit circle for stable estimation.
Unlike the Kalman filter, the Luenberger observer uses a fixed gain and does not assume noise statistics. No matrix inverses are needed at runtime — just three matrix-vector multiplies.
Uses column vectors \((n, 1)\) throughout (not flat \((n,)\)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
A
|
State transition matrix (n_x, n_x). |
required | |
B
|
Control input matrix (n_x, n_u). |
required | |
observer_gain
|
Observer gain matrix L (n_x, n_y). Must place eigenvalues of (A - LC) inside the unit circle. |
required | |
C
|
Any | None
|
Output matrix (n_y, n_x). Defaults to identity. |
None
|
D
|
Any | None
|
Feedthrough matrix (n_y, n_u). Defaults to zeros. |
None
|
x0
|
Any | None
|
Initial state estimate (n_x, 1). Defaults to zeros. |
None
|
backend
|
ArrayBackend | None
|
Array backend. Defaults to NumpyBackend. |
None
|
Source code in src/shinro/estimators/luenberger_observer.py
estimate
¶
Perform one step of state estimation.
Computes the predicted state from the dynamics, calculates the innovation (measurement residual), and corrects the prediction using the observer gain:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
measurement
|
Output measurement \(y_k\) (n_y, 1). |
required | |
control_input
|
Control input \(u_k\) (n_u, 1). |
required |
Returns:
| Type | Description |
|---|---|
|
Updated state estimate \(\hat{x}_{k+1}\) (n_x, 1). |
Source code in src/shinro/estimators/luenberger_observer.py
reset
¶
Reset the observer to its initial state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x0
|
Any | None
|
Initial state estimate (n_x, 1). Defaults to zeros. |
None
|
Source code in src/shinro/estimators/luenberger_observer.py
from_config
classmethod
¶
from_config(config, backend: ArrayBackend | None = None)
Create a Luenberger observer from a TOML config dict or LuenbergerObserverConfig.
Config fields
observer_gain: Diagonal gain weights (n_x,) or full gain matrix (n_x, n_y). dt: Time step — used to set B = dt * I unless B_dynamics is given. A_dynamics: Optional full A matrix (n_x, n_x). Defaults to I. B_dynamics: Optional full B matrix (n_x, n_u). Defaults to dt * I. C: Optional full C matrix (n_y, n_x). Defaults to I. D: Optional full D matrix (n_y, n_u). Defaults to zeros.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
TOML config dict or LuenbergerObserverConfig. |
required | |
backend
|
ArrayBackend | None
|
Array backend. Defaults to NumpyBackend. |
None
|
Returns:
| Type | Description |
|---|---|
|
LuenbergerObserver instance. |