Project documentation

Fixed-size C++23 state estimation with a thin Python interface.

Getting started

Install (Python)

pip install fast-kalman

The wheel is cp312-abi3-<plat> and works on CPython ≥ 3.12 on Linux x86_64, aarch64 (Linux), arm64 (macOS), LoongArch64, and ppc64le. Non-x86 Linux variants are published in the project's GitHub Releases until PyPI supports them.

At import time the package detects the host CPU and picks the best ISA-specific backend it ships:

import kalman
kalman.backend()
# 'x86_v4' on AVX-512 hosts, 'x86_v3' on AVX2, 'generic' otherwise
kalman.available_backends()
# [(short_name, compiled_into_wheel, supported_by_host), ...]

See Dispatch for the priority list.

Your first filter (Python)

A constant-velocity 1D tracker (position + velocity, position-only measurement):

import kalman, numpy as np

kf = kalman.make_constant_velocity_1d(
    initial_position=0.0,
    initial_velocity=0.0,
    dt=1.0,
    position_variance=1.0,
    velocity_variance=1.0,
    process_position_variance=1e-4,
    process_velocity_variance=1e-3,
    measurement_variance=0.5,
)

measurements = np.array([1.0, 2.1, 3.0, 4.2, 5.0])
result = kf.update_many(measurements, record_states=True)

print(result["x"])         # final filtered [position, velocity]
print(result["states"])    # shape (5, 2) trajectory

Same model, but updating one step at a time:

kf = kalman.make_constant_velocity_1d(...)
for z in measurements:
    stats = kf.update([z])    # returns a lazy stats handle
    if not stats:             # __bool__ is the .ok flag
        # innovation covariance was singular this tick
        ...

Your first filter (C++)

#include <kalman/kalman.hpp>

kalman::LinearKalmanFilter<2, 1> kf{};
kf.x = {{0.0, 0.0}};
kf.P = kalman::Mat<2, 2>::identity();
kf.F = {{1.0, 1.0, 0.0, 1.0}};
kf.Q = {{1e-4, 0.0, 0.0, 1e-3}};
kf.H = {{1.0, 0.0}};
kf.R = {{0.5}};

for (double z : measurements) {
    kf.update({z});
}

Build with CMake:

find_package(kalman REQUIRED)
target_link_libraries(my_app PRIVATE kalman::kalman)

The library is header-mostly; the kalman::kalman target also ships a small .so / .a for utilities like model-recommendation helpers.