FilterPy: Kalman Filtering and Optimal Estimation

1.4.5 · active · verified Sat Apr 11

FilterPy is a Python library for Kalman filtering and optimal estimation, including Kalman Filters, Extended Kalman Filters, and Unscented Kalman Filters. It provides a robust and well-documented framework for state estimation. The current version is 1.4.5, which has been stable since 2017, indicating a very mature but not actively developed codebase.

Warnings

Install

Imports

Quickstart

Initializes a simple 1D Kalman Filter to track position and velocity based on position measurements. It demonstrates the setup of the KalmanFilter object, defining its core matrices (F, H, P, R, Q), and iterating through measurements using `predict()` and `update()`.

import numpy as np
from filterpy.kalman import KalmanFilter

# Create a KalmanFilter object
kf = KalmanFilter(dim_x=2, dim_z=1)

# Initialize state vector x (position, velocity)
kf.x = np.array([[0.], [0.]])

# State transition matrix F
kf.F = np.array([[1., 1.],
                 [0., 1.]])

# Measurement function H
kf.H = np.array([[1., 0.]])

# State covariance matrix P
kf.P *= 1000.

# Measurement noise covariance R
kf.R = 5 # Measurement noise

# Process noise covariance Q
kf.Q = np.diag([0.01, 0.01]) # Simple process noise

# Simulate some measurements
z = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

# Run the filter
estimates = []
for measurement in z:
    kf.predict()
    kf.update(measurement)
    estimates.append(kf.x.T[0].tolist())

# print(estimates)
# Expected output for first few: [[...], [...]]

view raw JSON →