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 and be the input and the output signals in the frequency (or better, Laplace) domain, respectively, then the transfer function is defined as
A simple example is the transfer function of a first order low pass filter, which is defined as
where is the filter time constant. Alternatively, this can also be expressed in terms of the cutoff frequency (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 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 , 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, and represent the -th output and input sample, respectively. So is the signal to be filtered, and the filtered output. From the above equation we can extract the “implementation” of our digital filter by simply isolating on the left side:
This formula simply says that the output of the filter at time step is a linear function of previous input and output values. Now we need to find the values of the coefficients and . 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 and not because we are in the digital domain, and not in the analog domain as in the first example. In the domain, represents a delay of samples. If the transfer function is given, then implementing the digital filter is straightforward. However, we usually have the transfer function in the analog domain and not 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 we compute for , where 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 . We can simply divide numerator and denominator by the highest order , in this case , 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 is the gain, the damping factor, the natural frequency (not the cutoff frequency), and 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 seconds. Given that indicates a delay of samples we obtain the corresponding delay in the digital domain as with being
where 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 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 ) to a step function and test our first and second order filters. We set the sampling time ms for both filters. For the first order filter, we set s, while for the second order filter we choose , , , and two different time delays: s and s. The result is shown in the following plot.