### Difference equation

The difference equation of an exponential moving average filter is very simple: $y\left[n\right]=\alpha x\left[n\right]+\left(1-\alpha \right)y\left[n-1\right]$ In this equation, $y\left[n\right]$ is the current output, $y\left[n-1\right]$ is the previous output, and $x\left[n\right]$ is the current input; $\alpha$ is a number between 0 and 1. If $\alpha =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: $\begin{array}{rl}y\left[n\right]& =\alpha x\left[n\right]+\left(1-\alpha \right)y\left[n-1\right]\\ & =\alpha x\left[n\right]+\left(1-\alpha \right)\left(\alpha x\left[n-1\right]+\left(1-\alpha \right)y\left[n-2\right]\right)\\ & =\alpha x\left[n\right]+\left(1-\alpha \right)\left(\alpha x\left[n-1\right]+\left(1-\alpha \right)\left(\alpha x\left[n-2\right]+\left(1-\alpha \right)y\left[n-3\right]\right)\right)\\ & \dots \\ & =\alpha \sum _{k=0}^{n}{\left(1-\alpha \right)}^{k}x\left[n-k\right]\end{array}$

### 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: $\delta \left[n\right]=\left\{\begin{array}{ll}1& n=0\\ 0& n\ne 0.\end{array}$ The impulse response of the EMA is $\begin{array}{rl}{y}_{impulse}\left[n\right]& =h\left[n\right]\\ & =\alpha \sum _{k=0}^{n}{\left(1-\alpha \right)}^{k}\delta \left[n-k\right]\\ & =\alpha \left(1-\alpha {\right)}^{n}.\end{array}$ For example, if $\alpha =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\left[n\right]=\left\{\begin{array}{ll}0& n<0\\ 1& n\ge 0.\end{array}$ The step response of the EMA is $\begin{array}{rl}{y}_{step}\left[n\right]& =\alpha \sum _{k=0}^{n}{\left(1-\alpha \right)}^{k}H\left[n-k\right]\\ & =\alpha \sum _{k=0}^{n}{\left(1-\alpha \right)}^{k}\\ & =1-{\left(1-\alpha \right)}^{n+1}.\end{array}$ For example, if $\alpha =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\left[n\right]$ can be expressed as a convolution of the Kronecker delta function with itself, that is You can easily see that all terms where $k\ne 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\left[n\right]$.
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\left[n\right]$ is the output after applying $T$ to the input signal $x\left[n\right]$. The following derivation makes use of the fact that $T$ is a linear transformation and that it is time-invariant: Since the factor $x\left[k\right]$ 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\left[n\right]$, 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: $\mathcal{Z}\left\{x\left[n\right]\right\}=\sum _{n=0}^{\mathrm{\infty }}x\left[n\right]{z}^{-n}$ If $X\left(z\right)=\mathcal{Z}\left\{x\left[n\right]\right\}$, $Y\left(z\right)=\mathcal{Z}\left\{y\left[n\right]\right\}$ and $H\left(z\right)=\mathcal{Z}\left\{h\left[n\right]\right\}$ are the Z-transforms of the input, output and impulse response respectively, then: $\begin{array}{rl}\mathcal{Z}\left\{y\left[n\right]\right\}& =\mathcal{Z}\left\{x\left[n\right]\ast h\left[n\right]\right\}\\ Y\left(z\right)& =\sum _{n=0}^{\mathrm{\infty }}x\left[n\right]\ast h\left[n\right]{z}^{-n}\\ & =\sum _{n=0}^{\mathrm{\infty }}\sum _{k=0}^{\mathrm{\infty }}x\left[k\right]h\left[n-k\right]{z}^{-n}\\ & =\sum _{k=0}^{\mathrm{\infty }}\sum _{n=0}^{\mathrm{\infty }}x\left[k\right]h\left[n-k\right]{z}^{-n+k-k}\\ & =\sum _{k=0}^{\mathrm{\infty }}x\left[k\right]{z}^{-k}\sum _{n=0}^{\mathrm{\infty }}h\left[n-k\right]{z}^{-\left(n-k\right)}\\ Y\left(z\right)& =X\left(z\right)H\left(z\right)\\ H\left(z\right)& =\frac{Y\left(z\right)}{X\left(z\right)}\end{array}$ $H\left(z\right)$ 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 $\frac{Y\left(z\right)}{X\left(z\right)}$, or apply the definition of the Z-transform to the impulse response $h\left[n\right]$ directly.

#### Using the difference equation

All you have to do is apply the time shifting property of the Z transform: $\mathrm{\forall }{n}_{0}\in \mathbb{N}:\mathcal{Z}\left\{y\left[n-{n}_{0}\right]\right\}={z}^{-{n}_{0}}\mathcal{Z}\left\{y\left[n\right]\right\}$ Then just rearrange the terms. $\begin{array}{rl}y\left[n\right]& =\alpha x\left[n\right]+\left(1-\alpha \right)y\left[n-1\right]\\ \mathcal{Z}\left\{y\left[n\right]\right\}& =\mathcal{Z}\left\{\alpha x\left[n\right]+\left(1-\alpha \right)y\left[n-1\right]\right\}\\ & =\alpha \mathcal{Z}\left\{x\left[n\right]\right\}+\left(1-\alpha \right)\mathcal{Z}\left\{y\left[n-1\right]\right\}\\ & =\alpha \mathcal{Z}\left\{x\left[n\right]\right\}+\left(1-\alpha \right){z}^{-1}\mathcal{Z}\left\{y\left[n\right]\right\}\\ Y\left(z\right)& =\alpha X\left(z\right)+\left(1-\alpha \right){z}^{-1}Y\left(z\right)\\ Y\left(z\right)-\left(1-\alpha \right){z}^{-1}Y\left(z\right)& =\alpha X\left(z\right)\\ \frac{Y\left(z\right)}{X\left(z\right)}& =\frac{\alpha }{1-\left(1-\alpha \right){z}^{-1}}\\ H\left(z\right)& =\frac{\alpha }{1-\left(1-\alpha \right){z}^{-1}}\end{array}$

#### Using the impulse response

$\begin{array}{rl}H\left(z\right)& =\mathcal{Z}\left\{h\left[n\right]\right\}\\ & =\sum _{n=0}^{\mathrm{\infty }}h\left[n\right]{z}^{-n}\\ & =\alpha \sum _{n=0}^{\mathrm{\infty }}\left(1-\alpha {\right)}^{n}{z}^{-n}\\ & =\alpha \sum _{n=0}^{\mathrm{\infty }}{\left(\frac{1-\alpha }{z}\right)}^{n}\\ & =\frac{\alpha }{1-\frac{1-\alpha }{z}}\end{array}$ The last step is only valid if the sum converges, this is the case for . (See Infinite Geometric Series)

#### Poles and zeros

In these expressions, $z$ is a complex variable, and $H\left(z\right)$ 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. $\begin{array}{rl}H\left(z\right)& =\frac{\alpha }{1-\frac{1-\alpha }{z}}\\ & =\frac{\alpha z}{z-\left(1-\alpha \right)}\end{array}$ By rewriting the transfer function, we can easily see that $z=0$ is a zero, and $z=1-\alpha$ 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 $\alpha =0.25$. The zero in the origin is indicated by an O, and the pole at $1-\alpha =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: ${X}_{2\pi }\left(\omega \right)=\sum _{n=-\mathrm{\infty }}^{\mathrm{\infty }}x\left[n\right]{e}^{-i\omega n}$ Note that this is just a special case of the Z-transform, where $z={e}^{i\omega }$.

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\left(\omega \right)$ to the spectrum of the input signal $X\left(\omega \right)$. 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\left(z\right)=Y\left(z\right)/X\left(z\right)$. To get the frequency response of the filter, we can just evaluate the transfer function for $z={e}^{i\omega }$. Also note that this is the DTFT of the impulse response $h\left[k\right]$. $\begin{array}{rl}{\mathcal{F}}_{DTFT}\left\{h\left[k\right]\right\}& =\sum _{n=-\mathrm{\infty }}^{\mathrm{\infty }}h\left[n\right]{e}^{-i\omega n}\\ & =H\left({e}^{i\omega }\right)\\ & =\frac{\alpha }{1-\left(1-\alpha \right){e}^{-i\omega }}\end{array}$ We can now calculate the amplitude of each frequency component in the output by taking the modulus of $H\left({e}^{i\omega }\right)$. 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. $\begin{array}{rl}|H\left({e}^{i\omega }\right){|}^{2}& =\frac{{\alpha }^{2}}{|1-\left(1-\alpha \right){e}^{-i\omega }{|}^{2}}\\ & =\frac{{\alpha }^{2}}{|1-\left(1-\alpha \right)\left(\mathrm{cos}\left(-\omega \right)+i\mathrm{sin}\left(-\omega \right)\right){|}^{2}}\\ & =\frac{{\alpha }^{2}}{\left(1-\left(1-\alpha \right)\mathrm{cos}\left(\omega \right){\right)}^{2}+\left(\left(1-\alpha \right)\mathrm{sin}\left(\omega \right){\right)}^{2}}\\ & =\frac{{\alpha }^{2}}{1-2\left(1-\alpha \right)\mathrm{cos}\left(\omega \right)+\left(1-\alpha {\right)}^{2}{\mathrm{cos}}^{2}\left(\omega \right)+\left(1-\alpha {\right)}^{2}{\mathrm{sin}}^{2}\left(\omega \right)}\\ & =\frac{{\alpha }^{2}}{1-2\left(1-\alpha \right)\mathrm{cos}\left(\omega \right)+\left(1-\alpha {\right)}^{2}}\end{array}$ $\omega$ is the normalized pulsatance in radians per sample. You can substitute it with $\omega =\frac{2\pi f}{{f}_{s}}$ 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. 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 . 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\left(z\right)|$ surface in the Z-domain. As mentioned above, the DTFT is just a special case of the Z-transform, where $z={e}^{i\omega }$, i.e. the unit circle in the complex plane. Remember that the point $z={e}^{i\omega }$ is a point with a distance of $1$ 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 blue. Notice that this is the same curve as the blue curve in the magnitude response graph above: close to 0 when $\omega \to 0$ (the right half of the circle) and negative when $\omega \to \pi$ (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}^{i\omega }|=1$ or $\mathrm{log}\left(|{e}^{i\omega }|\right)=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 -point, because .
To find it, just solve the following equation: $|H\left({e}^{i{\omega }_{c}}\right){|}^{2}=\frac{1}{2}$ $\begin{array}{rl}\frac{{\alpha }^{2}}{1-2\left(1-\alpha \right)\mathrm{cos}\left({\omega }_{c}\right)+\left(1-\alpha {\right)}^{2}}& =\frac{1}{2}\\ {\omega }_{c}=\mathrm{arccos}\left(\frac{{\alpha }^{2}+2\alpha -2}{2\alpha -2}\right)\end{array}$

For example, if , then ${\omega }_{c}=\mathrm{arccos}\left(\frac{23}{24}\right)\approx 0.2897\frac{rad}{sample}$.
To convert it to a frequency in Hertz, you can multiply $\omega$ by $\frac{{f}_{s}}{2\pi }$, with ${f}_{s}$ the sample frequency.
For example, if the sample frequency is , then .

#### 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\left(z\right)=\frac{{b}_{0}+{b}_{1}{z}^{-1}+{b}_{2}{z}^{-2}+\cdots +{b}_{p}{z}^{-p}}{{a}_{0}+{a}_{1}{z}^{-1}+{a}_{2}{z}^{-2}+\cdots +{a}_{q}{z}^{-q}}.$ 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\left(z\right)=\frac{\alpha }{1+\left(\alpha -1\right){z}^{-1},}$ so ${b}_{0}=\alpha$, ${a}_{0}=1$ and ${a}_{1}=\alpha -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()