SimpleKalmanFilter

repository·master·Indexed 19 days ago

https://github.com/denyssene/simplekalmanfilter

A lightweight C++ library for implementing a unidimensional Kalman Filter, optimized for Arduino environments to smooth sensor data such as temperature, altitude, or motion. It provides the SimpleKalmanFilter class to process single-value streams using parameters for measurement uncertainty, estimation uncertainty, and process variance.

Tokens
888
Snippets
2
Records
4
Agent score
16%

What's inside simplekalmanfilter

  1. Basic Usage of SimpleKalmanFilter

    master

    The SimpleKalmanFilter class is designed for unidimensional models to process streams of single values (e.g., from barometric, temperature, or motion sensors).

    To use the library, instantiate the class with three parameters:

    1. e_mea (Measurement Uncertainty): How much you expect your measurement to vary.
    2. e_est (Estimation Uncertainty): Can be initialized with the same value as e_mea; the filter will adjust this value automatically.
    3. q (Process Variance): Represents how fast your measurement moves. Usually a small number between 0.001 and 1. A recommended starting value is 0.01. This should be tuned to your specific application.

    Use the updateEstimate(x) method to pass a new measurement and receive the filtered estimate.

    // Initialization
    // e_mea: Measurement Uncertainty
    // e_est: Estimation Uncertainty
    // q: Process Variance (recommended 0.01)
    SimpleKalmanFilter kf = SimpleKalmanFilter(e_mea, e_est, q);
    
    while (1) {
      float x = analogRead(A0);
      float estimated_x = kf.updateEstimate(x);
      
      // ... use estimated_x
    }
  2. SimpleKalmanFilter API Reference

    master

    The SimpleKalmanFilter class provides the following interface for filtering unidimensional sensor data:

    Constructor

    SimpleKalmanFilter(e_mea, e_est, q)

    • e_mea: Measurement Uncertainty.
    • e_est: Estimation Uncertainty.
    • q: Process Variance.

    Methods

    float updateEstimate(float x)

    • Takes a new measurement x and returns the updated, filtered estimate.
  3. Use the SimpleKalmanFilter class for unidimensional filtering

    master

    The SimpleKalmanFilter class provides a lightweight implementation of a Kalman Filter for single-variable models. It is used to smooth noisy measurements by maintaining an internal estimate and updating it based on new measurements and noise parameters.

    Constructor

    SimpleKalmanFilter(float mea_e, float est_e, float q)

    • mea_e: Measurement error (how much you trust the sensor).
    • est_e: Estimate error (initial uncertainty in the estimate).
    • q: Process noise (how much the system state changes between updates).

    Core Methods

    • updateEstimate(float mea): The primary method to use. Pass a new measurement (mea) to the filter; it returns the new, filtered estimate.
    • getKalmanGain(): Returns the current Kalman gain.
    • getEstimateError(): Returns the current estimate error.

    Parameter Tuning

    You can adjust the filter's behavior at runtime using:

    • setMeasurementError(float mea_e)
    • setEstimateError(float est_e)
    • setProcessNoise(float q)
    // Example initialization and usage
    SimpleKalmanFilter kf(0.1f, 1.0f, 0.01f);
    
    // In your measurement loop:
    float measurement = readSensor();
    float filteredValue = kf.updateEstimate(measurement);