arduino-audio-tools
Loading...
Searching...
No Matches
KalmanFilter.h
Go to the documentation of this file.
1#pragma once
2
3namespace audio_tools {
4
28 public:
36 KalmanFilter(float process_noise = 0.01f, float measurement_noise = 1.0f) {
37 begin(process_noise, measurement_noise);
38 }
39
56 bool begin(float process_noise, float measurement_noise) {
57 Q = process_noise;
58 R = measurement_noise;
59 P = 1.0;
60 X = 0.0;
61 return true;
62 }
63
70 bool begin() {
71 X = 0;
72 P = 1.0;
73 return true;
74 }
75
79 void end() {
80 X = 0;
81 P = 1.0;
82 }
83
91 void addMeasurement(float measurement) {
92 // Prediction update
93 P = P + Q;
94
95 // Measurement update
96 K = P / (P + R);
97 X = X + K * (measurement - X);
98 P = (1 - K) * P;
99 }
100
106 float calculate() { return X; }
107
108 protected:
112 float Q;
116 float R;
120 float P;
124 float X;
128 float K;
129};
130
131} // namespace audio_tools
Simple 1D Kalman Filter for smoothing measurements.
Definition KalmanFilter.h:27
float calculate()
Returns the current estimated value.
Definition KalmanFilter.h:106
bool begin(float process_noise, float measurement_noise)
reset the filter with new parameters
Definition KalmanFilter.h:56
float Q
Process noise covariance (Q)
Definition KalmanFilter.h:112
float R
Measurement noise covariance (R)
Definition KalmanFilter.h:116
float K
Kalman gain (K)
Definition KalmanFilter.h:128
bool begin()
Reset the filter state to zero, keeping the current noise parameters.
Definition KalmanFilter.h:70
void end()
End or clear the filter (sets the estimate to zero).
Definition KalmanFilter.h:79
KalmanFilter(float process_noise=0.01f, float measurement_noise=1.0f)
Construct a new Kalman Filter object.
Definition KalmanFilter.h:36
float X
Estimated state (X)
Definition KalmanFilter.h:124
float P
Estimation error covariance (P)
Definition KalmanFilter.h:120
void addMeasurement(float measurement)
Updates the filter with a new measurement and returns the filtered value.
Definition KalmanFilter.h:91
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6