by joezydeco · 10y ago · 0 comments
In the back of my head I remembered one word: Kalman. This line of code fixed it right up:
static float display_temp = 0;
display_temp += 0.04 * (adc_temp - display_temp);The general model for a linear system is: x(t+1) = A * x(t) + B * u(t) + v1(t) y(t) = C * x(t) + D * u(t) + v2(t)
Generally, x (state), y (measurement) and u (input) are vectors, A,B,C,D matrices and v1, v2 noise.
The optimal Kalman estimate for this system is: xhat(t+1|t) = A * xhat(t|t-1) + B * u(t) + K(y(t) - C * x(t|t-1)
where K is the Kalman gain which might be calculated for each update step or in the case of a stationary Kalman filter will be calculated to a fix value. The idea behind using a fix value is that generally, K(t) will converge as the filter runs.
Now say the state x is scalar, the model is that x is always the same and unaffected by input (A = 1, B = 0) and that we measure the state directly and that also the measurement is unaffected by input (C = 1, D = 0). Let's also say K = 0.04, it might well be.
Then: xhat(t+1|t) = xhat(t|t-1) + 0.04 * (y(t) - x(t|t-1) <=> display_temp += 0.04 * (adc_temp - display_temp)
Thus for the model that 1) the oven temperature is constant and 2) unaffected (at least not quickly affected) by the temperature control and 3) that we measure temperature directly and 4) the assumption that the stationary Kalman gain is 0.04, this is a (stationary) Kalman filter.
A classic example is a fuel gauge where you want to reject low frequency sloshing but have a rapid startup without knowing in advance what the level really is.
I'm skeptical that anyone sane would credit this obvious idea should to one particular person, which is why it can't be the Kalman filter.
The alpha-beta filter is the position/velocity version, and is commonly seen in settings where less is known about the system dynamics, or there's not enough CPU for matrix math. See https://en.wikipedia.org/wiki/Alpha_beta_filter
I ask because I don't know enough about the Kalman Filter. But it seems that the parent post could also be accurate.
I imagine many implementations of the Kalman Filter take advantage of the local use case, and don't necessarily have to carry a fully generalised Kalman filter.
There are at least a couple dozen of commonly used filters that can be understood as form of the alpha-beta filter. Some use constants for g/h, some vary them over time. The Kalman filter varies them on each epoch based on the covariance of the state and measurements. There are other schemes. The KF is optimal in the least squares sense when the noise is Gaussian and and the system obeys the Markov property.
Another way to look at these is to derive them from Bayes' theorem. You can derive both the alpha-beta filter and Kalman filter from Bayes' theorem. It's all the same family, just with different assumptions/knowledge about your process and measurement noise.
https://en.wikipedia.org/wiki/Moving_average#Exponential_mov...