Microcontroller targeted naive Kalman filter implementation in pure C using code ported from the Efficient Java Matrix Library.
A 🦀 Rust port is available at sunsided/minikalman-rs.
The project is licensed under the MIT license, a copy of which can be found in LICENSE.md.
- Memory-optimizing preprocessor based Kalman Filter factory
- Algorithmically optimized matrix/matrix and matrix/vector operations
- Matrix inverse using Cholesky decomposition
- Gravity constant estimation using only measured position
Drop the files directly into your own build — no external dependencies, no library to link.
- Add
src/cholesky.c,src/kalman.c, andsrc/matrix.cto your source list. - Add the
include/directory to your compiler's include path (-I include). - Link against the math library (
-lm).
The library uses a preprocessor-based factory to generate filter and measurement structs. Before including the factory headers you must define a few macros:
/* Control inline semantics: use static in a single translation unit */
#define EXTERN_INLINE_MATRIX static inline
#define EXTERN_INLINE_KALMAN static inline
#include <kalman.h>
/* Define a filter named "gravity" with 3 states and 0 inputs */
#define KALMAN_NAME gravity
#define KALMAN_NUM_STATES 3
#define KALMAN_NUM_INPUTS 0
#include <kalman_factory_filter.h>
/* Define a measurement named "position" with 1 output */
#define KALMAN_MEASUREMENT_NAME position
#define KALMAN_NUM_MEASUREMENTS 1
#include <kalman_factory_measurement.h>
/* Undef all KALMAN_* macros so you can define another filter */
#include <kalman_factory_cleanup.h>
/* Now use the generated names */
kalman_t *kf = kalman_filter_gravity_init();
kalman_measurement_t *kfm = kalman_filter_gravity_measurement_position_init();
kalman_predict(kf);
kalman_correct(kf, kfm);To define multiple filters, repeat the KALMAN_NAME / kalman_factory_filter.h / kalman_factory_cleanup.h cycle with different names.
The project can be built, installed, and consumed via find_package():
cmake -B build -S .
cmake --build build
cmake --install build --prefix /usr/local # or any prefixDownstream CMakeLists.txt:
find_package(kalman_clib 1.0 REQUIRED)
target_link_libraries(your_target PRIVATE kalman_clib::kalman_clib)Options:
| Option | Default | Description |
|---|---|---|
KALMAN_CLIB_BUILD_EXAMPLES |
ON |
Build example programs |
- SPDX-License-Identifier:
MIT - Upstream: https://github.com/sunsided/kalman-clib
- Copyright (c) 2014-2024 Markus Mayer - see LICENSE.md.