Kalman Filter (kalman)
Single-variable linear Kalman filter implementing predict-update cycle with configurable state transition, observation, process noise, and measurement noise parameters.
Data Structure
struct kalman_filter_t {
float a, h;
float q, r;
float x, p;
float k, a2, h2;
};
API
struct kalman_filter_t * kalman_alloc(float a, float h, float q, float r);
void kalman_free(struct kalman_filter_t * filter);
float kalman_update(struct kalman_filter_t * filter, float value);
void kalman_clear(struct kalman_filter_t * filter);
kalman_alloc— Allocate a Kalman filter.ais the state transition factor,his the observation factor,qis the process noise variance,ris the measurement noise variance. Returns NULL on failurekalman_free— Free the filterkalman_update— Feed a new measurementvalue, perform predict-update cycle, return the filtered estimatekalman_clear— Reset the filter state (sets estimate to NaN, error covariance to q, gain to 1)
Example
struct kalman_filter_t * kf = kalman_alloc(1.0f, 1.0f, 0.01f, 0.1f);
if(!kf)
return;
float measurements[] = { 10.1f, 9.8f, 10.3f, 9.9f, 10.0f };
for(int i = 0; i < 5; i++)
{
float estimate = kalman_update(kf, measurements[i]);
}
kalman_free(kf);