Simple Moving Average

Pieter P

Difference equation

The difference equation of the Simple Moving Average filter is derived from the mathematical definition of the average of values: the sum of the values divided by the number of values. In this equation, is the current output, is the current input, is the previous input, etc.
is the length of the average.

Impulse and step response

From the difference equation, we can now easily derive the impulse and step response.

The impulse response is the output of the filter when a Kronecker delta function is applied to the input. For example, if , 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. For example, if , the step response is as follows:

Transfer function

Any discrete signal can be written as an infinite sum of scaled Kronecker deltas. You can easily see that all terms where are zero, because the Kronecker delta is zero in that case. Only the term for is non-zero, in which case the Kronecker delta is one, so the result is just .
We'll use this property in the following proof.

You can express the output of discrete-time linear time-invariant systems, of which the SMA is an example, as the convolution of the input with the impulse response.

Proof

If is the transformation performed by the DTLTI system, and by using the fact that is a linear transformation and that it is time-invariant: The symbol in the last step is called the convolution operator, and it is defined as the sum in the step before it.

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: If , and are the Z-transforms of the input, output and impulse response respectively, then: is called the transfer function of the system.
Changing the boundaries of the second summation is valid, because if , then is zero by definition of unilateral signals.

Let's calculate the transfer function of the SMA.
We can use one of two approaches: use the difference equation and use some of the properties of the Z-transform to calculate , or apply the definition of the Z-transform to the impulse response directly.

Using the difference equation

All you have to do is apply the time shifting property of the Z transform: Then just use the fact that the Z-transform is linear, and the derivation is trivial: In the last steps, we just rearrange some stuff to get only positive powers of in both the numerator and the denominator.

Using the impulse response

For this derivation, we can use the fact that the impulse response is only non-zero for the first N terms. It is clear that this expression is identical to the previous one.

We can solve the summation in a different way using the formula for the sum of a geometric series: The transfer function then becomes:

In these expressions, is a complex variable, and is a complex function.
There are a couple of interesting values for : values that result in the transfer function becoming zero, called zeros, and values that result in the transfer function becoming undefined, called poles.
They can be found as the roots of the numerator and the denominator respectively, after eliminating any common factors.

The roots of the numerator are the solutions of or . Note that this equation has solutions, namely all -th roots of unity. One of the solutions is , but there are complex solutions as well, evenly spaced along the unit circle: .

The roots of the denominator are the solutions of . This has just one solution, .
Since both the numerator and the denominator have this root in common, we have to factor out this common factor.

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 SMA as before, with . The zeros are indicated by an O, and the poles by an X (as discussed before, the SMA doesn't have any poles).

Frequency response

An important property of discrete-time linear time-invariant systems is that it preserves the frequency of sinusoidal signals, only the phase shift and the amplitude are altered.
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: Note that this is just a special case of the Z-transform, where .

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 to the spectrum of the input signal . We already had an expression for the spectrum of the output divided by the spectrum of the input, we called it the transfer function . To get the frequency response of the filter, we can just evaluate the transfer function for . Also note that this is the DTFT of the impulse response .
We use the formula . We can now calculate the amplitude of each frequency component in the output by taking the modulus of . For reasons that will become apparent in a minute, we'll calculate the modulus squared. is the normalized pulsatance (angular frequency) in radians per sample. You can substitute it with where is the frequency in Hertz, and 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. Note that when a frequency is not present in the output signal, the gain will be . If a frequency has an amplitude of one in the output signal, the gain will be .

We can also plot the phase shift introduced by the SMA. This is just the complex argument of the transfer function.

The image below shows the bode plot of an SMA with a length of , and a sample frequency of . The frequency axis is limited between and the Nyquist frequency . This corresponds to a normalized pulsatance of . You can clearly see the low-pass behavior of the SMA: low frequencies have a near-unit gain, and high frequencies are attenuated.
Also note the zeros, where the magnitude plot goes to . In a zero, the transfer function changes sign, so the phase jumps as well.

To get a better understanding of where this curve comes from, we can plot the entire surface in the Z-domain. As mentioned above, the DTFT is just a special case of the Z-transform, where , i.e. the unit circle in the complex plane. Remember that the point is a point with a distance of to the origin, and with an angle of between its position vector and the positive x axis. The image of the unit circle is shown in red. Notice that this is the same curve as the blue curve in the magnitude response graph above: close to 0 when (the left half of the circle) and negative when (the right half of the circle). Note that the positive x axis points to the left hand side (y points out of the screen, and z points up).

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 -point, because .
To find it, just solve the following equation: This equation doesn't have a general analytical solution, but we can use numerical methods to determine the cut-off frequency. Note that the intersections at zero and are not valid solutions. In these points the transfer function is undefined, because it evaluates to . We only care about the first solution for .
A possible approach to find the intersection is to use the Newton-Raphson method.
A good starting point seems to be at half the period of the high-frequency sine wave.
We don't have to keep the squares, because both sines will be positive in the region of the intersection. A Python implementation can be found in the get_sma_cutoff function in the snippet below.

Plotting the frequency response, impulse response and step response in Python

We can use the SciPy and Matplotlib modules to plot the frequency response in Python.
This is the script that was used to generate the plots in the previous paragraphs.

from scipy.optimize import newton
from scipy.signal import freqz, dimpulse, dstep
from math import sin, cos, sqrt, pi
import numpy as np
import matplotlib.pyplot as plt

# Function for calculating the cut-off frequency of a moving average filter
def get_sma_cutoff(N, **kwargs):
    func = lambda w: sin(N*w/2) - N/sqrt(2) * sin(w/2)  # |H(e^jω)| = √2/2
    deriv = lambda w: cos(N*w/2) * N/2 - N/sqrt(2) * cos(w/2) / 2  # dfunc/dx
    omega_0 = pi/N  # Starting condition: halfway the first period of sin(Nω/2)
    return newton(func, omega_0, deriv, **kwargs)

# Simple moving average design parameters
f_s = 100
N = 5

# Find the cut-off frequency of the SMA
w_c = get_sma_cutoff(N)
f_c = w_c * f_s / (2 * pi)

# SMA coefficients
b = np.ones(N)
a = np.array([N] + [0]*(N-1))

# Calculate the frequency response
w, h = freqz(b, a, worN=4096)
w *= f_s / (2 * pi)                      # Convert from rad/sample to Hz

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

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

# Plot the impulse response
t, y = dimpulse((b, a, 1/f_s), n=2*N)
plt.suptitle('Impulse Response')
_, _, baseline = plt.stem(t, y[0], basefmt='k:')
plt.setp(baseline, 'linewidth', 1)
baseline.set_xdata([0,1])
baseline.set_transform(plt.gca().get_yaxis_transform())
plt.xlabel('Time [seconds]')
plt.ylabel('Output')
plt.xlim(-1/f_s, 2*N/f_s)
plt.yticks([0, 0.5/N, 1.0/N])
plt.show()

# Plot the step response
t, y = dstep((b, a, 1/f_s), n=2*N)
plt.suptitle('Step Response')
_, _, baseline = plt.stem(t, y[0], basefmt='k:')
plt.setp(baseline, 'linewidth', 1)
baseline.set_xdata([0,1])
baseline.set_transform(plt.gca().get_yaxis_transform())
plt.xlabel('Time [seconds]')
plt.ylabel('Output')
plt.xlim(-1/f_s, 2*N/f_s)
plt.yticks([0, 0.2, 0.4, 0.6, 0.8, 1])
plt.show()

Comparing the Simple Moving Average filter to the Exponential Moving Average filter

Using the same Python functions as before, we can plot the responses of the EMA and the SMA on top of each other.
First, the length of the SMA is chosen, then its cut-off frequency is calculated, and this frequency is then used to design the EMA. Do note that this is a fairly arbitrary decision.
from scipy.optimize import newton
from scipy.signal import freqz, dimpulse, dstep
from math import sin, cos, sqrt, pi
import numpy as np
import matplotlib.pyplot as plt

# Function for calculating the cut-off frequency of a moving average filter
def get_sma_cutoff(N, **kwargs):
    func = lambda w: sin(N*w/2) - N/sqrt(2) * sin(w/2)  # |H(e^jω)| = √2/2
    deriv = lambda w: cos(N*w/2) * N/2 - N/sqrt(2) * cos(w/2) / 2  # dfunc/dx
    omega_0 = pi/N  # Starting condition: halfway the first period of sin(Nω/2)
    return newton(func, omega_0, deriv, **kwargs)

# Simple moving average design parameters
f_s = 360
N = 9

# Find the cut-off frequency of the SMA
w_c = get_sma_cutoff(N)
f_c = w_c * f_s / (2 * pi)

# Calculate the pole location of the EMA with the same cut-off frequency
b = 2 - 2*cos(w_c)
alpha = (-b + sqrt(b**2 + 4*b)) / 2

# SMA & EMA coefficients
b_sma = np.ones(N)
a_sma = np.array([N] + [0]*(N-1))

b_ema = np.array((alpha, 0))
a_ema = np.array((1, alpha - 1))

# Calculate the frequency response
w, h_sma = freqz(b_sma, a_sma, worN=4096)
w, h_ema = freqz(b_ema, a_ema, w)
w *= f_s / (2 * pi)                      # Convert from rad/sample to Hz

# Plot the amplitude response
plt.subplot(2, 1, 1)
plt.suptitle('Bode Plot')
plt.plot(w, 20 * np.log10(abs(h_sma)),   # Convert modulus to dB
         color='blue', label='SMA')
plt.plot(w, 20 * np.log10(abs(h_ema)),
         color='green', label='EMA')
plt.ylabel('Magnitude [dB]')
plt.xlim(0, f_s / 2)
plt.ylim(-60, 10)
plt.axvline(f_c, color='red')
plt.axhline(-3.01, linewidth=0.8, color='black', linestyle=':')
plt.legend()

# Plot the phase response
plt.subplot(2, 1, 2)
plt.plot(w, 180 * np.angle(h_sma) / pi,  # Convert argument to degrees
         color='blue')
plt.plot(w, 180 * np.angle(h_ema) / pi,
         color='green')
plt.xlabel('Frequency [Hz]')
plt.ylabel('Phase [°]')
plt.xlim(0, f_s / 2)
plt.ylim(-180, 90)
plt.yticks([-180, -135, -90, -45, 0, 45, 90])
plt.axvline(f_c, color='red')
plt.show()

# Plot the impulse response
t, y_sma = dimpulse((b_sma, a_sma, 1/f_s), n=2*N)
t, y_ema = dimpulse((b_ema, a_ema, 1/f_s), n=2*N)
plt.suptitle('Impulse Response')
plt.plot(t, y_sma[0], 'o-',
         color='blue', label='SMA')
plt.plot(t, y_ema[0], 'o-',
         color='green', label='EMA')
plt.xlabel('Time [seconds]')
plt.ylabel('Output')
plt.xlim(-1/f_s, 2*N/f_s)
plt.legend()
plt.show()

# Plot the step response
t, y_sma = dstep((b_sma, a_sma, 1/f_s), n=2*N)
t, y_ema = dstep((b_ema, a_ema, 1/f_s), n=2*N)
plt.suptitle('Step Response')
plt.plot(t, y_sma[0], 'o-',
         color='blue', label='SMA')
plt.plot(t, y_ema[0], 'o-',
         color='green', label='EMA')
plt.xlabel('Time [seconds]')
plt.ylabel('Output')
plt.xlim(-1/f_s, 2*N/f_s)
plt.legend()
plt.show()