Recursive Filters: SMA, EMA, Low‑Pass, and a Tiny Kalman
Posted by kamilstaszewski 6 days ago
Comments
Comment by jampekka 4 days ago
Specifically:
alpha = (Q + 2*R - sqrt(Q^2 + 4*Q*R))/(2*R)
This also concretizes that the Kalman filter is not adaptive in the sense of adapting to the data. The filtering behavior is fully specified by the parameterization.The sometimes useful part is that with this formulation you can get the alpha parameter for an EMA in terms of the sensor and process noises.
Comment by quietbritishjim 4 days ago
I remember using a Kalman filter many years ago and getting confused when a measurement was fairly far from the current estimate, and the uncertainty still went down. Surely the uncertainty should go up if a measurement shows that our previous estimate was unreliable? I spent a lot of time debugging my code before someone pointed out to me that the Kalman filter does not adjust its uncertainties (and therefore the effective value of alpha) at all based on the data.
But a Kalman filter does not converge to EMA if either:
* Measurements occur at irregular intervals
* Measurements can have different errors (which you can meaningfully estimate)
These are exactly the situations when it's worth using a Kalman filter instead.
Comment by deepsun 4 days ago
> Tune by eyeballing lag vs. noise.
In any serious installations (more than a homelab) you want some numeric metrics, not eyeballing. E.g. management or next engineer may reasonably ask "why alpha is 0.8? what's the rationale, why not 0.85", you want some formula to show it's what we want.
Comment by jason_s 4 days ago
Comment by CamperBob2 4 days ago
That being said, it'd be good to update this post to clarify the difference between angular frequency and plain old cycles-per-second frequency, as one of the commenters calls out.
Comment by buildingrobots 4 days ago
Comment by CamperBob2 3 days ago
double omega_c = TWO_PI * cutoff_Hz * dt_s;
double b = 4.0 - (2.0 * cos(omega_c));
return 1.0 - ((b - sqrt(b*b - 4.0)) * 0.5);
Probably won't matter most of the time, though, when you're just trying to make some noise go away.Comment by myky22 4 days ago
Comment by adenjoe 4 days ago
Comment by reindeer2 4 days ago
Comment by adenjoe 4 days ago