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

\[H(s) = \frac{Y(s)}{X(s)}.\]

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

\[H(s) = \frac{1}{1 + \tau s},\]

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):

\[H(s) = \frac{2\pi f_c}{2\pi f_c + s}.\]

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.

bode

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

\[\sum_{p=0}^N y[n-p]\alpha_p = \sum_{q=0}^M x[n-q]\beta_q.\]

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:

\[y[n] = \frac{1}{\alpha_0}\left( \sum_{q=0}^M x[n-q]\beta_q - \sum_{p=1}^N y[n-p]\alpha_p\right).\]

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

\[H(z) = \frac{Y(z)}{X(z)} = \frac{ \beta_0 + \beta_1 z^{-1} + \beta_2 z^{-2} + \dots + \beta_M z^{-M} }{ \alpha_0 + \alpha_1 z^{-1} + \alpha_2 z^{-2} + \dots + \alpha_N z^{-N} }.\]

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:

\[\begin{align*} H(z) = H(s){\big \vert}_{s = \frac{2}{T}\left(\frac{z-1}{z+1}\right)} &= \frac{1}{1 + \tau\left(\frac{2}{T}\left(\frac{z-1}{z+1}\right)\right)}\\ &= \frac{1}{\frac{T\left(z+1\right) + 2\tau \left(z-1\right)}{T\left(z+1\right)}}\\ &= \frac{z+1}{z+1 + \frac{2\tau}{T} \left(z-1\right)}\\ &= \frac{z+1}{\left(1 + \frac{2\tau}{T}\right)z + \left(1-\frac{2\tau}{T}\right)} \end{align*}\]

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

\[H(z) = \frac{1 + z^{-1}}{\left(1 + \frac{2\tau}{T}\right) + \left(1 - \frac{2\tau}{T}\right)z^{-1}}.\]

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

\[\beta_0 = \beta_1 = 1, \quad\alpha_0 = 1 + \frac{2\tau}{T}, \quad\alpha_1 = 1 - \frac{2\tau}{T}.\]

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

\[y[n] = \frac{1}{1 + \frac{2\tau}{T}}\left(x[n] + x[n-1] - \left(1 - \frac{2\tau}{T}\right)y[n-1]\right).\]

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

\[H(s) = \frac{k}{s^2 + 2\theta\omega_n s + \omega_n^2}e^{-T_d s},\]

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

\[d_d = \text{round}\left(\frac{T_d}{T}\right),\]

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:

\[\begin{align*} H(z) &= \frac{k}{\left(\frac{2}{T}\left(\frac{z-1}{z+1}\right)\right)^2 + 2\theta\omega_n \left(\frac{2}{T}\left(\frac{z-1}{z+1}\right)\right) + \omega_n^2} z^{-d_d}\\ &= \frac{k(T(z + 1))^2}{ (2(z-1))^2 + 2\theta\omega_n(2(z-1))T(z+1) + \omega_n^2(T(z+1))^2} z^{-d_d}\\ &= \frac{kT^2z^2 + 2kT^2z + kT^2}{ \left(\omega_n^2 T^2 + 4 \omega_n \theta T + 4\right)z^2 + \left(2 \omega_n^2 T^2 - 8\right)z + \left(\omega_n^2 T^2 - 4 \omega_n \theta T + 4\right)} z^{-d_d}\\ &= \frac{kT^2 + 2kT^2z^{-1} + kT^2z^{-2}}{ \left(\omega_n^2 T^2 + 4 \omega_n \theta T + 4\right) + \left(2 \omega_n^2 T^2 - 8\right)z^{-1} + \left(\omega_n^2 T^2 - 4 \omega_n \theta T + 4\right)z^{-2}} z^{-d_d}\\ \end{align*}\]

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

\[H(z) = \frac{kT^2 z^{-d_d} + 2kT^2z^{-1 - d_d} + kT^2z^{-2 - d_d}}{ \left(\omega_n^2 T^2 + 4 \omega_n \theta T + 4\right) + \left(2 \omega_n^2 T^2 - 8\right)z^{-1} + \left(\omega_n^2 T^2 - 4 \omega_n \theta T + 4\right)z^{-2}}\]

From the formula above we identify our coefficients, which are

\[\beta_{d_d} = \beta_{2 + d_d} = kT^2, \quad \beta_{1 + d_d} = 2kT^2,\\ \quad\alpha_0 = \omega_n^2 T^2 + 4 \omega_n \theta T + 4, \quad\alpha_1 = 2 \omega_n^2 T^2 - 8,\\ \alpha_2 = \omega_n^2 T^2 - 4 \omega_n \theta T + 4.\]

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

\[y[n] = \frac{ x[n-d_d]\beta_{d_d} + x[n-d_d-1]\beta_{d_d+1} + x[n-d_d-2]\beta_{d_d+2} - y[n-1]\alpha_{1} - y[n-2]\alpha_{2} }{ \alpha_0 }\]

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.

filters