This project provides a MATLAB interface to a C++ implementation of a Simple Kalman Filter using MEX files. This allows MATLAB users to leverage the performance of C++ while working in their familiar MATLAB environment.
- High Performance: C++ implementation for computational efficiency
- MATLAB Integration: Native MATLAB interface using MEX files
- Object-Oriented: MATLAB wrapper class for easy usage
- Batch Processing: Process multiple measurements efficiently
- Visualization: Built-in plotting capabilities for analysis
- Memory Safe: Automatic cleanup of C++ resources
kalman.h/kalman.cpp- C++ Kalman filter implementationkalman_mex.cpp- MEX interface between MATLAB and C++
KalmanFilter.m- Object-oriented MATLAB wrapper classbuild_mex.m- Script to compile the MEX filekalman_demo.m- Comprehensive demonstration script
utils.h/utils.cpp- Utility functionsmain.cpp- C++ standalone demo (optional)
Open MATLAB and navigate to this directory, then run:
build_mex()This will compile the C++ code into a MEX file that MATLAB can call.
kalman_demo()This will run a comprehensive demonstration showing various features of the Kalman filter.
% Create a Kalman filter
filter = KalmanFilter(0.0, 1.0, 0.01, 0.1);
% Process measurements one by one
measurements = [4.9, 5.1, 4.8, 5.2, 4.95];
for i = 1:length(measurements)
filter.step(measurements(i)); % predict + update
fprintf('Estimate: %.4f\n', filter.getState());
end
% Or process all measurements at once
[states, uncertainties] = filter.process(measurements);
% Visualize results
filter.plotResults(measurements, 'TrueValue', 5.0);filter = KalmanFilter(initial_estimate, initial_error, process_noise, measurement_noise)- initial_estimate: Your first guess of the true value
- initial_error: How uncertain you are about your initial guess
- process_noise: How much you expect the true value to change over time
- measurement_noise: How noisy/unreliable your sensor measurements are
predict()- Perform prediction stepupdate(measurement)- Perform measurement updatestep(measurement)- Combined predict + updategetState()- Get current state estimategetErrorCovariance()- Get current uncertainty
process(measurements)- Batch process multiple measurementsreset(initial_estimate, initial_error)- Reset filtergetCurrentState()- Get complete state structureplotResults(measurements, ...)- Visualize performance
If you prefer to use the MEX interface directly:
% Create filter
filter_id = kalman_mex('create', 0.0, 1.0, 0.01, 0.1);
% Use filter
kalman_mex('predict', filter_id);
kalman_mex('update', filter_id, 5.0);
state = kalman_mex('getState', filter_id);
% Clean up
kalman_mex('delete', filter_id);% Generate noisy measurements around true value of 10.0
true_value = 10.0;
noise_std = 0.5;
measurements = true_value + noise_std * randn(100, 1);
% Create and use filter
filter = KalmanFilter(0.0, 2.0, 0.01, noise_std^2);
[filtered_states, uncertainties] = filter.process(measurements);
% Compare results
fprintf('True value: %.2f\n', true_value);
fprintf('Mean measurement: %.2f (std: %.2f)\n', mean(measurements), std(measurements));
fprintf('Final filtered estimate: %.2f\n', filtered_states(end));
fprintf('Final uncertainty: %.4f\n', uncertainties(end));% Simulate real-time sensor data
filter = KalmanFilter(0.0, 1.0, 0.001, 0.1);
figure;
h_meas = plot(NaN, NaN, 'b.', 'MarkerSize', 10);
hold on;
h_filt = plot(NaN, NaN, 'r-', 'LineWidth', 2);
legend('Measurements', 'Filtered', 'Location', 'best');
for i = 1:100
% Simulate new measurement
measurement = 5.0 + 0.1 * randn();
% Update filter
filter.step(measurement);
state = filter.getState();
% Update plot
h_meas.XData(end+1) = i;
h_meas.YData(end+1) = measurement;
h_filt.XData(end+1) = i;
h_filt.YData(end+1) = state;
drawnow;
pause(0.1);
end% Test different noise parameters
true_value = 7.5;
measurements = true_value + 0.2 * randn(50, 1);
% Different process noise values
process_noises = [0.001, 0.01, 0.1];
colors = {'b', 'r', 'g'};
figure;
hold on;
plot(measurements, 'k.', 'MarkerSize', 8, 'DisplayName', 'Measurements');
for i = 1:length(process_noises)
filter = KalmanFilter(0.0, 1.0, process_noises(i), 0.04);
[states, ~] = filter.process(measurements);
plot(states, colors{i}, 'LineWidth', 2, ...
'DisplayName', sprintf('Q = %.3f', process_noises(i)));
end
yline(true_value, 'k--', 'LineWidth', 2, 'DisplayName', 'True Value');
legend('Location', 'best');
title('Effect of Process Noise Parameter');- MATLAB R2017b or later (for C++17 support)
- MATLAB Coder (for MEX compilation)
- Visual Studio 2017 or later, OR
- MinGW-w64 compiler
- Xcode Command Line Tools:
xcode-select --install - macOS 10.14 or later
- GCC 7.0 or later with C++17 support
- Build essentials:
sudo apt-get install build-essential(Ubuntu/Debian)
If you encounter compilation issues, configure your compiler:
mex -setup cpp-
MEX file not found
- Run
build_mex()to compile the MEX file - Check that compilation completed without errors
- Run
-
Compiler not found
- Install appropriate compiler for your platform
- Run
mex -setup cppin MATLAB
-
C++17 not supported
- Update your compiler
- Use MATLAB R2017b or later
-
Memory issues
- The MATLAB wrapper automatically manages memory
- Use
clear classesif experiencing issues
-
Use batch processing for large datasets:
[states, errors] = filter.process(measurements); % Fast
Instead of:
for i = 1:length(measurements) % Slower filter.step(measurements(i)); end
-
Reuse filter objects instead of creating new ones
-
Choose appropriate parameters based on your system characteristics
The Kalman filter implemented here is a 1D (scalar) filter that operates in two steps:
-
Predict: Project the current state forward in time
- State prediction:
x_pred = x_current(constant model) - Covariance prediction:
P_pred = P_current + Q
- State prediction:
-
Update: Correct the prediction using a measurement
- Kalman gain:
K = P_pred / (P_pred + R) - State update:
x_new = x_pred + K * (measurement - x_pred) - Covariance update:
P_new = (1 - K) * P_pred
- Kalman gain:
Where:
Qis the process noise (how much the system changes)Ris the measurement noise (how noisy sensors are)Pis the error covariance (uncertainty in estimate)
This project is provided as-is for educational and research purposes.
Feel free to submit issues, suggestions, or improvements to enhance the MATLAB interface or extend functionality.