# Double Exponential Moving Average Filter – Speeding up the EMA

Sometimes you have a noisy signal which you want to low-pass filter to a less noisy signal. The Exponential Moving Average (EMA) filter is a nice and easy filter to implement on your embedded system. Read more about the EMA filter here.

However, sometimes the EMA can be too slow. The trade-off between noise suppression and speed might simply not be good enough. Either the filtered signal doesn’t move fast enough or the signal is too noisy. This is not a very good comparison image between EMA and DEMA. We just wanted a cool image right here. Read further down for a proper comparison!

## DEMA

…the DEMA is underdamped while the EMA is overdamped.

One solution to this might be the Double Exponential Moving Average (DEMA) filter. At a given α the DEMA has faster response than the EMA while still maintaining the same noise suppression. One potential drawback with the DEMA filter is that you might get an overshoot. In other words the DEMA is underdamped while the EMA is overdamped. Look at this post to find out what we mean by this.

#### Algorithm

The algorithm is pretty straight forward: The most tricky thing here is the last term where you take the EMA of the EMA. To achieve this we have to have two global variables instead of one.

#### Implementation

Here’s a quick implementation of the DEMA filter on Arduino.

```float EMA_function(float alpha, int latest, int stored);

int sensor_pin = 0;

float ema_a = 0.06;
int ema_ema = 0;
int ema = 0;

void setup() {
}

void loop() {

ema = EMA_function(ema_a, sensor_value, ema);
ema_ema = EMA_function(ema_a, ema, ema_ema);

int DEMA = 2*ema - ema_ema;

delay(1);
}

float EMA_function(float alpha, int latest, int stored){
return round(alpha*latest) + round((1-alpha)*stored);
}
```

Notice that we use two global variables for the filtering instead of one.