6 ms·
Just last week I needed to smooth out a display reading on an oven controller. The RTD was being read way too fast so I'd get a lot of flicker between values d
by joezydeco 10y ago
Just last week I needed to smooth out a display reading on an oven controller. The RTD was being read way too fast so I'd get a lot of flicker between values due to ADC resolution.
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);
- Bromskloss 10y agoWell, I guess, but, I mean, really… that's a first-order low-pass filter.
- kazinator 10y agoIndeed; it's a discrete approximation to the infinite impulse response exhibited by all sorts of physical systems leading to the characteristic exponential decay.
- gaze 10y agoThat's not a kalman filter.
- kazinator 10y agoThat's just a linear interpolation between two values: take 96% of the current display value plus 4% of the new value from the ADC, which is the same as finding 4% of the way between the two values. It's a simple generalization of the arithmetic mean, which is the special case of 50%. 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.
- deleted 10y ago[deleted]
- phkahler 10y agoYou can do better by having the 0.04 parameter follow an exponential decay as well. Start at 1.0 and have it decay to 0.04 or even 0.01 over time. That would make it more like a real Kalman filter for such a simple measurement. You'll get fast convergence to an initial value and then very smooth response after that. 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.
- skykooler 10y agoSomething that Toyota apparently doesn't do; their fuel gauges take over a minute to react fully when you turn on the car after filling the tank.
- Bromskloss 10y agoHow about a mechanical low-pass filter?: A vertical tube connected to the tank via a tiny hole would not be affected much by sloshing, but would slowly drift towards having the same level of fuel as the tank has on average.
- ams6110 10y agoThat's pretty much what fuel tanks do.
- abstrakraft 10y agoIf you're really trying to find a cooler name than the obvious low-pass IIR filter, you could call it an "alpha filter", which is equivalent to a position only (no derivatives) Kalman filter in steady-state, using a precomputed gain (called alpha, here equal to 0.04). 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 https://en.wikipedia.org/wiki/Alpha_beta_filter
- zbuf 10y agoAre alpha-beta (or alpha) filters a subset of Kalman Filters? 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.
- abstrakraft 10y agoOne of the defining characteristics of the Kalman filter is that it computes a gain (called the Kalman gain) that is a function of the state covariance and the measurement covariance. Alpha-beta filters use constants, so I'd say no, they are not a subset.
- RogerL 10y agoYou can define alpha/beta in terms of the covariance of the Kalman filter. See my book (linked above) for the derivation (I call it a g-h filter, some literature uses alpha-beta, some g-h, they are the same thing). Eli Brookner in "Tracking and Kalman Filters Made Easy" uses a different but mathematically equivalent derivation to show the relationship. 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.
- xchip 10y agoLOL if there's ever a zoolander movie for signal processing I want that line in.
- ArkyBeagle 10y agoI've heard that called an exponential moving average: https://en.wikipedia.org/wiki/Moving_average#Exponential_moving_average https://en.wikipedia.org/wiki/Moving_average#Exponential_mov...
- skoelden 10y agoDespite some saying this is not a Kalman filter I'd argue that it is, although a quite simple stationary Kalman filter. I'm not saying calling it a low-pass filter or IIR is wrong, but that there is not a clear distinction in this case. Hear me out: 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.
- Bromskloss 10y agoEven a constant could be a Kálmán filter. ;-)