Lab 3. Digital Filters - Real Time DSP Lab Manual
Lab 3. Digital Filters - Real Time DSP Lab Manual
Contents
Aim of the experiment
Reading assignment
Lab 3 instructions: Week 1
Lab 3 instructions: Week 2
Lab 3 instructions: Week 3
Lab report contents
In this experiment, you will design and implement finite impulse response (FIR) filters and infinite impulse response (IIR) filters in Matlab. You
will measure the response of the filters using a variety of input test signals and quantify the change in magnitude response due to an increase
in filter order.
Reading assignment
Digital Signal Processing using Arm Cortex-M based Microcontrollers by Cem Ünsalan, M. Erkin Yücel, H. Deniz Gürhan.
Chapter 4, sections 1-8
Chapter 7, sections 1-4
Chapter 8, sections 1-6
Software Receiver Design by C. Richard Johnson, Jr., William A. Sethares and Andrew Klein
Chapter 3, sections 1-4
Chapter 4
Chapter 6, sections 1-2
Chapter 7
Course reader
Lecture 3 slides on Signals and Systems
Lecture 5 slides on Finite Impulse Response Filters
Lecture 6 slides on Infinite Impulse Response Filters
F stop1 = 4 kHz
F pass1 = 6 kHz
F pass2 = 14 kHz
F stop2 = 16 kHz
users.ece.utexas.edu/~bevans/courses/realtime/lectures/laboratory/stm32h735gdk/lab3/index.html 1/6
1/2/23, 3:35 PM Lab 3. Digital Filters — Real Time DSP Lab Manual
1. Open the the MATLAB Filter Designer by calling the filterDesigner function. Enter the parameters and design the filter.
2. Export the coefficients (File → Export → coefficients to workspace). The default variable name is Num, corresponding to the numerator of
the transfer function.
Later, we will experimentally measure the frequency response at 1000 Hz, 2000 Hz, …, 19000 Hz, and 20000 Hz. In order to compare
with our measurements, we need to first tabulate the theoretical response at these frequencies. Steps 3-5 describe this process.
3. Use the freqz function to get the response of the filter at 10 Hz intervals:
[h,f] = freqz(Num,1,2205,44100);
4. Define a function to convert the frequency response values returned by freqz from complex values to magnitude values in dB:
dB = @(x) 20*log10(abs(x));
5. Tabulate the theoretical magnitude response of this filter at 1000 Hz increments up to 20 kHz:
freq = f(101:100:2001);
resp_dB = dB(h(101:100:2001));
table(freq,resp_dB)
You may find it helpful to export the values to a spreadsheet so that you can record the measured values in a new column later. You can
use the xlswrite function to export the data to a spreadsheet
xlswrite('theoretical_response.xlsx',[freq,resp_dB]);
Include the tabulated values of the theoretical magnitude response in your lab report
6. We will need to define an array in C containing these coefficients. You can use the sprintf command in MATLAB to generate a comma
separated string which you can copy into your C code:
sprintf('%f,',Num)
2. In lab.c, define variables corresponding to the state values of the filter. An good convention is to create an array x where x[0]
corresponds with x[n], x[1] corresponds with x[n − 1], and so on. Initialize your variables so that all initial conditions are zero. An easy
way to do this is with the ‘universal zero initializer’ {0}, e.g.
3. In lab.c, modify the process_left_sample function to implement the filter as a tapped delay line.
1. Each time the process_left_sample function is called, we are given a new input sample from the ADC which is a 16-bit signed
integer. Convert this value to floating point, apply the appropriate scaling, and store it into the array location corresponding to
x[n]
1. Each time the process_left_sample function is called, we need to calculate the value of y[n] from a summation. Assign y a value of
zero to ‘reset’ the summation.
2. Create a for loop to perform the summation for the output of the tapped delay line
N
y[n] = ∑ b i x[n − i]
i=0
users.ece.utexas.edu/~bevans/courses/realtime/lectures/laboratory/stm32h735gdk/lab3/index.html 2/6
1/2/23, 3:35 PM Lab 3. Digital Filters — Real Time DSP Lab Manual
3. After computing y[n] we need to move the variables down the delay line. Create another for loop that reassigns the state variables
to prepare them for the next output when process_left_sample is called again. (Hint: use a down counting for loop.)
4. Connect the signal generator (set to 10 kHz) as the input and the oscilloscope as the output. Verify the operation of the program. (Since
10kHz is in the passband, you should see the signal on the output).
5. Measure the frequency response of your filter at 1 kHz increments up to 20 kHz by changing the frequency on the signal generator and
measuring the amplitude of the output signal. Include the measured values in your lab report.
6. Convert the amplitude values you measured to decibels. Since your measurement is a voltage and decibels are a ratio of power, the
conversion is:
P out V out
|H | = 10 log 10 ( ) = 20 log 10 ( )
dB
P in V in
The value for V out is your measurement. Check the setting your signal generator to determine V . in
7. Plot the values of your measured response on top of or next to the values of the theoretical response. Include this plot in your lab
report.
1. We will use the ARM CMSIS DSP library to perform filtering on a frame of data. Take a moment to read the description of the FIR filter
functions in this library.
2. In lab.c, initialize the variables necessary to use the arm_fir_f32 function
1. Create buffers for the input and output of the filter.
(The FRAME_SIZE constant specified in lab.h corresponds to the length of the entire circular buffer for both left and right channels.
When one half of the circular buffer is accumulating data, we will work with the other half. Additionally, we are only working with one
of the two channels. This is why our arrays are of length FRAME_LENGTH/4).
2. Create a buffer for the filter state
(The arm_fir_f32 function uses a state buffer of length num_taps+block_size-1 to implement the filter more efficiently.)
3. Create a filter instance struct.
arm_fir_instance_f32 filter_instance;
3. Copy the filter output buffer back to the array used by the DMA controller.
users.ece.utexas.edu/~bevans/courses/realtime/lectures/laboratory/stm32h735gdk/lab3/index.html 3/6
1/2/23, 3:35 PM Lab 3. Digital Filters — Real Time DSP Lab Manual
5. Connect the signal generator as the input and the oscilloscope as the output. Verify the operation of the program by sweeping through
different frequencies and observing the response.
F pass1 = 5 kHz
F pass2 = 15 kHz
A stop = 60dB
A pass = 2dB
1. Open the the MATLAB Filter Designer by calling the filterDesigner function. Enter the parameters and design the filter.
Notice that once the filter is designed, the filter structure is changed to Direct Form II, Second-Order Sections in the ‘Current Filter
Information’ panel.
2. Choose Edit → convet to single section, and export the coefficients (File → Export → coefficients to workspace). Two variables will be
created corresponding to the numerator and denominator coefficients of the transfer function.
3. Use the freqz function to get the response of the filter at 10 Hz intervals:
[h,f] = freqz(Num,Den,2205,44100);
4. Define a function to convert the frequency response values returned by freqz from complex values to magnitude values in dB:
dB = @(x) 20*log10(abs(x));
5. Tabulate the theoretical magnitude response of this filter at 1000 Hz increments up to 20 kHz:
freq = f(101:100:2001);
resp_dB = dB(h(101:100:2001));
table(freq,resp_dB)
You may find it helpful to export the values to a spreadsheet so that you can record the measured values in a new column later. You can
use the xlswrite function to export the data to a spreadsheet
xlswrite('theoretical_response_iir.xlsx',[freq,resp_dB]);
Include the tabulated values of the theoretical magnitude response in your lab report
6. We will need to define an array in C containing these coefficients. You can use the sprintf command in MATLAB to generate a comma
separated string which you can copy into your C code:
sprintf('%f,',Num)
sprintf('%f,',Den)
7. In the filter designer, change the structure back to the default (Direct-Form II, Second-Order Sections) and export the coefficients. Two
variables, ‘SOS’ and ‘G’ will be created.
users.ece.utexas.edu/~bevans/courses/realtime/lectures/laboratory/stm32h735gdk/lab3/index.html 4/6
1/2/23, 3:35 PM Lab 3. Digital Filters — Real Time DSP Lab Manual
8. The first three columns of the SOS matrix that is created corresponds to the numerator coefficients of three cascaded second-order
filters, and the last three columns correspond to the denominator coefficients. The G variable corresponds to the gain that is applied
between sections. Later, we will use these coefficients in our C Code. You can use sprintf in MATLAB to format the coefficients for C.
Numerator coefficients:
sprintf('{%f,%f,%f},\n',SOS(1,1:3),SOS(2,1:3),SOS(3,1:3))
Denominator coefficients:
sprintf('{%f,%f,%f},\n',SOS(1,4:6),SOS(2,4:6),SOS(3,4:6))
Gain:
sprintf('%f,',G)
1. In lab.c, initialize variables for your filter coefficients and state variables. Notice that MATLAB has assigned a value of one where the
nonexistent a coefficient would be.
0
2. In lab.c, modify the process_left_sample function to implement the summation for the filter output. The diagram above should help you.
N M
k=0 m=1
Recall that input_sample is a 16 bit integer, so you will need to scale it when converting to floating point.
3. In lab.c modify the process_left_sample function to simulate the movement of data through the delay blocks (Hint: use a down counting
for loop). Include the C code for your implementation of the filter in the lab report.
4. Connect the signal generator as the input and the oscilloscope as the output. Measure the frequency response of your filter at 1 kHz
increments up to 20 kHz by changing the frequency on the signal generator and measuring the amplitude of the output signal. Convert
the amplitude values you measured to decibels. Include the measured values in your lab report.
5. Plot the values of your measured response on top of or next to the values of the theoretical response. Include this plot in your lab
report.
1. In lab.c, initialize variables for your biquad coefficients state variables, and gain. A convenient way organize the coefficients and state
variables in C is with two dimensional arrays:
float32_t B[3][3] = {
<first three columns of SOS matrix>
};
float32_t A[3][3] = {
<last three columns of SOS matrix>
};
float32_t Y[3][3] = {0};
float32_t X[3][3] = {0};
2. In lab.c, add a for loop to process_left_sample that iterates over the three biquads to implement the filter.
1. The input to the first biquad should come from input_sample. Otherwise, the input should come from the output of the the
previous biquad.
2. Apply the appropriate gain value to the input of each biquad.
3. For each biquad, implement the second order filter using direct form (see the diagrams above).
The process_left_sample function should return the output of the last biquad (after appropriate scaling and conversion back to a 16 bit
integer). Include the C code for your implementation of the filter in the lab report.
users.ece.utexas.edu/~bevans/courses/realtime/lectures/laboratory/stm32h735gdk/lab3/index.html 5/6
1/2/23, 3:35 PM Lab 3. Digital Filters — Real Time DSP Lab Manual
3. Run the program and verify that it produces the expected bandpass response.
I. Introduction
Briefly explain the theory and algorithms behind the programs that you wrote. The slides and reading material might help you in writing this
section.
II. Methods
Describe the steps you took to implement the algorithms in your own words.
IV. Discussion
In this section, discuss the takeaway from each lab. You can mention any intuition that you developed. Also mention any problems that you
faced and how you rectified them.
V. Assignment questions
Please answer the following questions.
users.ece.utexas.edu/~bevans/courses/realtime/lectures/laboratory/stm32h735gdk/lab3/index.html 6/6