The `ExponentialFilter`

class implements a simple linear recursive exponential filter for the Arduino. It provides a simple way to smooth noisy measurements from analog sensors without using as much memory as a moving average filter.

Every time you provide a new value (x_{n}), the exponential filter updates a smoothed value (y_{n}):

y_{n} = *w* × x_{n} + (1 – *w*) × y_{n – 1}

Here:

- y
_{n}is the output of the filter at a moment in time*n* - x
_{n}is the new input value at a moment in time*n* - y
_{n – 1}is the previous output value of the filter *w*is the weighting factor in the range [0, 100]. High values of*w*(90, for example) favor new data over old data. The output responds quickly to changes in the input but is not smoothed much. Low values of*w*(10, for example) favor old data over new data. The filter output is heavily smoothed and responds slowly to changes (noisy or not) in the input .

## Functions

The `ExponentialFilter`

is a template class that takes a single parameter: the type of measurement to filter. The current filtered value is multiplied by 10 internally to improve precision of the filter. Normally use either `long`

(to save code space if you haven’t already used float’s) or `float`

(for more accurate results) as the filter parameter.

The constructor takes two arguments:

- The initial filter weight. This should be between 0 and 100
- The initial value. Typically 0.

Declare a new exponential filter using:

1 2 3 | #include "Filter.h" // Create a new exponential filter with a weight of 5 and an initial value of 0. ExponentialFilter<long> ADCFilter(5, 0); |

### Filter(NewValue)

Applies the filter to a new value. See example below.

### SetWeight(NewWeight)

Changes the weight applied when filtering. This value should be between 0 and 100.

High weights (90, for example) favor new data over old data. So, the output responds quickly to changes in the input and is not smoothed much.

Low values of (10, for example) favor old data over new data. So, the output is heavily smoothed and the filter responds slowly to changes (noisy or not) in the input.

### GetWeight()

Returns the current value of the filter weight parameter.

### Current()

Returns the current value of the filter output.

### SetCurrent(NewValue)

Forces the current value to the new value supplied. Typically used to initialize the filter from a measurement.

## Example

This example reads the channel-0 ADC on an Arduino. Both the raw measurement and a heavily filtered value are sent to MegunoLink for plotting.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include "MegunoLink.h" #include "Filter.h" // Create a new exponential filter with a weight of 5 and an initial value of 0. ExponentialFilter<long> ADCFilter(5, 0); void setup() { Serial.begin(9600); Serial.println("Exponential Filter demo"); Serial.println("-----------------------"); } void loop() { int RawValue = analogRead(0); ADCFilter.Filter(RawValue); TimePlot Plot; Plot.SendData("Raw", RawValue); Plot.SendData("Filtered", ADCFilter.Current()); delay(100); } |

The noisy output from the analog-to-digital converter (green series) is heavily filtered here (red series):

## Availability

The exponential filter is included in our MegunoLink Arduino Library and can be installed using the Arduino integration tool.

[…] because you don’t need to make many measurements at once. For this solution, they developed an Arduino filter library so you don’t need to go mad with […]

Cool! Where can I get it?

Nevermind, found it! 😀

How do I use this with two separate analog inputs without affecting each other?

Sorry if this is a stupid question but I have little OOP knowledge.

Hi Derek,

You can just create separate filter variables. For example:

Hi!

Can I somehow set with this filter the cutoff frequency? I would like to filter my input signal with a band-pass filter between 20-500 Hz. So e.g. I would use a lowpass filter at 500 Hz, and a highpass at 20 Hz.

Could you help me with this?

Hi, no not with our filter library. Our library just uses a simple exponential filter. If you want to be able to specify those kind of details you probably need a more complicated filter. This could be useful. http://www.schwietering.com/jayduino/filtuino/ and this https://github.com/MartinBloedorn/libFilter