## How to get a digital IIR filter from an analog or how to “invent” a digital IIR filter

on 16.04.2015 at 20:34Despite its widespread, the development of digital IIR filters (Infinite impulse response), even in our time, a time of high technology, for many remains a mystery. From time to time I come across articles or videos about the principle of the IIR filters design. Becomes sad when looking at the next one “good explanation” I understand that thanks to just such a presentation of the material most students loose their hope to understand it. Many of my friends have practical skills in the design of IIR filters, but frequently they use such package as Matlab, to obtain the coefficients of an IIR filter, sometimes even without understanding its principle of work. If you do not fully understand a principle of work of a particular piece of code, you can not say with certainty about its reliability. Refreshing knowledge from the university, I decided to share them with you in form of the article developing digital IIR filter “from scratch”!Further presentation of the material will occur in the context of the development of the real digital LPF (low pass filter) on the basis of the analog prototype RC filter. We will not go into all the details of the Laplace and Z-transform. I set myself the task to show to the reader the skeleton of development and it seems to me that for the fast start this would be sufficient. All the missing knowledge, if desired, can be easily gleaned from the Internet. “What’s in this article?” – You ask. The novelty lies in the fact that in this relatively short post will be collected together such things as:

- history of the emergence of analog, and later digital filtering;
- description of the analog RC filter with the help of mathematical models;
- conversion of the resulting model to another, more convenient, using the Laplace transform;
- explanation about why you need to accomplish the previous step;
- Z transform;
- obtain the difference equation (the result of the work).

So, let’s begin! Where did IIR filters? Where they are from? As often happens, they are taken from proven by many years practice circuit solutions. Historically known, that such an electronic component as a capacitor was developed (1745) long before the radio was developed (1895). At the dawn of audio, people developed radio. In any of these devices certainly were headphones or speaker. Developers have noticed that if a capacitor connected to a speaker (in parallel or in series) the speaker will sound differently. Definitely it was possible to pick a capacitor so that the sound becomes cleaner i.e. capacitor reduced the amount of noise. As time went on, science developed rapidly and soon grew an entire physical and mathematical machine, with which it became possible to explain such phenomena. As we know, digital filtering, until recently, not much used because of the inability of its physical implementation, because the processor didn’t have enough power in form of clock frequency. Due to that fact just industry of filtering in analog form was developed. By the way, it is quite well developed. Time passed and processors with frequencies above 100 MHz have become a reality. It became real to filter using the mechanism of IIR filters. But in order to use the analog filter model in the IIR digital filtering the analog filter should be described using mathematical model and then somehow transformed with the aim to be used in digital form. Next, we will go with you the path from analog RC filter to the final realization of digital IIR filter and of course with the sample program at the end of the article, since the article is oriented to practitioners, not just theoretical guys. So, we have the following schematic of analog RC filter:It contains a resistor R and a capacitor C. The input is a voltage which varies in time. The output will be different from the input voltage, which, in turn, also changes over time. The current which flows in the circuit can be described using the following expression for the capacitor:

Just for the sake of convenience, we make the change:

The resulting expression: In this step, we have obtained an expression which fully describes the behavior of our RC circuit. But instead of a simple dependence of output signal from the input one, we got something terrible at first glance, because we have the derivative of the output signal with respect to time. In general, our main task is to answer the following question: **“What should be done with the input signal to get the output one? Should we multiply, summarise, move in time, or should we do something else?** “. Looks like it is the key question. All of the following actions related to the mathematical transformations and it will be focused on the fact that you can get a clear expression, which shows the dependence of the output signal from the input. In other words, we need to solve a differential equation which was obtained before. Of course, it is possible to solve differential equation using multiple number of methods, but one of my knowledge – universal, is the method of using the Laplace transform. In math this method is called as method of operational calculus. What is it? I especially will not write formulas and try to explain with one’s fingers by means of gestures. Suppose we have a function (in discrete form it can be represented, for example, as a sequence of numbers, the waveform). So, under the Laplace transform understand the decomposition of the initial function into a sum of damped, undamped and increasing oscillating exponents (it is a sin to say that they are just sine wave signals). Here’s what they look like: Some of the readers may hold some parallels with the Fourier transform. Indeed, the Fourier transform is a special case for the Laplace transform at a constant (undamped) exponent.

Different functions may have different combination of exponents with their own frequency, phase and speed of attenuation. Laplace transform is very easy to use (at least in this particular case). There are tables of the Laplace transform, and we need just to look into them and substitute desired. As a result of this substitution, we get:

Well, it’s quite another matter. Now is not difficult to show the dependence of the image of output signal from the image of the input signal:

where:

The resulting ratio of the images can be viewed as the ratio of the output spectrum (of the exponential oscillograms) to the input spectrum (of the exponential oscillograms). What is variable S? There is a little more complicated, but wait – it’s just a complex variable. Where did it come from then? From the Laplace transform. Here it is not possible to underestimate the achievements of mathematics, because due to operations with complex numbers became possible to obtain the solution in more elegant form. I promised not to go into details. Only the skeleton (well, thick skeleton with minimal deviations)! I must admit, will be even more difficult. But if you do not go into the details, everything is pretty simple. The next step is the transition from the Laplace transform to the Z-transform (it is also the Fisher transformation or Laurent conversion). Z-transform is the same as the Laplace transform, but only for a discrete (not continuous) signal. Fortunately for us, there is an easy way to transition from continuous Laplace transform to discrete Z-transform and it is called “The bilinear transform.” It’s very easy. It is enough to make a substitution into the Laplace transform:

After substitution, we get the following:

Let me remind you, that we work in the frequency domain, not in time domain. We are working with complex spectrums. Our task is to obtain the dependence of the output from the input in the time domain rather than in the frequency one. We need something to move from frequency to time domain. This “something” is the inverse Z-transform. It also has a conversion table, and our task is to bring the equation into the form of “table components”. If we rewrite this expression as:

we get the reverse Z-transform:

Reader should already understands that now we are in the time domain. Lets rewrite:

The resulting expression can be regarded as the end result, but if to provide a substitution:

we obtain a more elegant form:

My congratulation! We got the difference expression, using which it is possible to filter the digital signal. For those who do not understand how to use this expression in practice I am giving an example source code written in the Python programming language:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import numpy as np import matplotlib.pyplot as plot Fs = 8000 # Sampling frequency (Hz) Fc = 2000 # Cutoff frequency (Hz) N = Fs # Number of samples per 1 s x = range(0, N) y = range(0, N) f1 = 100 # Hz f2 = 3500 # Hz N_TO_DISPLAY = 2*Fs/f1 # Generating test signal for t in range(0, N): x[t] = (np.sin(2.0*np.pi*f1*t/Fs) + np.sin(2.0*np.pi*f2*t/Fs))/2.0 # Calculating necessary values for filter dt = 1.0/Fs tau = 1.0/(2.0*np.pi*Fc) alpha = dt / (dt + 2*tau) # Applying filter for i in range(1, N): y[i] = alpha*(x[i]+x[i-1]) + (1-2*alpha)*y[i-1] # Drawing original and filtered spectrums plot.clf() plot.xlabel("Time") plot.ylabel("Sample value") plot.plot(x[0:N_TO_DISPLAY], 'r') plot.plot(y[0:N_TO_DISPLAY], 'g') plot.draw() |

In this code, the initial signal is the sum of two sine waves with a frequency of 100 Hz and 3500 Hz. Suppose that a sine wave with a frequency of 3500 Hz is an interference signal and we want to set up a digital filter to filter it. Sampling frequency in this case is equal to 8 kHz, and the cutoff frequency was chosen to be 2 kHz. As a result, of the script’s operation we get the following picture:

This picture shows the initial red signal (useful signal + noise) and filtered signal marked by green color. As you can see, the filtering is sufficient enough!

And at the end … I hope the information presented here was accessible to you? In the next article we will investigate the amplitude-frequency and phase-frequency responses of the RC low-pass filter developed by us in this article. Wait for new posts on digital signal processing in the context of their application in practice. I’ll be waiting for your comments at the bottom of the page! You can not imagine how nice to get this kind of feedback All the best!)

P.P.S. Note that the frequency scale transformation «frequency-prewrapping» was intentionally missed by me, so as not to scare the reader. Sure this kind of digital filter has a right to exist. If to compare the filter which uses «frequency-prewrapping» and other one, which doesn’t use it the response will be similar in the frequency range from 0 to the sampling frequency divided by 8 (approximate value is taken from my memory!). More details about the «frequency-prewrapping» I was going to write, and will write in the articles posted in future!