# Implementing a digital filter

Category: Misc
Date: January 31, 2017

Disclaimer: The concepts of this post are self-taught. I have never taken a course in digital signal processing. I studied this myself and tried to report here what I understood for future reference. Please do not take this post as a scientific and reliable source. If you find some mistakes or a better way to explain these concepts, feel free to drop me a mail.

Filter have tons of applications in digital signal processing including audio and image processing or telecommunications. Filters are usually defined through their transfer function in the frequency domain, i.e., a function that represent how the filter attenuates different spectrum frequencies. More formally, let $X(s)$ and $Y(s)$ be the input and the output signals in the frequency (or better, Laplace) domain, respectively, then the transfer function $H(s)$ is defined as

A simple example is the transfer function of a first order low pass filter, which is defined as

where $\tau$ is the filter time constant. Alternatively, this can also be expressed in terms of the cutoff frequency $f_c = \frac{1}{2\pi\tau}$ (in Hz):

The best way of understanding this is drawing the Bode plot for the gain. The figure below shows the filter gain as a function of the frequency, for a cutoff frequency $f_c = 1$ Hz. The plot shows that, for frequencies up to 1 Hz, the gain is 0 dB, so low frequency components of the signal are untouched by the filter. At 1 Hz, the Bode plot starts to decay showing that, for example, the 100 Hz component is attenuated by 20 dB.

The interesting question is, given the transfer function $H(s)$, how can we digitally implement the filter? The first thing to know is that a digital filter is implemented as an autoregressive moving average, so a moving average where the output at a certain time step linearly depends on previous output and input values. We can express the filter using the so called Linear Constant-Coefficient Difference (LCCD) equation, that is

In this equation, $y[k]$ and $x[k]$ represent the $k$-th output and input sample, respectively. So $x$ is the signal to be filtered, and $y$ the filtered output. From the above equation we can extract the “implementation” of our digital filter by simply isolating $y[n]$ on the left side:

This formula simply says that the output of the filter at time step $n$ is a linear function of previous input and output values. Now we need to find the values of the coefficients $\alpha_k$ and $\beta_k$. Without going into the details, by computing the transfer function of the above equation (through the Z-transform) we obtain

The transfer function variable now is $z$ and not $s$ because we are in the digital domain, and not in the analog domain as in the first example. In the $z$ domain, $z^{-k}$ represents a delay of $k$ samples. If the transfer function $H(z)$ is given, then implementing the digital filter is straightforward. However, we usually have the transfer function $H(s)$ in the analog domain and not $H(z)$ in the digital domain. So our root problem finally is: can we convert the transfer function in the analog domain to the transfer function in the digital domain? The answer is obviously yes, and there are different methods. The one we see here is through the bilinear transform, also known as Tustin method.

The approach is fairly simple. To obtain $H(z)$ we compute $H(s)$ for $s = \frac{2}{T}\left(\frac{z-1}{z+1}\right)$, where $T$ is the digital sampling time. To do a simple example, we can compute the digital transfer function for the first order low pass filter described at the beginning:

The shape seems reasonable, but we are still missing something, i.e., the digital transfer function of a filter is expressed with negative powers of $z$. We can simply divide numerator and denominator by the highest order $z^a$, in this case $z^1$, to obtain

The above transfer function permits to easily spot our coefficients, i.e.,

Now we can plug the coefficients into the difference equation to obtain the digital implementation of the low pass filter:

Later on we will see how this filter behaves.

Another, filter I encountered had the following was a second order filter with the following trasfer function

where $k$ is the gain, $\theta$ the damping factor, $\omega_n$ the natural frequency (not the cutoff frequency), and $T_d$ the time delay. Here the computation is exactly the same, but there is the exponential terms which comes into play. The exponential term simply indicates a delay of $T_d$ seconds. Given that $z^{-k}$ indicates a delay of $k$ samples we obtain the corresponding delay in the digital domain as $z^{-d_d}$ with $d_d$ being

where $T$ is the sampling time. This is clearly an approximation, so the smaller the sampling time, the better. We obtain the remaining part of the transfer function in the digital domain using the bilinear transform as before:

We can now simply multiply the numerator by $z^{-d_d}$ to include the delay, obtaining

From the formula above we identify our coefficients, which are

We can now plug the coefficients inside our digital filter formula to obtain

To see how the filters behave, we set our reference signal (input signal $x[k]$) to a step function and test our first and second order filters. We set the sampling time $T = 10$ ms for both filters. For the first order filter, we set $\tau = 0.5$ s, while for the second order filter we choose $k = 0.16$, $\theta = 0.6$, $\omega_n = 0.4$, and two different time delays: $T_d = 0.1$ s and $T_d = 5$ s. The result is shown in the following plot.