An NSF Research Experience for Teachers Program
A filter changes the nature of the signal. A digital filter does so by manipulating a discretized signal.
As we have seen in the previous lab every signal in the time domain has an equivalent representation in the frequency domain by way of the Fourier transform. Information in a signal can be distinctly displayed as a function of the three parameters: time, frequency and phase. Different filters are designed to manipulate the said information in the signal with respect the different parameters.
Use for example the signal x as a function of time:
we can then create filter that when the signal passes through it changes the amplitude, to get . The output will have a different amplitude compared to the input, depending on the frequency.
A moving average filter does exactly what its name suggests. It is a very commonly used filter and very simple in its implementation. It performs optimally in the time domain particularly functioning to reduce random noise. It is mathematically represented as:
NOTE: A moving average filter is a simple finite impulse response(FIR) filter. Specifically it is a low-pass filter. You can also think of this as a convolution.
Use a sine wave from the signal source
and add noise to it (Discussed in Lab 1). Pass this to a 8-tap moving average filter. The flowgraph should resemble this diagram:
Observe the signal before and after the filtering. Cascade multiple 8 point average filters (i.e. connect end to start). This will require saving just your filter as a ‘hier block’ with two ‘pad sources’ and two ‘pad sinks’ (why two?). Once the block is compiled, reload the blocks (refresh button in gnuradio-companion). There should now be a “GRC Hier Blocks” section with your block named. How does the signal change adding more and more averaging?
*Note: There are two options for ‘heir’ block with or without ‘QT GUI’ appending heir. Choose the one without the ‘QT GUI. Male sure you have to disabled blocks in your floegraph *
“A finite impulse response (FIR) filter is a filter whose impulse response (or response to any finite length input) is of finite duration, because it settles to zero in finite time. This is in contrast to infinite impulse response (IIR) filters, which may have internal feedback and may continue to respond indefinitely (usually decaying).”^{1}
The flow diagram of an FIR filter is shown below, are inputs and outputs respectively. is the unit delay and are the filter coefficients.
FIR filters are mathematically fast and relatively easier to design and implement digitally.
We shall design FIR filters using the graphical tool that ships with gnuradio. This tool requires an additional software dependency to work which can be installed by opening a new termina window and enteringsudo apt install python-qwt5-qt4
. Once the dependency is installed, in the taskbar (move mouse to the top of the window) click on tools
and then filter design tool
We shall design all our filters by graphically using this tool. Theoretically, there are several methods employed to design a filter. The most common used method is by designing a window function. In the window design method, one first designs an ideal filter which may have an infinite time response (IIR) and then truncates the impulse response by multiplying it with a finite length window function. The result is a finite impulse response filter whose frequency response is modified from that of the IIR filter. Multiplying the infinite impulse by the window function in the time domain results in the frequency response of the IIR being convolved with the Fourier transform of the window function ^{1}
For us, this GUI does everything behind the scenes. We observe the response of the designed filters in the window and use the necessary taps/filter-coefficients in the FIR block.
GNU Radio has two kinds of FIR filter blocks. A decimating and interpolating FIR filter which reduce(by dividing) and increase(by multiplying) the number of samples of the filtered signal respectively. When we design our filter we simply copy and paste the Filter Taps in the Taps
field of the filter block properties.
A chord is the sound produced by playing three or more notes together. We can use fourier transform to extract the frequencies that make up those chords. Build a flow-graph that simply uses an audio source
, converts the float output to complex and sends that into a Frequency Sink
Once ready, let’s play the C-Major chord. It is the C (= 261.63Hz) , E (= 329.63Hz) and G (= 392Hz). Notice that the frequency spectrum shows more than just the three fundamental frequencies. They show the harmonics of the three frequencies mentioned above! Recall from Fourier series , these harmonics including the fundamental frequencies when added together makes the waveform that forms the music note. The amplitudes of these harmonics for the same note is different for different instruments because the “timbre” i.e. the waveform produced by every instrument is different!
Now let us extract just the fundamental frequencies by implementing low pass filters to have:
The outputs of the filters in the frequency sink should look like this by zooming in:
You may want to save the filtered audio to disk. Look for the the appropriate “sink” block to do so! You can try this with any other chord. Change the filters accordingly. Click here for the frequency of every key on the piano.
Fun Fact: Using Fourier transforms and some cool filtering you can transcribe any chords in any any music piece. For example, deciphering the “mystery” opening chord of the Beatles’ Hard Day’s Night
Let us try and construct a simple digital equalizer. I shall present the primer for a very simple 3 band equalizer divided into three frequency bands created by three band-pass filters:
Attach this to FM Radio flow-graph before the audio sink
The gnuradio flow-graph will follow this diagram:
You can of course have more than three frequency channels, you can google commonly used frequency divisions in commercial equalizers.
NOTE: “Ancient” stereos/cassette players and old audio workstations in music studios have analog equalizers designed using analog filters. If you look at the frequency sink of the output of our digital Equalizer you can observe that the filters work really really well. Digital filters can be very very precisely designed. The roll-off(or transition width) can be as narrow as you want!
Let us be more ambitious and design a Digital Guitar Tuner in GNU Radio. It is rather simple and very similar to the equalizer: It is a series of band-pass filters centred on the fundamental frequency of the strings. The Fundamental frequencies of the open guitar string in a standard tuning:
String | Frequency |
---|---|
1 (E) | 329.63 Hz |
2 (B) | 246.94 Hz |
3 (G) | 196.00 Hz |
4 (D) | 146.83 Hz |
5 (A) | 110.00 Hz |
6 (E) | 82.41 Hz |
The flow-graph should resemble this diagram:
Choose an appropriate bandwidth. Add frequency and waterfall sinks for each string. What type of window will you use?
The flow diagram of an IIR filter is shown below, are inputs and outputs respectively. is the unit delay and are the feed-forward filter coefficients, and are the feed-back coefficients.
Analog filters built using traditional circuit elements like inductors, resistors, capacitors, transistor, op-amps, etc. have mathematical approximations that allow us to implement them digitally. There are 4 types each excelling in one parameter while under-performing in another:
Butterworth: Flattest pass-band but a poor roll-off rate.
Chebyshev: Some pass-band ripple but a better (steeper) roll-off rate. Chebyshev filters where the ripple is only allowed in the pass-band are called type 1 filters. Chebyshev filters that have ripple only in the stop-band are called type 2 filters , but are are seldom used. Chebyshev filters have a poor phase response.
Elliptic: Some pass- and stop-band ripple but with the steepest roll-off rate.
IIR or Infinite response filters are the closest digital implementations of these filters.
Now create an IIR filter to again pull out the fundamental frequency of the C-chord. Do this again using the ‘Filter design tool’. When you use an IIR filter, the design is now in ‘normalized’ units of ‘cycles/sample’. To get units of frequency, you must multiply by the sample rate/2 (fs/2) you are using. So with a sample rate of 48000, you divide the frequency you want by 24000. For an IIR filter you can use the “IIR Filter” block, where the feed-forward taps are the “b’s” and the feed-back taps are the “a’s”. Be sure to also select the ‘new’ style of taps. How many coefficients (taps) are required for an IIR filter in comparison to an FIR filter with a similar response function? More? Less? Why?