Kalman Filter

An algorithm uses a series of smoothing noisy measurements over time

In One Line

Too Noise? Filter it. Make Noise? Take me. Burst? Ready.

Package Info

display name

AceLand Kalman Filter

package name

latest version

2.2.1

namespace

dependencies


Overview

Kalman Filter is known as linear quadratic estimation. Simple put, this is a denoise filter.

please read this wiki document for details.

In wiki document, it may be too complicated if you are not familiar to Mathmatics. Basically we use the following values to estimate the result (prediction). What you need is to know the following values:

Q

process noise covariance. How much we expect the true value to change between measurements. (model uncertainty) default: 1e-6f // 0.000001

R

measurement noise covariance. How noisy the sensor/measurement is. default: 1e-3f // 0.001

P

estimate uncertainty after the measurement update step. (a scalar variance-like quantity) default: 1

K

kalman gain (0..1) the weight given to the new measurement relative to the current estimate.

X

current state estimate. (the smoothed value)

Summary

  • A compact, scalar Kalman filter suitable for smoothing noisy readings.

  • Tune Q and R to balance smoothness vs responsiveness.

  • Use Update for streaming data or batch updates for historical sequences.

  • Inspect and control state via GetCurrentValues, SetValues, and Reset.

  • Customizable value adapter can modularizecalculation process.

  • Jobs and ECS ready.


Machanizm

Core idea in plain words

  • The filter maintains a running best-guess X of the true value and a notion of confidence P.

  • Each new measurement is blended with the current estimate using a weight K (the Kalman gain).

  • If measurements are noisy (high R), the filter trusts its prior estimate more; if the process is volatile (high Q), it adapts faster to change.

  • Over time, the filter converges to a smooth, lag-minimized estimate.

Update math (intuitive)

  1. Optionally update noise parameters:

    • If newQ or newR are provided, the filter updates Q and/or R for this call onward.

  2. Compute the Kalman gain K using current uncertainty and noise:

    • Predict uncertainty: P + Q.

    • Combine with measurement noise: P + Q + R.

    • Gain: K = (P + Q) / (P + Q + R).

    • Intuition: If R is large (noisy sensor), K gets smaller; if Q is large (we expect changes), K gets larger.

  3. Update the posterior uncertainty P:

    • P = R * (P + Q) / (R + P + Q).

    • Intuition: After incorporating the measurement, our uncertainty shrinks, bounded by measurement noise R.

  4. Update the state estimate X:

    • X_new = X_old + K * (measurement − X_old).

    • A weighted blend: K close to 1 tracks the measurement closely; K near 0 barely moves.

Choosing Q and R

  • R (measurement noise):

    • Larger R if your sensor is noisy; this makes the filter smoother and slower to react.

    • Smaller R if measurements are reliable; the filter tracks input more tightly.

  • Q (process noise):

    • Larger Q if the underlying true value changes rapidly; the filter adapts faster to new trends.

    • Smaller Q if the true value is stable; the filter will be steadier.


Quick Start

build a Kalman Filter of supported type.


Supported Types

Our simplified Kalman Filter supported following types in Unity:

  • float

  • Vector2

  • Vector3

  • Vector4


Value Adapter

Providing a customable type calcuation to Kalman Filter for supporting any unmanaged types.

Data must be a struct, and contains with unmanaged types.


Jobs and ECS

It's easy to create a Burst Kalman Filter and Value Adapter for Jobs or ECS system.

In next version, we will create default value adapters for Jobs and ECS in default, and simplify the use of Burst Kalman Filter.

For burst compile, an adapter MUST be readonly struct .

Guidelines

  • Mark them readonly struct.

  • Do not capture managed state (no reference-type fields, no string, no delegates).

  • Only use Burst-supported APIs (Unity.Mathematics.math where helpful).

  • Expose a static instance if you need to reuse the adapter without allocations:


Best Practice

Practical guidance

  • Start with small Q (e.g., 1e-6 to 1e-3) and moderate R (e.g., 1e-3 to 1e-1). Tune empirically.

  • If you see lag (filter responds too slowly), increase Q or decrease R slightly.

  • If you see jitter (filter chases noise), decrease Q or increase R.

State management

  • GetCurrentValues lets you inspect the internal state for debugging or telemetry.

  • SetValues lets you initialize with a known starting estimate:

    • For example, set X to your first measurement, P to a moderate value (e.g., 1), K to 0.

  • Reset sets the filter to a neutral starting point (P=1, X=default, K=0).


FAQ

Why is K between 0 and 1?

  • Because K is a ratio of variances; it represents a blending weight between your prior estimate and the measurement.

Do I need to set P manually?

  • Not usually. P starts at 1 after Reset and then adapts. If you have a strong prior, set P smaller to indicate higher confidence.

What if measurements come at uneven intervals?

  • This scalar filter assumes a fixed implicit timestep. For highly irregular intervals, Q should ideally scale with time; that logic isn’t included here.

Can I change Q and R on every update?

  • Yes. Use newQ/newR parameters to adapt on-the-fly (e.g., dynamic sensor noise estimation).


Last updated