Exponential Moving Average (EMA) Filters
Overview
The exponential moving average (EMA) filter is a discrete, low-pass, infinite-impulse response (IIR) filter. It places more weight on recent data by discounting old data in an exponential fashion, and behaves similarly to the discrete first-order low-pass RC filter.
Unlike a simple moving average (SMA) filter, most EMA filters is not windowed, and the next value depends on all previous inputs. Thus most EMA filters are a form of infinite impulse response (IIR) filter, whilst a SMA is a finite impulse response (FIR) filter. There are exceptions, and you can indeed build a windowed exponential moving average filter in where the coefficients are weighted exponentially.
The EMA filter is my personal go-to filter if I ever want to do some basic filtering on a microcontroller. It's fast, memory efficient, and easy to implement.
EMA Equation
The difference equation for an exponential moving average filter is:
where:
is the output ( denotes the sample number)
is the input
is a constant which sets the cutoff frequency (a value between and )
Notice that the calculation does not require the storage of past values of and only the previous value of , which makes this filter memory and computation friendly (especially relevant for microcontrollers). Only one addition, one subtraction, and two multiplication operations are needed.
The constant determines how aggressive the filter is. It can vary between and (inclusive). As , the filter gets more and more aggressive, until at , where the input has no effect on the output (if the filter started like this, then the output would stay at ). As , the filter lets more of the raw input through at less filtered data, until at , where the filter is not "filtering" at all (pass-through from input to output).
The filter is called exponential because the weighted contribution of previous inputs decreases exponentially the further the input is away in time. This can be seen in the difference equation if we substitute in previous inputs: