Convolution Lab

Resources

Video

Slides

Introduction

In this assignment, you will write a custom 1-D convolution function in Matlab and use it to pass signals through LTI systems by convolution with the system impulse response. Matlab has a built-in convolution function called conv. Later on you can use conv, but right now while you're learning about the mechanics of convolution, you must write your own.

Definition of Convolution

The mathematical definition of convolution is given by

(1)y[n]=x[n]h[n]=k=x[k]h[nk],<n<.

The general convolution definition in (1) accommodates infinite length inputs x[n] and impulse responses h[n]. In this assignment, we will focus on the finite case where the input x[n] has length N and the impulse response h[n] has length L. We also assume that both x[n] and h[n] "start" at time n=0. Given these specifications, the convolution sum may be written as

(2)y[n]=x[n]h[n]=k=max(0,nL+1)min(N1,n)x[k]h[nk],0n<N+L1.

Finite length x[n] and h[n] affect the convolution sum in two ways.

  1. Convolution of length-N and length-L sequences produces a output sequence y[n] of length N+L1. Therefore, output y[n] is defined for n=0,1,2,,N+L2.

  2. The sequences x[n] and h[n] are assumed to be zero outside their support sets:

(3)x[n]=0forn<0andnN,(4)h[n]=0forn<0andnL.

Removing from the convolution sum terms that are zero due to these limits leads to the messy looking limits on the summation.

Mechanics of Convolution

The mathematical definitions of convolution in (1) and (2) present convolution as a sequence of operations. To compute the output y[n] at time n, do the following:

  1. Time reverse the impulse response and shift it to time n. At this point the output time n is fixed. Think of h[nk] as being a function of k.

  2. Multiply x[k]h[nk] for a suitable range of k values.

  3. Sum the products and assign the result to y[n]=kx[k]h[nk].

By repeating these steps for each value of n, the output sequence y[n] is computed. This process requires two for loops: an outer loop over the output times n and an inner loop over k to form and sum the products.

Diagram illustrating the flip, shift, inner-product action of convolution.

There are multiple ways to implement convolution in code. The approach we will follow takes inspiration from the table depicted above. In this example N=8 and L=5. The length of the output y[n] is N+L1=12. These twelve values are laid out in the first row of the table. The table uses the compressed notation of subscripts yn to represent y[n]. The second row of the table lays out the eight values of the input sequence xn. The other rows of the table show time-reversed shifts of the impulse response hnk=h[nk] for twelve shifts n=0,1,2,,11, one shift for each output value. Note that whereas the input xn and output yn appear in natural order with the subscript/index increasing from left to right, the impulse response is laid out in time-reversed order with the subscript/index decreasing from left to right.

To compute the output yn at time n, multiply the sequence hnk on the nth row of the table by the input sequence xn on the second row of the table, sum the products, and assign the result to the appropriate value yn in the top row of the table. A design decision that greatly simplifies the code is to pad the input sequence xn with L1=4 zeros on both sides. Zero-padding eliminates the need to encode the complicated limits in the finite summation in (2). Zero-padding is justified due to the assumption in equation in (3).

The matlab script below is a barebones script for testing out the ideas presented above. After creating a simple input signal and impulse response, the script calls the custom convolution function myconv. The rest of the script is plotting.

The basic structure of the custom convolution function myconv appears below. This code is placed in a separate file called myconv.m. Your job is to figure out the array indexes in the inner convolution loop.

If done correctly, the result should look like this.

Stem plot showing the convolution result.

The output sequence is [1,2,3,4,5,5,5,5,4,3,2,1].

To understand the Matlab code, you can set breakpoints and run the script in debug mode. You can examine variables and make plots to visualize signals at each point and in each step of a loop. Refer back to the table above and make your code implement the operations implied by the table.

Process Audio Signal with Low Pass Filter

Download the audio file and the impulse impulse response file. The script below illustrates how to load audio data from a WAV file and to read the impulse response from the binary file. It uses your myconv function to filter the input signal by convolution with the impulse response. It also plots the input and output signals in the time domain, the impulse response of the filter, the magnitude response of the filter, and spectrograms of the input and output signals. I am providing this script for educational purposes. There are over 80 lines of code in this script. Most of these lines are for visualization. This script illustrates how to:

  1. Write commented and organized code in Matlab.

  2. Make time domain plots that have (1) labeled axes, (2) title, (3) legend where appropriate, (4) a grid, (5) time axis with units of seconds, (6) plot two data sets in one axis.

  3. Make frequency responsen plots that have (1) labeled axes, (2) title, (3) frequency axis with units of Hertz, (4) decibel scaled y-axis, (5) adjust axis limits.

  4. Make spectrograms that have (1) labeled axes, (2) title, (3) color bar, (4) good intensity scaling, (5) several subplots in a figure.

Please study this code and learn how it works so that you can use Matlab, develop good coding habits, create beautiful visualizations of signals in time and frequency domains.

Pictures of the various signals and responses are included here.

 

Filter impulse response.

Note that the impulse response has a sinc-like shape.

Filter magnitude response.

This filter is called a low-pass filter because it passes low frequency components (approximately 0 dB attenuation) and attenuates higher frequencies by about 80 dB. Notice a small amount of "ripple" in the passband response and the uniform ripple amplitude in the stop band. Note that the frequency response extends to higher frequencies. This plot is zoomed in to show the lower 1,200 Hz.

Time domain signals.

Can you see that the filter rejected some of the notes in this song?

Spectrograms of input and output signals.

These spectrograms show how energy in the input and output signals is distributed over time and frequency. The spectrogram of the input shows that it contains energy at frequencies above 400 Hz, but these components have been removed in the output spectrogram. The spectrograms explicitly show that the filter rejects high frequencies. Can you hear the effect when you listen to the input and output signals?

Assignment

Write a report that addresses the following questions. This report includes Matlab plots. When making plots in Matlab or by hand, please keep the following points in mind.

Here are the report questions (five points for each question).

  1. What are the array indexes you used for xzp(...) and h(...) in the myconv code?

  2. Include a stem plot showing the convolution of the following sequences: x = [1, 2, 3, 4, 5, 6, 7, 8] and h = [1 2 3 4]. Also list the values y[n]=x[n]h[n] in the convolution result.

  3. Plot the impulse response of the filter used in the audio example.

  4. Plot the dB-magnitude response of the filter used in the audio example.

  5. Use the magnitude response plot to explain why the filter in the audio example is considered to be a low-pass filter. Describe the features of the passband and the stop band of this filter. What frequencies does this filter "pass" and what frequencies does this filter "stop"?

  6. What would the response of a high pass filter look like? Make a sketch.

  7. Plot spectrograms of the audio signals at the filter input and output (2 spectrogram plots).

  8. Use the spectrograms to describe the action of this filter on a signal.

  9. Plot the filter input and output signals on the same axis (time-domain plot).

  10. There are times when the input signal is large and the output signal is essentially zero. See for example the intervals 2-3 seconds and 7-8 seconds in the time-domain plot. Use the filter magnitude response and the spectrograms to explain what is happening in these intervals. Why is the output signal zero there?

  11. Attach your Matlab code.

Policy note: Please include your answers to these questions, the plots, and your code in a single PDF file uploaded to Canvas.