Digital Signal Processing Laboratory 1: Discrete Time Signals with MATLAB Thursday, 23 September 2010 No PreLab is Required Objective: In this laboratory you will review the basics of MATLAB as a tool for computation and visualization by using it to create sampled signals. You will learn the units for and relationships between sampling intervals, sampling frequency, number of samples observed, total time observed, signal frequency, and normalized frequency. Background notes: A continuous signal defined mathematically can be "sampled" by simply evaluating it at integer multiples of a sampling interval, T. The corresponding sampling frequency is F T = 1/T. If T is the sampling interval in seconds per sample, then F T is the sampling frequency in Hertz ( i.e. samples per second). An analog sinusoidal signal with frequency f 0 in Hertz (ie cycles per second) is given by x a (t) = A cos(2πf 0 t + φ). The discrete time representation is given by x[n] = A cos(2πf 0 (nt)+ φ ) = A cos(2π(f 0 T)n+ φ ) = A cos(2π(f 0 /F T )n+ φ ) = A cos(2π(ν )n+ φ ) where ν= (f 0 /F T ) cycles per sample. For sinusoidal signals we will often use the normalized frequency, ν. If f 0 cycles per second is the actual frequency of the sinusoid to be sampled, the normalized frequency, ν, is the ratio of the actual frequency to the sampling frequency, ν =f 0 /F T. Unit analysis shows that the units of the normalized frequency are ( cycles per second)/(samples/second) = cycles per sample. The value of the normalized frequency, ν, should be less than 0.5 for accurate representation of the continuous sinusoidal signal by the discrete time sampled signal. MATLAB can be used to easily create and display discrete time signals. There are several ways to generate MATLAB statements to be executed. MATLAB can be used interactively, like a calculator. Type statements in response to the >> prompt and see the result of the statement immediately after it is typed. The arrow keys allow previous statements to be executed again or edited. However, this can be tedious if many statements need to be repeated.
An m-file can be a MATLAB script that contains a list of statements. The m-file is executed by simply typing the file name in response to the prompt in the interactive mode. This has the same effect as typing all the individual m-file statements in the interactive mode. Used in this way, the m-file is not a function. The statements in the m-file have access to variables in the workspace that were defined before the m-file is used. After the m-file instructions are complete, the interactive instructions that follow it in the command window or other m-files have access to all data created by the m-file instructions. This laboratory has four parts. Follow the instructions for each part and create the files and plots as required. Laboratory - Part 1 - Interactive creation of vectors and plots MATLAB represents a discrete time signal as an array or vector. Many operations are available for point by point operations on a vector so that explicit "for loops" are rarely needed. Start MATLAB on your workstation. Type the statements listed in the left column of the table below and observe the responses. Then answer the associated questions. Note that semicolons are not placed at the end of these statements, so the result of each statement will be displayed in the command window. This is sometimes useful for short vectors, but should be avoided for larger data vectors. test1=0:6 test1(4) Statements to type Questions...Answers... How long is the vector test1? (Look in the workspace window or type "whos" in response to the >> prompt. Is element 4 of vector test1 equal to 4? Why or why not? powv=2.^test1 cosv=cos(2*pi*(1/6)*test1) expv1=exp(2*pi*(1/6)*test1) Explain each of the four vectors created with these expv2=exp(2*pi*j*(1/6)*test1) four statements. How is expv1 different from expv2? It is easier to see the structure of a signal from a plot than from reading the list of numerical values. plot(expv1) What does this plot show? What is the horizontal axis?
plot(expv2) plot( test1, real(expv2), test1, imag(expv2) ) What does this plot show? Explain it in terms of the values displayed when it was created. How is this plot different from the previous one? How is this horizontal axis different from the axis for the plot of expv1? Laboratory - Part 2 - Sampled Sinusoids Interactive Mode statements: 1. Type these statements and observe the plots in the Figure window. (Note the semicolons at the end of each statement!!) Type "help plot" to find more information about plotting. Both plots are plotting the same set of sampled values with linear interpolation between the samples. Verify that the titles and scaling of each are correct. N1=2000; freq1=0.02; phase=0.0; svec1=0:n1; sig1=cos(2*pi*freq1*svec1+phase); whos subplot(2,1,1) plot(svec1,sig1) xlabel('sample number') title([ 'cosine at normalized frequency = ' num2str(freq1)... ' cycles/sample']) The... allows a statement to be continued on the next line subplot(2,1,2) Ts=.005; sample interval plot(svec1*ts, sig1) xlabel('time in seconds') title([ 'Cosine at frequency = ' num2str(freq1/ts)...
' Hz with Ts = ' num2str(1000*ts) ' ms']) 2. The interactive statements above will be put into two m-files so they can be used many times without retyping them all. The Input statement will allow you to change the value of parameters. Using the MATLAB editor, create and save the following text as an m-file named getcossig.m. Note that this m-file is not a function. N1=input('Enter number of samples: '); freq1=input('enter normalized frequency in cycles/sample: '); phase=0.0; svec1=0:(n1-1); sig1=cos(2*pi*freq1*svec1+phase); 3. Using the MATLAB editor, create and save the following text as an m-file named plotcossig.m. subplot(2,1,1) plot(svec1,sig1) xlabel('sample number') title([ 'cosine at normalized frequency = ' num2str(freq1) ' cycles/sample']) subplot(2,1,2) Ts=input('Enter sample time interval in seconds: ') plot(svec1*ts, sig1) xlabel('time in seconds') title([ 'Cosine at frequency = ' num2str(freq1/ts)... ' Hz with Ts = ' num2str(1000*ts) ' ms']) 4. Using the two mfiles, do the following. Reproduce the plots from step 1. How many statements do you need to type in the interactive mode? If you get error messages on the screen from either m-file, edit the m-file to correct the error and then run it again. After you have successfully reproduced the results from step 1, use the two m-files to make plots of the signals specified below. In each case answer the following questions: What is the sampling frequency? How many samples are there per cycle of the input signal? What is the time duration of one cycle on your plot? o For N1=1000, find the correct parameters to plot 10 seconds of a 2.5 Hz signal. o For Ts=0.002, find the correct parameters to plot 5 seconds of a 1.2 Hz signal. o o For f=0.1, find the correct parameters to plot 0.4 seconds of a 5 Hz signal. For N1=20, find the correct parameters to plot 0.4 seconds of a 55 Hz signal. Does this plot match its labels? Why or why not?
Laboratory - Part 3 - Sound Output Using getcossig, create sig1 with N1=8000 and ν =0.0625. Type help sound to learn how to make sound output. Listen to the output sound using sound(sig1,8000). Estimate the duration of the tone and a relative frequency. Is this consistent with F T = 8000Hz? Listen to the output sound using sound(sig1,4000). Compare the duration and frequency to the previous sound output. Is this consistent with F T = 4000Hz? Explain. Listen to the output sound using sound(sig1,12000). Compare the duration and frequency to the previous sound output. Is this consistent with F T = 12000Hz? Explain. What is the relationship of ν to the actual frequency of the tone you hear? How can the same sequence of data points make different frequency sounds? Listen to the output sound using sound(0.5*sig1,8000). Compare it to the first sound output you created. What is different? Why? Listen to the output sound using sound(5.0*sig1,8000). Compare it to the first sound output you created. What is different? Why? (You may want to reread the information about the sound function.) Laboratory - Part 4 Two Simple Filters Write an short m-file named myfilter3 that will implement a three coefficient weighted average filter as follows: Assume that x_in and b_coef vectors are already created before you use myfilter3, and that b_coef has only 3 values. Explicitly create delayed input vectors: x_n = [ x_in 0 0], x_nm1=[0 x_in 0 ], and x_nm2=[0 0 x_in ]. Create the filter output y_out = b_coef[1]* x_n + b_coef[2]*x_nm1 + b_coef[3]*x_nm2 Use your m-file to create the output for the coefficients and inputs specified below. Plot you re the input and output for each case. Set b_coef=[1/3 1/3 1/3] o Set x_in = [ zeros(1,10), ones(1, 20), zeros(1,10) ] o Set x_in = 0:39 o Set x_in = (-1).^(0:39) Set b_coef = [1/4-1/2 1/4]] o Set x_in = [ zeros(1,10), ones(1, 20), zeros(1,10) ] o Set x_in = 0:39 o Set x_in = (-1).^(0:39) Laboratory Report: For your laboratory report submit the answers to all questions in the laboratory procedure and submit the plots from Part 4.
(C) Sally Wood 2001-20010