Project documentation

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

C++ API reference

Header-mostly, C++23, no external dependencies. The Python binding compiles the same headers without modification.

#include <kalman/kalman.hpp>      // fixed-size templates
#include <kalman/dynamic.hpp>     // runtime-sized classes (Dyn*)
#include <kalman/bank.hpp>        // LinearKalmanFilterBank

Namespace: kalman (alias rk inside bindings.cpp).

Types

Scalar

using Scalar = double; Always double. Float32 is not in scope.

Vec<N> / Mat<R, C>

Fixed-size column-vector / row-major matrix, naturally aligned (no alignas(64) — see CHANGELOG v0.3.0). Underlying storage is std::array<Scalar, R * C>.

kalman::Vec<3> v{1.0, 2.0, 3.0};       // aggregate init
kalman::Mat<2, 2> m{1, 0,  0, 1};       // row-major
m(0, 1) = 0.5;                          // operator(row, col)
auto t = kalman::transpose(m);
auto p = m * v;                         // matmul

Free helpers in <kalman/kalman.hpp>:

DynVec / DynMat

Runtime-sized counterparts in <kalman/dynamic.hpp>. Storage is std::vector<Scalar>. Same operator surface ((i, j), *, +, -, transpose, inverse, symmetrize).

Filters

LinearKalmanFilter<NX, NZ>

template <std::size_t NX, std::size_t NZ>
struct LinearKalmanFilter {
    Vec<NX> x{};
    Mat<NX, NX> P = Mat<NX, NX>::identity();
    Mat<NX, NX> F = Mat<NX, NX>::identity();
    Mat<NX, NX> Q{};
    Mat<NZ, NX> H{};
    Mat<NZ, NZ> R = Mat<NZ, NZ>::identity();
    UpdateStats<NX, NZ> last{};

    void predict() noexcept;
    void predict_kinematic(Scalar dt) noexcept;     // NX == 2*NZ or 3*NZ only
    UpdateStats<NX, NZ> correct(const Vec<NZ>& z, Scalar eps = kDefaultEps);
    UpdateStats<NX, NZ> update(const Vec<NZ>& z, Scalar eps = kDefaultEps);
    UpdateStats<NX, NZ> update(const Vec<NZ>& z, Scalar dt, Scalar eps = kDefaultEps);
};

correct dispatches to scalar_correct when NZ == 1, using the rank-1 form P -= k · vᵀ (see CHANGELOG). For NZ > 1 it uses the standard form P -= K · (H · P). joseph_covariance(...) is still available if you want the symmetric Joseph form for stability over the standard form.

predict_kinematic(dt) updates P in place via row-shear / column-shear operations that exploit the kinematic block structure of F. Only legal when NX == 2*NZ (constant velocity) or NX == 3*NZ (constant acceleration).

AdaptiveKalmanFilter<NX, NZ>

Wraps a LinearKalmanFilter<NX, NZ> (accessible as .kf) and updates R online with an exponential forgetting factor:

template <std::size_t NX, std::size_t NZ>
struct AdaptiveKalmanFilter {
    LinearKalmanFilter<NX, NZ> kf;
    Scalar forgetting = 0.95;
    Scalar min_variance = 1e-6;

    UpdateStats<NX, NZ> correct(const Vec<NZ>& z, Scalar eps = kDefaultEps);
    UpdateStats<NX, NZ> update(const Vec<NZ>& z, Scalar eps = kDefaultEps);
};

ExtendedKalmanFilter<NX, NZ>

Callback-templated; predict/correct accept callables.

template <std::size_t NX, std::size_t NZ>
struct ExtendedKalmanFilter {
    Vec<NX> x{};
    Mat<NX, NX> P, Q;
    Mat<NZ, NZ> R;
    UpdateStats<NX, NZ> last{};

    template <class TransitionFn, class JacobianFn>
    void predict(TransitionFn&&, JacobianFn&&);

    template <class MeasurementFn, class JacobianFn>
    UpdateStats<NX, NZ> correct(const Vec<NZ>& z, MeasurementFn&&, JacobianFn&&,
                                Scalar eps = kDefaultEps);
};

IteratedExtendedKalmanFilter<NX, NZ>

Like EKF; correct takes max_iterations and tolerance for Gauss-Newton refinement.

UnscentedKalmanFilter<NX, NZ>

Carries sigma-point weights (wm, wc) and parameters (alpha, beta, kappa). predict / correct are template methods on a callable. See include/kalman/kalman.hpp for the precise signatures.

EnsembleKalmanFilter<NX, NZ, EnsembleSize>

Deterministic 8-member ensemble. The Python binding only exposes EnsembleSize == 8 so that's the de-facto contract.

LinearKalmanFilterBank<NX, NZ>

K-independent bank of LinearKalmanFilter<NX, NZ> with shared F, Q, H and per-symbol x, P, R. Internal storage is SoA, k-inner:

x[i, k]      = x_data[i * K + k]      // component i of filter k
P[i, j, k]   = P_data[(i * NX + j) * K + k]
R[i, j, k]   = R_data[(i * NZ + j) * K + k]

This makes predict() and correct() vectorize across the K axis. The scalar-NZ correct uses a rank-1 update for the full K-batch; for NZ > 1 the binding falls back to a per-filter loop.

template <std::size_t NX, std::size_t NZ>
class LinearKalmanFilterBank {
    Mat<NX, NX> F, Q;
    Mat<NZ, NX> H;

    void predict() noexcept;
    void predict_kinematic(Scalar dt) noexcept;
    void correct(const Scalar* z) noexcept;         // shape (NZ, K), k-inner
    void update(const Scalar* z) noexcept;
};

DynLinearKalmanFilter, DynAdaptiveKalmanFilter, DynExtendedKalmanFilter, DynIteratedExtendedKalmanFilter, DynUnscentedKalmanFilter, DynEnsembleKalmanFilter, DynLinearKalmanFilterBank

Runtime-sized counterparts in <kalman/dynamic.hpp>. Same algorithm, same behavior; loops bound at runtime over nx, nz. The Python binding picks Dyn for any (nx, nz) outside the 9 templated pairs — see Dispatch.

These classes accept (nx, nz) (and (K) for the bank) at construction:

kalman::DynLinearKalmanFilter kf(7, 3);   // nx=7, nz=3
kf.x = kalman::DynVec(7);
kf.H = kalman::DynMat(3, 7);

Utilities

LinearRecommendation linear_recommendation(
    const std::vector<double>& samples, std::size_t count,
    std::size_t nx, std::size_t nz, double dt,
    const std::string& model, double minimum_variance,
    const std::vector<double>& delta_t);

Heuristic estimator for Q and R from a sample trace. Returns a struct containing recommended covariances + a fitted (F, Q, H, R). The Python tuner classes (LinearKalmanTuner, ...) wrap this.

UpdateStats<NX, NZ>

template <std::size_t NX, std::size_t NZ>
struct UpdateStats {
    Vec<NZ> innovation{};
    Mat<NZ, NZ> S{};
    Mat<NX, NZ> K{};
    Scalar nis{};
    bool ok{true};
};

nis is the normalized innovation squared, yᵀ S⁻¹ y, useful for outlier gating.

Build

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

kalman::kalman is mostly an interface (headers); the linkable libkalman exposes the linear_recommendation helper and the version string. Headers require C++23.

See Build for CMake options.