0% found this document useful (0 votes)
11 views

Lab_2_FIR___IIR_Filter_GNU_Radio

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab_2_FIR___IIR_Filter_GNU_Radio

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab 2: FIR, IIR Filter Implementation in Embedded Python Block and

their comparison

EE 352 DSP Laboratory (Spring 2022)

Lab Goals
The purpose of this lab is to build the framework for real-time filtering of an incoming signal in GNU Radio.
We will learn to implement an FIR Filter and Direct-form I IIR Filter in embedded python block of GNU
Radio. We will also learn about the advantages of one over another.
• Implementation of FIR Filter using linear buffer
• Direct form I implementation of an IIR filter
• FIR vs IIR implementation of a system

1 Introduction to FIR
1.1 Convolution
The convolution operation which is the fundamental operation in realizing a finite impulse response (FIR)
filter, is given by,

N −1
def X
y[n] = h[m]x[n − m] (1)
m=0

where h[n] is the impulse response (N length FIR Filter) of the filter, x[n] is the input signal and y[n] is the
filtered output signal. In non-real-time processing, the entire input data and the impulse response samples
are stored in the memory, and the processing is carried out on the stored data. But in real-time processing,
we are interested in acquisition and processing of samples simultaneously, which means that the processing
delay per input sample must be bounded even if the processing continues overall for an unlimited time. So it
can be said that the mean processing time per sample should not be greater than the sampling period. More
generally, the number of input samples entering the DSP in a particular time interval is same as the number
of processed samples delivered as output in the same time interval. As we are doing this lab in software we do
not need to worry about the time intervals. But while implementing on hardware one should always keep that
in mind.

1.2 Linear Buffer


Here, we will use a buffer whose length is equal to the length of impulse response i.e., coefficients of the filter,
which will store the input data samples required to generate the filtered output sample at a particular time
instant.
The linear buffer usage is explained using Figure 1. Here, memory locations are indicated by 0, 1, · · · , 4. We
can observe from the figure, that the most recent input sample will always be stored in memory location 0. In
order to avoid overwriting the required previous data stored at the same location, we need to first move the
data in each memory location to the next subsequent location (3 ⇒ 4, 2 ⇒ 3, 1 ⇒ 2, 0 ⇒ 1) and then write
the new data in location 0. In this process, the data sample in memory location 4 is discarded. But shifting
in memory is an expensive operation (in terms of CPU cycles) and considering that it has to be performed in
addition to the processing, this process becomes costly.

1
Figure 1: Linear buffer (a) new sample is put in position 0, (b) before the next sample comes, elements of the
buffer have to be shifted by one step and the latest sample needs to be accommodated in position 0.

1.3 Learning checkpoints


All the following tasks need to be shown to your TA to get full credit for this lab session.
1. You will be using the ‘FIR Implementation’ folder for this checkpoint. Before writing your code in
Embedded python Block you need to check its correctness and debug your code. For this you can use the
syntest.py python script. You are provided with a synthetic input i.e., a vector of 5 elements named
syninp. Six filter coefficients are stored in the list coefs. The buffer to store the incoming input samples
is named buff. The loop in linBuffconv() function is iterating through the inputs in the syninp list.
This function calls the work() function to calculate a new output.Note that you will be generating one
output for one input, so you will have 6 output samples. You have to complete the work() function to
find the output for each input sample by multiplying the buffer with the coefficient list.
Convolution is performed using the buffer using two stages:
• Shift the previous elements in the buffer and put the new input sample at the 0th index position of
the buffer.
• Carrying out multiplication and accumulation to generate the output sample.
2. After you have written the work function in the syntest file, check your implementation by comparing
convolution output with pen paper calculation output. Note that you need to compare only first six
output samples. Show it to your TA.
3. Now you will be implementing convolution using linear buffer in GNU Radio Embedded Python Block.
You will be using the ‘FIR Filter.grc’ file for this. Set the sampling frequency to 8000Hz and use

h[n] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}

Now obtain the magnitude frequency response of the filter by varying the input sine wave frequency from
0Hz to 4000Hz. What type of filter is it ? Is it High-Pass, Band-Pass, Low-Pass or Band-stop ?
4. Find the 3 dB cutoff frequency from the magnitude frequency response plotted.
5. Next you will be using the files in the ‘Noise Filtering’ Folder. Copy paste your FIR filter implementation
code from the previous implementation in the current Embedded python block. Here you will use a FIR
filter to filter a noisy sine wave corrupted by white Gaussian noise. Observe the noisy input signal and
the filtered noise free signal.
6. Using the freqz function provided by Matlab/Octave, plot the frequency response of the filter used in
the above checkpoint and identify the type of the filter (Lowpass / Highpass / Bandpass / Notch) and
note down the cutoff frequency. Verify the cutoff frequency with that obtained in GNU Radio.
7. Next we will be using GNU radio to filter an audio file saved in .wav format sampled at 22050Hz with a
Low pass and High pass Filter. We will be using files in the ‘Audio Filtering’ folder.
• Low pass Filter coefficients containing 51 elements are denoted by the list coeff on line 31.
• High pass Filter coefficients containing 62 elements are denoted by the list coeff on line 36.

2 of 4
• Comment one while using the other. Filter the audio signal first using the Low pass filter and later
using the High pass filter. Observe the effect on the original signal by listening to the .wav file.
8. You need to Estimate the number of Operations required for calculation of one output sample. Fill them
in Table 1 for both the FIR High pass and the Low pass Filter:

Operation Number of operations FIR Number of operations IIR


Add
Multiply
Read
Write

Table 1: Number of Operations in FIR and IIR Filter implementations

2 Infinite Impulse Response (IIR) Filters


A linear constant coefficient difference equation of the form given by the equation (2) represents a causal LTI
system with an infinite impulse response as long as ai 6= 0 for at least one i, i = 1, . . . , M .

N
X M
X
ai y[n − i] = bj x[n − j] (2)
i=0 j=0

The transfer function H(z) obtained by applying Z transform on both sides of equation (2) is as follows:

PM −j
j=0 bj z
H(z) = PN (3)
−i
i=0 ai z

Time domain impulse response h[n] obtained by applying inverse Z transform to equation 3 is of infinitely long
duration. Such a filter is therefore called an Infinite Impulse Response (IIR) filter.
The techniques to implement a FIR digital filter is not applicable to IIR filter due to the feedback component.
Thus, we need to implement IIR filters differently. Assuming a0 = 1, an IIR filter can be described using the
difference equation
XN M
X
y[n] = − ai y[n − i] + bj x[n − j] (4)
i=1 j=0

This form can be exploited to implement the filter by defining two buffers of lengths M and N , for holding past
values of output and input, respectively. Thereby, performing finite multiplication and addition operations, an
IIR filter can be easily realized.
The difference equation 4 can be rearranged in different ways thereby obtaining different structures. One
such class of structures referred to as direct form structures is introduced in this lab. Other known forms are
cascaded sections, parallel sections, lattice structures and state-space structures.

2.1 Direct-form I IIR implementation


Algorithm for implementing an IIR filter using direct-from I structure, defined by eq 4, in GNU radio can be
written as follows:
1. Initialize two buffers of size M and N with zeroes or initial conditions, if known.
2. Start the acquisition of data.
3. Go on calculating the values of the output according to (eq 4) and appending them in the output buffer,
one after the other.
4. Simultaneously, keep appending the values of input in the input buffer.
5. Feed the output to the display unit (Frequency sink GNU block in our case) with every calculated output
sample.

3 of 4
6. Continue steps 3 to 5 till the input data-flow continues.
7. Note: Equation 4 assumes a0 to be equal to 1, in otherwise case you have to consider a0 as well.

Figure 2: Block diagram for direct-form I structure for the IIR filter given by eq 4

3 Learning checkpoint
All the following tasks need to be shown to your TA to get full credit for this lab session.
1. Here, you will be removing Gaussian Noise of amplitude 1 from Cosine wave, Consider the IIR filter given
by eq 5.
0.0230184 + 0.0460368z −1 + 0.0230184z −2
H1 (z) = (5)
1 − 1.6185196z −1 + 0.7105934z −2
(a) Using the freqz function provided by Matlab/Octave, plot the frequency response of the filter and
identify the type of the filter (Lowpass / Highpass / Bandpass / Notch) and note down the cutoff
frequency.
(b) Use the provided IIR noise. file to open python script in GNU radio’s ‘Python Embedded Block’
to implement the filtering. You have to complete the python script by writing suitable code at the
place indicated in the python script of embedded block to realize the direct form I implementation
of the above filter (5).
(c) Verify the working of your filter by using ‘QT GUI Time Sink’ for both input noise and output
signal.
(d) Also compare the freqz plots obtained in Matlab/Octave for filtering of Gaussian noise using FIR
and IIR Filters. What can you comment on the magnitude and phase of the two filters ?
2. Now, when you have successfully implemented IIR filter, let’s move forward to understand the effect of
Low Pass Filter and High Pass Filter on audio.
(a) You are provided with an audio(.wav) file, use this file as input to your Python block using ‘QT
GUI Wav File Source’ and store the filtered response as wav file using ‘QT GUI Wav File Sink’.
Open the ‘ReadMe.txt’ file provided to access LPF (Low Pass Filter) and HPF (High Pass Filter)
coefficients for IIR filter.
(b) Implement IIR filter with these coefficients and notice the change in the audio. What is getting
filtered out from the audio when you use LPF & HPF ?
(c) Now you need to Estimate the number of Operations required for calculation of one output sample.
Fill them in Table 1 for both the IIR High pass and the Low pass Filter. Compare the results with
that of the FIR Filter.

4 of 4

You might also like