Table of Contents list

Difference equation

The difference equation of an exponential moving average filter is very simple: y[n]=αx[n]+(1α)y[n1] In this equation, y[n] is the current output, y[n1] 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[n1]=αx[n]+(1α)(αx[n1]+(1α)y[n2])=αx[n]+(1α)(αx[n1]+(1α)(αx[n2]+(1α)y[n3]))=αk=0n(1α)kx[nk]

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: δ[n]={1n=00n0. The impulse response of the EMA is yimpulse[n]=h[n]=αk=0n(1α)kδ[nk]=α(1α)n. For example, if α=0.25, the impulse response is as follows:

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 H[n]={0n<01n0. The step response of the EMA is ystep[n]=αk=0n(1α)kH[nk]=αk=0n(1α)k=1(1α)n+1. For example, if α=0.25, the step response is as follows:

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]=k=0+x[k] δ[nk]. You can easily see that all terms where kn 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(k=0x[k] δ[kn])=k=0T(x[k] δ[kn])=k=0x[k] T(δ[kn])=k=0x[k] h[kn]=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: Z{x[n]}=n=0x[n]zn 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]}Y(z)=n=0x[n]h[n]zn=n=0k=0x[k]h[nk]zn=k=0n=0x[k]h[nk]zn+kk=k=0x[k]zkn=0h[nk]z(nk)Y(z)=X(z)H(z)H(z)=Y(z)X(z) H(z) is called the transfer function of the system.

Let's calculate the transfer function of the EMA.
We can use one of two approaches: use the difference equation and use some of the properties of the Z-transform to calculate Y(z)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: n0N:Z{y[nn0]}=zn0Z{y[n]} Then just rearrange the terms. y[n]=αx[n]+(1α)y[n1]Z{y[n]}=Z{αx[n]+(1α)y[n1]}=αZ{x[n]}+(1α)Z{y[n1]}=αZ{x[n]}+(1α)z1Z{y[n]}Y(z)=αX(z)+(1α)z1Y(z)Y(z)(1α)z1Y(z)=αX(z)Y(z)X(z)=α1(1α)z1H(z)=α1(1α)z1

Using the impulse response

H(z)=Z{h[n]}=n=0h[n]zn=αn=0(1α)nzn=αn=0(1αz)n=α11α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)=α11αz=αzz(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. 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: X2π(ω)=n=x[n]eiωn Note that this is just a special case of the Z-transform, where z=eiω.

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=eiω. Also note that this is the DTFT of the impulse response h[k]. FDTFT{h[k]}=n=h[n]eiωn=H(eiω)=α1(1α)eiω We can now calculate the amplitude of each frequency component in the output by taking the modulus of H(eiω). For reasons that 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. |H(eiω)|2=α2|1(1α)eiω|2=α2|1(1α)(cos(ω)+isin(ω))|2=α2(1(1α)cos(ω))2+((1α)sin(ω))2=α212(1α)cos(ω)+(1α)2cos2(ω)+(1α)2sin2(ω)=α212(1α)cos(ω)+(1α)2 ω is the normalized pulsatance in radians per sample. You can substitute it with ω=2πffs where f is the frequency in Hertz, and fs 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. AdB(ω)=10 log10|H(eiω)|2 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=eiω, i.e. the unit circle in the complex plane. Remember that the point z=eiω 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. 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 |eiω|=1 or log(|eiω|)=0.

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-point, because 10log10(12)3.01 dB.
To find it, just solve the following equation: |H(eiωc)|2=12 α212(1α)cos(ωc)+(1α)2=12ωc=arccos(α2+2α22α2)

For example, if α =0.25, then ωc=arccos(2324)0.2897radsample.
To convert it to a frequency in Hertz, you can multiply ω by fs2π, with fs the sample frequency.
For example, if the sample frequency is fs=1000 sampless, then f=arccos(2324)fs2π46.12 Hz.

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 H(z)=b0+b1z1+b2z2++bpzpa0+a1z1+a2z2++aqzq. 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)z1, so b0=α, a0=1 and a1=α1.

from scipy.signal import freqz 
import matplotlib.pyplot as plt
from math import pi, acos
import numpy as np

alpha = 0.25

b = np.array(alpha)
a = np.array((1, alpha - 1))

print("b =", b)                        # Print the coefficients
print("a =", a)

x = (alpha**2 + 2*alpha - 2) / (2*alpha - 2)
w_c = acos(x)                          # Calculate the cut-off frequency

w, h = freqz(b, a)                     # Calculate the frequency response

plt.subplot(2, 1, 1)                   # Plot the amplitude response
plt.suptitle('Bode Plot')            
plt.plot(w, 20 * np.log10(abs(h)))     # Convert to dB
plt.ylabel('Magnitude [dB]')
plt.xlim(0, pi)
plt.ylim(-18, 1)
plt.axvline(w_c, color='red')
plt.axhline(-3, linewidth=0.8, color='black', linestyle=':')

plt.subplot(2, 1, 2)                   # Plot the phase response
plt.plot(w, 180 * np.angle(h) / pi)    # Convert argument to degrees
plt.xlabel('Frequency [rad/sample]')
plt.ylabel('Phase [°]')
plt.xlim(0, pi)
plt.ylim(-90, 90)
plt.yticks([-90, -45, 0, 45, 90])
plt.axvline(w_c, color='red')
plt.show()