Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Kalman filter struct

Struct defintion

The KalmanFilter struct is the central object of our implementation. It bundles together the current state of the filter, its uncertainty, and the matrices that govern its dynamics and observations.

#![allow(unused)]
fn main() {
pub struct KalmanFilter {
    _state: DVector<f64>,
    _covariance: DMatrix<f64>,
    _state_transition_matrix: DMatrix<f64>,
    _observation_matrix: DMatrix<f64>,
    _state_noise_covariance: DMatrix<f64>,
    _observation_noise_covariance: DMatrix<f64>,
}
}

We rely on the nalgebra crate for linear algebra operations. Its DVector and DMatrix types are dynamically sized, meaning their dimensions are not fixed at compile time but can be determined at runtime. Together, these fields provide all the ingredients needed to perform the two alternating steps of the Kalman filter algorithm: prediction (using ) and correction (using ).

Custom error Enum

#![allow(unused)]
fn main() {
#[derive(Debug, thiserror::Error)]
pub enum KalmanError {
    /// Innovation covariance was not symmetric and positive definite
    #[error("innovation covariance is not SPD")]
    InnovationNotSpd,
    /// Dimension mismatch error
    #[error("dimension mismatch: {0}")]
    Dim(String),
}
}

The KalmanError enum defines the possible errors that may arise when running the filter.

  • InnovationNotSPD
    This error is triggered when the innovation covariance matrix is not symmetric positive definite (SPD).

  • Dim(String)
    This error indicates a mismatch in matrix or vector dimensions. For example, if the state vector size does not match the number of rows in the transition matrix, the filter cannot proceed. The String message provides more context about the mismatch.

By defining a custom error type, we make the library easier to debug and integrate into larger applications. Instead of panicking on invalid inputs, we return descriptive error messages that the user can handle.