EE 328 Signals and Systems Laboratory Spring 2011 Experiment 3: Time Domain Convolution Introduction: This assignment emphasizes how MATLAB can be used to represent continuous-time signals and to approximate continuous-time convolution. In particular, we are going to use MATLAB to rework the Signals and Systems Workbook chapter 1's problem 4, parts (b) and (c). Although you have already been exposed somewhat to MATLAB, we will assume here that you don't have much experience using it (so bear with us if you do). All you have to do is follow along with the tutorial and turn in the specified plots.before you begin, here are some conventions that we will be using: Things that you need to turn in are in bold. Equations and theoretical expressions are in red. Don't enter these directly into MATLAB (it may choke if you try). The actual expressions you need to type into MATLAB are in blue. Objectives: 1) Represent continuous time signals in MATLAB. 2) Use MATLAB to perform basic transformations on continuous time signals. 3) Use MATLAB to perform convolution in time domain. Procedure: This LAB has three parts. In part 1, we will learn how to represent a continuous time signal in MATLAB. In part 2, we will perform some basic operations on continuous time signal, again using MATLAB and finally in part 3, we will perform time domain convolution of continuous time signals. Part 1: Creating a rectangular pulse in MATLAB Consider the rectangular pulse studied in class, which has the following definition: x(t) = 1 for -0.5 sec <= t < 0.5 sec = 0 elsewhere We would like to represent this continuous-time signal in MATLAB. However, digital computers in general (and MATLAB in particular) don't have the ability to cope with continuous-time signals... they can only work on samples of the signal. Thus, we must sample the signal to make it discrete-time. The first thing we must decide is what sample frequency we want to use. A high sample frequency
gives a more faithful reproduction of the signal but requires more computer memory to store it (and also more CPU cycles to process it). For our purposes, a sample frequency of f_s = 100 Hz is sufficient. Begin by entering the sample frequency into MATLAB: f_s = 100 If you want your rectangular pulses to have sharper edges, try using f_s = 1000 (although this will make the convolution operation much slower). Another important parameter is the sample period, which is just T_s = 1/f_s, and entered into MATLAB using: T_s = 1/f_s Another thing to consider is that MATLAB can only handle a finite number of samples at a time. Thus we must restrict the time axis to a limited range. I suggest that we only consider the signal in the range -5<= t <= 5 seconds. Now we must create a time axis. This will be a vector containing all the sample instants beginning at t = -5 sec and continuing to t = 5sec. These sample instants are all T_s seconds apart. The time axis is created in MATLAB as follows: t = [-5:T_s:5] The above command generates a vector containing all integer multiples of T_s = 0.01 between -5 and +5 (i.e. -5.00, -4.99, -4.98,...,4.99, 5.00). If you don't want MATLAB to echo this long vector onto the screen, simply terminate the command with a semicolon: t = [-5:T_s:5]; Now we must generate our rectangular pulse. While MATLAB has built in functions to generate a wide range of signals (such as cos, sin, and exp) it does not have a function to create the rectangular pulse. Thus, one has been created for you. Build the rect.m function from the appendix into your working directory (to find out which directory you are in type pwd and you can use cd to move to a different directory). To learn how to use the rect function type: help rect Also, you should open the file and read through the function. This will give you an idea about how to make your own functions, how to make a for loop, and how to do conditional branching with the if statement. You can open the file by using: edit rect Now let's generate a rectangular pulse: x1 = rect(t); Once it is generated we would like to plot it: plot(t,x1);
Note that you always want your time axis to come first. A problem with the plot is that the y-axis only shows values from 0 to 1, and thus you really can't see the pulse very clearly. You can change the field of view of the plot by using the axis command: The first two numbers specify the starting and ending times of the plot (the x-axis), while the last two numbers specify the range of the signal values (y-axis). A plot without labels doesn't give us much information, so next we need to label the axis. You do that as follows: ylabel( 'x_1(t)' ) Finally, let's put a title on the plot: title ('Plot 1: A rectangular pulse') Turn in a copy of this plot with your report. Don t need to include the code. Part 2: Elementary signal operations Now let's try to do some elementary signal operations, such as time-delay, time-scaling, and time-reversal. First let's create and plot the time-delayed signal x2(t) = rect(t-1) x2 = rect(t-1); plot(t,x2) Now let's try to make the time-scaled signal x3(t) = rect(t/2) x3 = rect(t/2); plot(t,x3) We can create a more interesting signal x4(t) = rect(t)+(1/2)rect(t-1) ( Finish the code by yourself similar to the above code) Now let's perform a time-reversal on this signal by forming the new signal x5(t) = x4(-t) = rect(-t)+(1/2)rect(-t-1) Finally, let's create the signal x6(t) = x4(1-t) = rect(1-t)+(1/2)rect(-t) Now, instead of turning in 5 new plots, let's try to save some space by putting all these plots on the same page. You can do that with the subplot command. Enter the following into MATLAB to get a single plot with the proper labels (you might want to cut and paste this): subplot(3,2,1) plot(t,x1)
ylabel('x_1(t) = rect(t)') subplot(3,2,3) plot(t,x2) ylabel('x_2(t) = x_1(t-1)') subplot(3,2,5) plot(t,x3) ylabel('x_3(t) = x_1(t/2)') subplot(3,2,2) plot(t,x4) ylabel('x_4(t)') subplot(3,2,4) plot(t,x5) ylabel('x_5(t) = x_4(-t)') subplot(3,2,6) plot(t,x6) ylabel('x_6(t) = x_4(1-t)') You might need to maximize the plot to its fullest size (otherwise you may not be able to read the x-axis label). Turn in this plot with you report. Although you need to write code to generate the x4(t),x5(t) and x6(t), don t include all the code in your report. Part 3: Convolution We can use MATLAB to approximate continuous-time convolution by using the conv function. Take for example, the convolution of two rectangular pulses (using the same time axis t and pulse x1 from part 1):
y = conv(x1,x1); Now try plotting y (first use the close statement to close the plot that's already displayed): close all plot ( t, y) Notice what just happened? MATLAB complained because the size of y is now different than the size of t. Check the size of y and t: length(y) length(t) You will notice that length(y) = 2*length(t)-1. What has happened is that MATLAB has given you the signal y(t) over the range -10 sec <= t <= 10 sec, instead of the range -5 sec <= t <= 5 sec used by x1(t). Thus, we need to create a separate time axis for the signal y: t_y = -10:T_s:10; Now we can plot: plot( t_y, y) Now what's wrong with this plot? As expected, we got a triangular pulse. However, the peak of this triangle is f_s = 1000... but it should have been at 1. This is because the discrete-time convolution used by MATLAB is slightly different from the continuous-time convolution we have studied in class. In general, when you use MATLAB to perform a continuous-time convolution, the result will be off by a factor of the sample frequency f_s. We can account for this discrepancy as follows: y1 = T_s*conv(x1,x1); Now our plot gives the correct answer: plot(t_y, y1) axis( [-2 2-1 2] ) xlabel( 'time (sec)') ylabel('y_1(t)') title('figure 3: y_1(t) = x_1(t)*x_1(t)') Turn in this plot with your report. Don t include the code. Problems (turn in all the codes and plots with your lab report): 1) Here you will perform convolution on discrete time signals using MATLAB: (1) x(n) = { 1,2,4 }, h(n) = {1,1,1,1,1} (2) x(n) = { 1,2,3,4,5 }, h(n) = {1} (3) x(n) = { 1,2,0,2,1}, h(n) = x(n). Use the Matlab function conv. For each set of signals, plot x(n), h(n) and y(n) as subplots in the same figure.
2) Consider a system with an impulse response Determine the input x (n) that will generate the output sequence y(n) = {1, 2, 2.5, 3, 3, 3, 2, 1,0...}. Plot x (n), y (n) and h (n) in one figure. Hint: Use the MATLAB function deconv. 3) Determine the cross-correlation sequence r r xy (n) of the sequences x (n) = {..., 0, 0, 2, -1, 3, 7, 1, 2, -3, 0, 0,...} y(n) = {..., 0, 0, 1, -1, 2, -2, 4, 1, -2, 5, 0, 0,...} Plot x(n), y(n) and r xy (n) in one figure. Hint: Use the MATLAB function xcorr.
Appendix function x = rect( t ) RECT rectangular pulse Usage: x = rect(t) This functions takes in a vector t of sample instants and outputs the corresponding rectangular pulse contained in the function x Written by Matthew Valenti For EE 329 Spring 2002 determine the size of the input matrix [m,n] = size(t); make sure the input matrix is actually a vector check_vector = min(m,n); if (check_vector ~= 1) error( 'Input to rect must be a vector' ); end determine the dimensionality K of the input vector K = max(m,n); initialize the output to all zeros x = zeros(m,n); loop through the time vector and assign the output to be equal to one whenever -0.5 <= t < 0.5 for i=1:k if ( ( t(i) >= -0.5)&( t(i) < 0.5) ) x(i) = 1; end end all done... exit function return