Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Exponential Moving Average

Pieter P

Difference equation

The difference equation of an exponential moving average filter is very simple:

y[n] = αx[n] + (1 − α)y[n − 1]

In this equation, y[n] is the current output, y[n − 1] is the previous output, and x[n] is the current input; α is a number between 0
and 1. If α = 1, the output is just equal to the input, and no filtering takes place.

The filter is called 'exponential', because the weighting factor of previous inputs decreases exponentially. This can be easily
demonstrated by substituting the previous outputs:

y[n] = αx[n] + (1 − α)y[n − 1]

= αx[n] + (1 − α) (αx[n − 1] + (1 − α)y[n − 2])

= αx[n] + (1 − α) (αx[n − 1] + (1 − α) (αx[n − 2] + (1 − α)y[n − 3]))


n
k
= α ∑ (1 − α) x[n − k]

k=0

Impulse and step response

From the previous equation, we can now easily calculate the impulse and step response.

The impulse response is the output of the filter when a Kronecker delta function is applied to the input.
Recall the definition of the Kronecker delta:

1 n = 0
δ[n] = {
0 n ≠ 0.

The impulse response of the EMA is

y impulse [n] = h[n]


n
k
= α ∑ (1 − α) δ[n − k]

k=0
n
= α(1 − α) .

For example, if α = 0.25 , the impulse response is as follows:

1
The step response is the output of the filter when a Heaviside step function is applied to the input.
The Heaviside step function is defined as

0 n < 0
H [n] = {
1 n ≥ 0.

The step response of the EMA is


n
k
y step [n] = α ∑ (1 − α) H [n − k]

k=0

n
k
= α ∑ (1 − α)

k=0

n+1
= 1 − (1 − α) .

For example, if α = 0.25 , the step response is as follows:

2
Transfer function

The output of discrete-time linear time-invariant (DTLTI) systems, of which the EMA is an example, can be expressed as the
convolution of the input with the impulse response. In other words, the impulse response describes the behavior of the system, for
all possible inputs.

To prove the previous statement, we'll start with the following trivial property: any signal x[n] can be expressed as a convolution of
the Kronecker delta function with itself, that is

+∞

x[n] = x[n] ∗ δ[n] = ∑ x[k] δ[n − k].

k=0

You can easily see that all terms where k ≠ n are zero, because the Kronecker delta is zero in that case. Only the term for k = n is
non-zero, in which case the Kronecker delta is one, so the result is just x[n].
You can also interpret this as the signal being made up of a sum of infinitely many scaled and shifted Kronecker delta functions.

Let T be the transformation performed by the DTLTI system, then y[n] is the output after applying T to the input signal x[n]. The
following derivation makes use of the fact that T is a linear transformation and that it is time-invariant:

y[n] = T (x[n])

= T (∑ x[k] δ[k − n])

k=0

= ∑ T (x[k] δ[k − n])

k=0

= ∑ x[k] T (δ[k − n])

k=0

= ∑ x[k] h[k − n]

k=0

= x[n] ∗ h[n]

Since the factor x[k] is independent of time n, it can be moved outside of the T operator. T applied to the Kronecker delta is (by
definition) the impulse response of T , h[n], but shifted by k time steps.

Analysis of such systems is usually easier in the Z-domain, in which the convolution is reduced to a simple product.
The (unilateral) Z-transform is defined as:

−n
Z{x[n]} = ∑ x[n]z

n=0

If X(z) = Z{x[n]}, Y (z) = Z{y[n]} and H (z) = Z{h[n]} are the Z-transforms of the input, output and impulse response
respectively, then:

Z{y[n]} = Z{x[n] ∗ h[n]}


−n
Y (z) = ∑ x[n] ∗ h[n]z

n=0

∞ ∞

−n
= ∑ ∑ x[k]h[n − k]z

n=0 k=0

∞ ∞
−n+k−k
= ∑ ∑ x[k]h[n − k]z

k=0 n=0

∞ ∞

−k −(n−k)
= ∑ x[k]z ∑ h[n − k]z

k=0 n=0

Y (z) = X(z)H (z)

Y (z)
H (z) =
X(z)

H (z) is called the transfer function of the system.

3
Let's calculate the transfer function of the EMA.
Y (z)
We can use one of two approaches: use the difference equation and use some of the properties of the Z-transform to calculate X(z)
,
or apply the definition of the Z-transform to the impulse response h[n] directly.

Using the difference equation

All you have to do is apply the time shifting property of the Z transform:
−n 0
∀n 0 ∈ N : Z{y[n − n 0 ]} = z Z{y[n]}

Then just rearrange the terms.

y[n] = αx[n] + (1 − α)y[n − 1]

Z{y[n]} = Z{αx[n] + (1 − α)y[n − 1]}

= αZ{x[n]} + (1 − α)Z{y[n − 1]}


−1
= αZ{x[n]} + (1 − α)z Z{y[n]}

−1
Y (z) = αX(z) + (1 − α)z Y (z)
−1
Y (z) − (1 − α)z Y (z) = αX(z)

Y (z) α
=
−1
X(z) 1 − (1 − α)z
α
H (z) =
−1
1 − (1 − α)z

Using the impulse response

H (z) = Z{h[n]}

−n
= ∑ h[n]z

n=0


n −n
= α ∑(1 − α) z

n=0

∞ n
1 − α
= α∑( )
z
n=0

α
=
1−α
1 −
z

The last step is only valid if the sum converges, this is the case for | 1−α

z
| < 1 . (See Infinite Geometric Series)

Poles and zeros

In these expressions, z is a complex variable, and H (z) is a complex function.


There are a couple of interesting values for z: values that result in the numerator becoming zero, called zeros, and values that result
in the denominator becoming zero, called poles.
α
H (z) =
1−α
1 −
z

αz
=
z − (1 − α)

By rewriting the transfer function, we can easily see that z = 0 is a zero, and z = 1 − α is a pole.

The poles and zeros determine the overall effect of the transfer function, so pole-zero plots are a very useful tool when describing
filters.
This is the pole-zero plot of the same example EMA as before, with α = 0.25.

4
The zero in the origin is indicated by an O, and the pole at 1 − α = 0.75 by an X.

Frequency response

An important property of discrete-time linear time-invariant systems is that it preserves the pulsatance (angular frequency) of
sinusoidal signals, only the phase shift and the amplitude are altered. In other words, sines and cosines are eigenfunctions of DTLTI
systems.
This makes it relatively easy to express the frequency response (sometimes called magnitude response) of a filter.

We're interested in the spectrum of frequencies that a signal contains, so it makes sense to decompose it as a sum of sines and
cosines. That's exactly what the discrete-time Fourier transform does:

−iωn
X 2π (ω) = ∑ x[n]e

n=−∞

Note that this is just a special case of the Z-transform, where z = e



.

The frequency response of the filter describes how the spectrum of a signal is altered when it passes through the filter. It relates the
spectrum of the output signal Y (ω) to the spectrum of the input signal X(ω). We already had an expression for the spectrum of the
output divided by the spectrum of the input, we called it the transfer function H (z) = Y (z)/X(z). To get the frequency response
of the filter, we can just evaluate the transfer function for z = e . Also note that this is the DTFT of the impulse response h[k].

−iωn
F DT F T {h[k]} = ∑ h[n]e

n=−∞


= H (e )
α
=
−iω
1 − (1 − α)e

5
We can now calculate the amplitude of each frequency component in the output by taking the modulus of H (e ). For reasons that iω

will become apparent in a minute, we'll calculate the modulus squared. We use Euler's formula for writing the exponential as a sines
and cosines.

2
α
iω 2
|H (e )| =
−iω 2
|1 − (1 − α)e |
2
α
=
2
|1 − (1 − α) (cos(−ω) + i sin(−ω))|
2
α
=
2 2
(1 − (1 − α) cos(ω)) + ((1 − α) sin(ω))
2
α
=
2 2 2 2
1 − 2(1 − α) cos(ω) + (1 − α) cos (ω) + (1 − α) sin (ω)

2
α
=
2
1 − 2(1 − α) cos(ω) + (1 − α)

2πf
ω is the normalized pulsatance in radians per sample. You can substitute it with ω =
fs
where f is the frequency in Hertz, and f s

is the sample frequency of the system in Hertz.

We can now plot the filter's gain in function of the frequency. These plots often use a logarithmic scale, to show the gain in decibels.
In order to calculate the power gain, the amplitude is squared.
iω 2
A dB (ω) = 10 log 10 |H (e )|

Note that when a frequency is not present in the output signal, the gain will be −∞ dB. If a frequency has an amplitude of one in
the output signal, the gain will be 0 dB.

You can clearly see the low-pass behavior of the EMA: low frequencies have a near-unit gain, and high frequencies are attenuated.

To get a better understanding of where this curve comes from, we can plot the entire |H (z)| surface in the Z-domain. As mentioned
above, the DTFT is just a special case of the Z-transform, where z = e , i.e. the unit circle in the complex plane. Remember that

the point z = e is a point with a distance of 1 to the origin, and with an angle of ω rad between its position vector and the

positive x axis.

6
The image of the unit circle is shown in blue. Notice that this is the same curve as the blue curve in the magnitude response graph
above: close to 0 when ω → 0 (the right half of the circle) and negative when ω → π (the left half of the circle).
You can clearly see the effect of the pole at 0.75 + 0i. The zero in the origin has no effect on the frequency response, because it
doesn't alter the image of the unit circle, since |e | = 1 or log(|e |) = 0.
iω iω

7
Cutoff frequency

The cutoff frequency is defined as the frequency of the half-power point, where the power gain is a half. It's often called the −3 dB
1
-point, because 10 log ( ) ≈ −3.01 dB.
10 2

To find it, just solve the following equation:

1
iω c 2
|H (e )| =
2

2
α 1
=
2
1 − 2(1 − α) cos(ω c ) + (1 − α) 2

2
α + 2α − 2
ω c = arccos ( )
2α − 2

For example, if α = 0.25 , then ω c = arccos (


23

24
) ≈ 0.2897
rad

sample
.
fs
To convert it to a frequency in Hertz, you can multiply ω by 2π
, with f the sample frequency.
s

samples fs
For example, if the sample frequency is f s = 1000
s
, then f = arccos (
23
24
)

.
≈ 46.12 H z

Plotting the frequency response in Python

We can use the SciPy and Matplotlib modules to plot the frequency response in Python.

The SciPy freqz function expects the transfer function coefficients in the form
−1 −2 −p
b0 + b1 z + b2 z + ⋯ + bp z
H (z) = .
−1 −2 −q
a0 + a1 z + a2 z + ⋯ + aq z

This is the reverse of the usual ordering of polynomial coefficients.

In the case of the exponential moving average filter, the transfer function is
α
H (z) =
−1
1 + (α − 1)z ,

so b 0 ,
= α a0 = 1 and a 1 = α − 1 .

1 from scipy.signal import freqz


2 import matplotlib.pyplot as plt
3 from math import pi, acos
4 import numpy as np
5
6 alpha = 0.25
7
8 b = np.array(alpha)
9 a = np.array((1, alpha - 1))
10
11 print("b =", b) # Print the coefficients
12 print("a =", a)
13
14 x = (alpha**2 + 2*alpha - 2) / (2*alpha - 2)
15 w_c = acos(x) # Calculate the cut-off frequency
16
17 w, h = freqz(b, a) # Calculate the frequency response
18
19 plt.subplot(2, 1, 1) # Plot the amplitude response
20 plt.suptitle('Bode Plot')
21 plt.plot(w, 20 * np.log10(abs(h))) # Convert to dB
22 plt.ylabel('Magnitude [dB]')
23 plt.xlim(0, pi)
24 plt.ylim(-18, 1)
25 plt.axvline(w_c, color='red')
26 plt.axhline(-3, linewidth=0.8, color='black', linestyle=':')
27
28 plt.subplot(2, 1, 2) # Plot the phase response
29 plt.plot(w, 180 * np.angle(h) / pi) # Convert argument to degrees
30 plt.xlabel('Frequency [rad/sample]')
31 plt.ylabel('Phase [°]')
32 plt.xlim(0, pi)
33 plt.ylim(-90, 90)
34 plt.yticks([-90, -45, 0, 45, 90])
35 plt.axvline(w_c, color='red')
36 plt.show()

8
9

You might also like