In a perfect world, a sensor’s output would exactly match the input it is sensing. In the real world, there are a number of errors introduced, including bias, time lags in response, and noise. This article discusses one of the simplest filters for reducing noise: the Exponentially Weighted Moving Average (EWMA) filter. Despite its long name, it’s about the simplest and most efficient filter you can implement. The name comes from the concept that the filter’s output, y, is the weighted average of the current input and each previous inputs, with the weighting decreasing exponentially:

Y_{t} = α (X_{t }+ (1 – α) X_{t-1 }+ (1-α)^{2 }X_{t-2 }+ (1-α)^{3 }X_{t-3 + ….}

where Y_{t }is the current output from the filter, and X_{t }is the current input, and the other X’s are each preceding value. The weighting is shown in the figure:

(graph courtesy of Kevin Ryde, licensed under the Creative Commons Attribution-Share Alike 3.0 Unported license.)

This averaging tends to smooth out the response and reduce noise. Of course, this formula would be cumbersome and time consuming to implement in an embedded system. Even if only a small number of past values were used. Some math can show that the formula can be rewritten as a recursive formula using the past *output* from the filter:

Y_{t} = α Y_{t-1 }+ (1 – α) X_{t }for all values of t > 0, or, re-arranging terms:

Y_{t} = X_{t }+ α ( Y_{t-1 }– X_{t})

This means that all you need to store is the immediate past output of the filter, which already has the weighted average of all previous values calculated in. This is simpler and takes less storage than, for example, averaging the current and past 4 values of the sensor. It will also respond more quickly to actual changes that you are trying to measure.

## Limits

You can choose a value for α ranging between 0 and 1. At 0, the output is just the raw input: no filtering occurs. The output will respond instantly both to noise and to true changes in the system.

At 1, the output becomes equal to the past value: it never changes, eliminating all noise and all real signal changes! Clearly one wants to select a value in between these extremes.

## Selecting α

The correct value will depend on your situation. The higher the a, the more noise if filtered out, but also the more lag that is introduced in reacting to real system changes. This is shown in the following figures:

This figure shows the response of two filters to a unit step function. The red curve is for α = 0.3, and the green curve is forα = 0.6.

This graph shows the output of the filters to a noisy sensor, with Gaussian noise with a standard deviation of 0.1. One can see from the graph that a value of 0.3 reduces the noise somewhat, but leaves a lot in. A value of 0.6 greatly reduces the noise, at the expense of greater lag.

## Initializing the Filter

The formulas work for all values after the initial run of the filter. Like any recursive formula there needs to be a starting value. My rather rough and informal scanning of the literature identified three common methods:

- For the first pass, set the output equal to the input (same as no filtering)
- Take a small number of readings, e.g., 5, and use a straight average of these as the initial output. This is what I did for compass readings in my current robotic vehicle.
- For control processes, an alternative is to set the initial output at the desired control point (figuring, I suppose, that if the control system is working, the average should be at the control point).

## Summary

The simple EWMA routing can be summarized as:

For t= 0:

Y_{t} = X_{t}

Else:

Y_{t} = X_{t }+ α ( Y_{t-1 }– X_{t})

## Further Reading

There are many different types of filters, each with advantages and disadvantages. No one filter is best for all applications. I’m just a hobbyist, and don’t pretend to have much knowledge in this area, but there is a wealth of information out there.

- A basic introduction to various moving averages, you can start with the Wikipedia article: Moving Averages
- A good explanation of how an EWMA filter is mathematically identical to a simple RC low-pass filter can be found at this blog entry: Data Smoothing: RC Filtering and Exponential Averaging
- A good introduction to filters, including EWMA, is this Greg Stanley and Associates white paper.
- Finally, a complete book on digital signal processing, The Scientist and Engineer’s Guide to Digital Signal Processing, which can be viewed and downloaded for free.

As always, any feedback, additional comments, or corrections are welcomed.

Pingback: A Simple Low-Pass Filter (Concluded) | The Aspiring Roboticist

Hi Mike!

Thank you for your post!

I was looking for something similar, as we study these things in our degrees but have not practical examples for real life. I came across a little but very educational robot (http://letsmakerobots.com/node/39882) and I found the filtering function for the IR distance sensor is not very good.

With your explanations I can develop a filter for this use. Thanks!

Francisco