A high-performance C++ implementation of a Simple Kalman Filter with a complete MATLAB interface using MEX files.
kalmancpp/
├── src/ # C++ source code
│ ├── kalman.h # Kalman filter header
│ ├── kalman.cpp # Kalman filter implementation
│ ├── utils.h # Utility functions header
│ └── utils.cpp # Utility functions implementation
├── matlab/ # MATLAB interface
│ ├── kalman_mex.cpp # MEX interface code
│ ├── KalmanFilter.m # MATLAB wrapper class
│ ├── build_mex.m # MEX compilation script
│ └── test_kalman_interface.m # Test script
├── examples/ # Example code and demos
│ ├── main.cpp # C++ standalone example
│ └── kalman_demo.m # MATLAB demo script
├── docs/ # Documentation
│ └── MATLAB_README.md # Detailed MATLAB usage guide
└── README.md # This file
-
Build the C++ project:
g++ -std=c++17 -Wall -Wextra -g -o program src/kalman.cpp src/utils.cpp examples/main.cpp
-
Run the example:
./program
-
Open MATLAB and navigate to the project root directory
-
Build the MEX interface:
cd matlab build_mex()
-
Run the demo:
cd ../examples kalman_demo()
-
Or use the object-oriented interface:
addpath('matlab') filter = KalmanFilter(0.0, 1.0, 0.01, 0.1); filter.step(5.0); % Process a measurement estimate = filter.getState();
- High Performance: Optimized C++ implementation
- Simple Interface: Easy-to-use class-based API
- Well Documented: Extensive comments explaining the theory
- Memory Safe: Proper resource management
- MEX Integration: High-performance C++ backend with MATLAB frontend
- Object-Oriented: Clean MATLAB class wrapper
- Batch Processing: Efficient processing of multiple measurements
- Visualization: Built-in plotting capabilities
- Error Handling: Comprehensive error checking and reporting
#include "src/kalman.h"
// Create filter: initial_estimate, initial_error, process_noise, measurement_noise
SimpleKalmanFilter filter(0.0, 1.0, 0.01, 0.1);
// Process measurements
std::vector<double> measurements = {4.9, 5.1, 4.8, 5.2};
for (double measurement : measurements) {
filter.predict();
filter.update(measurement);
std::cout << "Estimate: " << filter.getState() << std::endl;
}% Add MATLAB directory to path
addpath('matlab')
% Create filter
filter = KalmanFilter(0.0, 1.0, 0.01, 0.1);
% Process measurements
measurements = [4.9, 5.1, 4.8, 5.2];
[estimates, uncertainties] = filter.process(measurements);
% Visualize results
filter.plotResults(measurements, 'TrueValue', 5.0);- C++17 compatible compiler (GCC 7+, Clang 5+, MSVC 2017+)
- Standard library only (no external dependencies)
- MATLAB R2017b or later
- MEX compiler configured (
mex -setup cpp) - For macOS: Xcode Command Line Tools
- For Windows: Visual Studio or MinGW
- For Linux: GCC with C++17 support
- MATLAB Interface Guide - Comprehensive MATLAB usage documentation
- C++ API Documentation - Detailed C++ interface documentation
- Examples - Working examples in both C++ and MATLAB
# Build and run C++ tests
g++ -std=c++17 -Wall -Wextra -g -o program src/kalman.cpp src/utils.cpp examples/main.cpp
./programcd matlab
test_kalman_interface()This implements a simple 1D Kalman filter for tracking a scalar value. The filter operates in two steps:
- Predict: Project current state estimate forward in time
- Update: Correct prediction using new measurement
Key parameters:
- Process Noise (Q): How much the true value changes over time
- Measurement Noise (R): How noisy the sensor measurements are
- Initial Error (P): Initial uncertainty in the estimate
The filter automatically balances trust between predictions and measurements based on their relative uncertainties.
This project is provided for educational and research purposes.
Contributions are welcome! Please feel free to submit issues, suggestions, or pull requests.