Skip to content

feat: add KalmanFilter, a scalar Kalman filter with sensor fusion - #7592

Open
alxkm wants to merge 1 commit into
TheAlgorithms:masterfrom
alxkm:feat/kalman-filter
Open

feat: add KalmanFilter, a scalar Kalman filter with sensor fusion#7592
alxkm wants to merge 1 commit into
TheAlgorithms:masterfrom
alxkm:feat/kalman-filter

Conversation

@alxkm

@alxkm alxkm commented Sep 6, 2026

Copy link
Copy Markdown
Member

Adds a scalar (one-dimensional) Kalman filter under com.thealgorithms.streaming, the standard entry point to recursive Bayesian state estimation. The repository has no Kalman filter yet.

The filter tracks a quantity that drifts while every measurement of it is noisy:

predict:  x <- x + u            p <- p + q
update:   k <- p / (p + r)      x <- x + k * (z - x)      p <- (1 - k) * p

The gain k is the share of the measurement that gets believed; the variances tune it, not the caller. Calling update once per sensor, each with its own noise, is sensor fusion, and fuse(double[], double[]) gives the same thing in closed form. Both steps are O(1) in time and memory. (The covariance update is coded as p * r / (p + r) rather than (1 - k) * p; the two are algebraically equal, but the latter loses most of its significant digits when the gain is near one, as it is on the first measurements.)

KalmanFilterTest covers 17 cases. Two of them check the implementation against independent ground truth: with a diffuse prior and no process noise the filter must reduce to the running mean, and per-sensor updates must equal the closed-form inverse-variance fusion. The rest cover argument validation, covariance growth and shrinkage, control input, noise reduction on a constant signal, tracking a ramp, and reset.

Checklist

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

Co-authored-by: Oleksandr Klymenko <19151554+alxkm@users.noreply.github.com>
Signed-off-by: alxkm <19151554+alxkm@users.noreply.github.com>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.79%. Comparing base (14c8db8) to head (670caec).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7592      +/-   ##
============================================
+ Coverage     80.74%   80.79%   +0.04%     
- Complexity     7570     7598      +28     
============================================
  Files           818      819       +1     
  Lines         24257    24320      +63     
  Branches       4772     4775       +3     
============================================
+ Hits          19587    19650      +63     
- Misses         3906     3907       +1     
+ Partials        764      763       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants