Laboratory 1 Discrete and Continuous-Time Signals

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8
At a glance
Powered by AI
The key takeaways are that this lab covers continuous and discrete time signals, sampling, and using Matlab for signal processing tasks.

The purpose of this lab is to illustrate the properties of continuous and discrete-time signals using digital computers and the Matlab software environment.

A continuous-time signal takes on a value at every point in time, whereas a discrete-time signal is only defined at integer values of the “time” variable. However, while discrete-time signals can be easily stored and processed on a computer, it is impossible to store the values of a continuous-time signal for all points along a segment of the real line.

EE325 Digital Signal Processing

LABORATORY 1: DISCRETE AND CONTINUOUS-TIME SIGNALS


1 INTRODUCTION
The purpose of this lab is to illustrate the properties of continuous and discrete-time signals using digital computers and the Matlab software environment. A continuous-time signal takes on a value at every point in time, whereas a discrete-time signal is only defined at integer values of the time variable. However, while discrete-time signals can be easily stored and processed on a computer, it is impossible to store the values of a continuous-time signal for all points along a segment of the real line. Digital computers are actually restricted to the storage of quantized discrete-time signals. Such signals are appropriately known as digital signals. How then do we process continuous-time signals? In this lab, we will show that continuous-time signals may be processed by first approximating them by discrete-time signals using a process known as sampling. We will see that proper selection of the spacing between samples is crucial for an efficient and accurate approximation of a continuous-time signal. Excessively close spacing will lead to too much data, whereas excessively distant spacing will lead to a poor approximation of the continuous-time signal. Sampling will be an important topic in future labs, but for now we will use sampling to approximately compute some simple attributes of both real and synthetic signals. Note: Please note that additional help on some topics and some files required to do this lab are provided in the course webpage under the directory Laboratary_1_Discrete_and_Continuoustime_Signals. You are encouraged to read these additional help files before coming to the lab. INLAB REPORT: The report must be in a form similar to a MS Word file with the figures and matlab codes copied and pasted from Matlab. The answers to discussion questions should also be provided on the same file in the relevant section. Hint: You can copy a figure to the clipboard from a Matlab figure window by selecting Copy figure option in Edit menu. You can then Paste it on a Word document for submission. To include matlab code, you can select the code you want, and copy and paste on the Word document. Make sure to save your work at regular intervals to avoid loss of work due to unforeseeable reasons.

This lab was adapted from Lab1 of ECE438 Digital Signal Processing with Applications course at Purdue University, USA.

EE325 Digital Signal Processing

2 MATLAB REVIEW
Practically all lab tasks in the EE325 lab will be performed using Matlab. Matlab (MATrix LABoratory) is a technical computing environment for numerical analysis, matrix computation, signal processing, and graphics. In this section, we will review some of its basic functions.

2.1 STARTING MATLAB AND GETTING HELP


You can start Matlab on your workstation by typing the command matlab in a command window (in Linux) or by clicking on the matlab menu option from the programs menu. After starting up, you will get a Matlab window. To get help on any specific command, such as plot, you can type the following help plot in the Command Window portion of the Matlab window. You can do a keyword search for commands related to a topic by using the following lookfor topic You can get an interactive help window using the function helpdesk or by following the Help menu near the top of the Matlab window.

2.2 MATRICES AND OPERATIONS


Every element in Matlab is a matrix. So, for example, the Matlab command a = [1 2 3] creates a matrix named a with dimensions of 1 3. The variable a is stored in what is called the Matlab workspace. The operation b = a. stores the transpose of a into the vector b. In this case, b is a 3 1 vector. Since each element in Matlab is a matrix, the operation c = a*b This lab was adapted from Lab1 of ECE438 Digital Signal Processing with Applications course at Purdue University, USA.

EE325 Digital Signal Processing

computes the matrix product of a and b to generate a scalar value for c of 14 = 1 * 1 + 2 * 2 + 3 * 3. Often, you may want to apply an operation to each element of a vector. For example, you may want to square each value of a. In this case, you may use the following command. c = a.*a The dot before the * tells Matlab that the multiplication should be applied to each corresponding element of a. Therefore the .* operation is not a matrix operation. The dot convention works with many other Matlab commands such as divide ./, and power .^. An error results if you try to perform element-wise operations on matrices that arent the same size. Note also that while the operation a. performs a transpose on the matrix a, the operation a performs a conjugate transpose on a (transposes the matrix and conjugates each number in the matrix).

2.3 MATLAB SCRIPTS AND FUNCTIONS


Matlab has two methods for saving sequences of commands as standard files. These two methods are called scripts and functions. Scripts execute a sequence of Matlab commands just as if you typed them directly into the Matlab command window. Functions differ from scripts because they take inputs and return outputs. A script-file is a text file with the filename extension .m. The file should contain a sequence of Matlab commands. The script-file can be run by typing its name at the Matlab prompt without the .m extension. This is equivalent to typing in the commands at the prompt. Within the script-file, you can access variables you defined earlier in Matlab. All variables in the script-file are global, i.e. after the execution of the script-file; you can access its variables at the Matlab prompt. To create a function call func, you first create a file called func.m. The first line of the file must be function output = func(input) where input designates the set of input variables, and output are your output variables. The rest of the function file then contains the desired operations. All variables in the function are local; that means the function cannot access Matlab workspace variables that you dont pass as inputs. After the execution of the function, you cannot access internal variables of the function.

3 CONTINUOUS-TIME VS. DISCRETE-TIME


The introduction in Section 1 mentioned the important issue of representing continuous-time signals on a computer. In the following sections, we will illustrate the process of sampling, and demonstrate the importance of the sampling interval to the precision of numerical computations. This lab was adapted from Lab1 of ECE438 Digital Signal Processing with Applications course at Purdue University, USA.

EE325 Digital Signal Processing

3.1 ANALYTICAL CALCULATION


PRELAB ACTIVITY (You must finish the following calculations and bring them to the lab when you come.): Compute these two integrals. Do the computations manually.

1 2

INLAB REPORT: Hand in your calculations of these two integrals. Show all work. (This can be hand written)

2.1 DISPLAYING CONTINUOUS-TIME AND DISCRETE-TIME SIGNALS IN MATLAB


Functions used: plot, stem, subplot (see help on these topics in Matlab) It is common to graph a discrete-time signal as dots in a Cartesian coordinate system. This can be done in the Matlab environment by using the stem command. We will also use the subplot command to put multiple plots on a single figure. Start Matlab on your workstation and type the following sequence of commands. n = 0:2:60; y=sin(n/6); subplot(3,1,1) stem(n,y) This plot shows the discrete-time signal formed by computing the values of the function sin(t/6) at points which are uniformly spaced at intervals of size 2. Notice that while sin(t/6) is a continuous-time function, the sampled version of the signal, sin(n/6), is a discrete-time function. A digital computer cannot store all points of a continuous-time signal since this would require an infinite amount of memory. It is, however, possible to plot a signal which looks like a continuous-time signal, by computing the value of the signal at closely spaced points in time, and then connecting the plotted points with lines. The Matlab plot function may be used to generate such plots. Use the following sequence of commands to generate two continuous-time plots of the signal sin(t/6). n1 = 0:2:60; z = sin(n1/6); subplot(3,1,2) plot(n1,z) n2 = 0:10:60; w = sin(n2/6); subplot(3,1,3) This lab was adapted from Lab1 of ECE438 Digital Signal Processing with Applications course at Purdue University, USA.

EE325 Digital Signal Processing

plot(n2,w) As you can see, it is important to have many points to make the signal appear smooth. But how many points are enough for numerical calculations? In the following sections we will examine the effect of the sampling interval on the accuracy of computations.

INLAB REPORT: Submit a soft copy of the plots of the discrete-time function and two continuous-time functions. Label them with the title command, and include your name. Comment on the accuracy of each of the continuous time plots.

2.2 NUMERICAL COMPUTATION OF CONTINUOUS-TIME SIGNALS


2.2.1 BACKGROUND ON NUMERICAL INTEGRATION
One common calculation on continuous-time signals is integration. Figure 1 illustrates a method used for computing the widely used Riemann integral. The Riemann integral approximates the area under a curve by breaking the region into many rectangles and summing their areas. Each rectangle is chosen to have the same width t, and the height of each rectangle is the value of the function at the start of the rectangles interval.

To see the effects of using a different number of points to represent a continuous-time signal, write a Matlab function for numerically computing the integral of the function sin2(5t) over the interval [0, 2]. The syntax of the function should be I=integ1(N) where I is the result and N is the number of rectangles used to approximate the integral. This function should use the sum command and it should not contain any for loops! This lab was adapted from Lab1 of ECE438 Digital Signal Processing with Applications course at Purdue University, USA.

EE325 Digital Signal Processing

Note: Since Matlab is an interpreted language, for loops are relatively slow. Therefore, we will avoid using loops whenever possible. Next write an m-file script that evaluates I(N) for 1 N 100, stores the result in a vector and plots the resulting vector as a function of N. This m-file script may contain for loops. Repeat this procedure for a second function J=integ2(N) which numerically computes the integral of exp(t) on the interval [0, 1]. INLAB REPORT: Submit plots of I(N) and J(N) versus N. Use the subplot command to put both plots on a single figure. Also submit your Matlab code for each function. Compare your results to the analytical solutions from Section 3.1. Explain why I(5) = I(10) = 0.

3 PROCESSING OF SPEECH SIGNALS


Download speech.au file from the course web page. Read how to load and play audio signals from the course web page. Digital signal processing is widely used in speech processing for applications ranging from speech compression and transmission, to speech recognition and speaker identification. This exercise will introduce the process of reading and manipulating a speech signal. First download the speech audio file speech.au (from course web page) to your working directory, and then do the following: 1. Use the auread command to load the file speech.au into Matlab. 2. Plot the signal on the screen as if it were a continuous-time signal (i.e. use the plot command). 3. Play the signal via the digital-to-analog converter in your workstation with the Matlab sound function. INLAB REPORT: Submit your plot of the speech signal.

4 ATTRIBUTES OF CONTINUOUS-TIME SIGNALS


Download signal1.p function. In this section you will practice writing .m-files to calculate the basic attributes of continuous-time signals. Download the function signal1.p. This is a pre-parsed pseudocode file (P-file), which is a precompiled form of the Matlab function signal1.m. To evaluate this function, simply type y = This lab was adapted from Lab1 of ECE438 Digital Signal Processing with Applications course at Purdue University, USA.

EE325 Digital Signal Processing

signal1(t) where t is a vector containing values of time. Note that this Matlab function is valid for any real-valued time, t, so y = signal1(t) yields samples of a continuous-time function. First plot the function using the plot command. Experiment with different values for the sampling period and the starting and ending times, and choose values that yield an accurate representation of the signal. Be sure to show the corresponding times in your plot using a command similar to plot(t,y). Next write individual Matlab functions to compute the minimum, maximum, and approximate energy of this particular signal. Each of these functions should just accept an input vector of times, t, and should call signal1(t) within the body of the function. You may use the built-in Matlab functions min and max. Again, you will need to experiment with the sampling period, and the starting and ending times so that your computations of the min, max, and energy are accurate. Remember the definition of the energy is = | 1 |

INLAB REPORT: Submit a plot of the function, and the computed values of the min, max, and energy. Explain your choice of the sampling period, and the starting and ending times. Also, submit the code for your energy function.

5 SPECIAL FUNCTIONS
Plot the following two continuous-time functions over the specified intervals. Write separate script files if you prefer. Use the subplot command to put both plots in a single figure, and be sure to label the time axes. for [10 , 10 ] for [2, 2]

Hint: The function rect(t) may be computed in Matlab by using a Boolean expression. For example, if t=-10:0.1:10, then y = rect(t) may be computed using the Matlab command y = (abs(t)<=0.5) Write an .m-script file to stem the following discrete-time function for a = 0.8, a = 1.0 and a = 1.5. Use the subplot command to put all three plots in a single figure. Issue the command orient(tall) just prior to printing to prevent crowding of the subplots.

This lab was adapted from Lab1 of ECE438 Digital Signal Processing with Applications course at Purdue University, USA.

EE325 Digital Signal Processing

Repeat this procedure for the function for

10

for

[20, 20]

and

[1, 10]

Hint: The unit step function y = u(n) may be computed in Matlab using the command y = (n>=0) , where n is a vector of values of time indices. INLAB REPORT: Submit all three figures, for a total of 8 plots. Also submit your Matlab .m-files.

6 SAMPLING
The word sampling refers to the conversion of a continuous-time signal into a discrete-time signal. The signal is converted by taking its value, or sample, at uniformly spaced points in time. The time between two consecutive samples is called the sampling period. For example, a sampling period of 0.1 seconds implies that the value of the signal is stored every 0.1 seconds. Consider the signal f(t) = sin(2t). We may form a discrete-time signal, x(n), by sampling this signal with a period of Ts. In this case, = = sin 2

Use the stem command to plot the function f(Tsn) defined above for the following values of Ts and n. Use the subplot command to put all the plots in a single figure, and scale the plots properly with the axis command.

1 2 3 4

= , 0 = , 0 =

, 0

30; 30; 9;

100;

[0,30, 1,1] [0,9, 1,1] [0,20, 1,1]

[0,100, 1,1]

, 0

INLAB REPORT: Submit a copy of the figure containing all four subplots. Discuss your results. How does the sampled version of the signal with Ts = 1/10 compare to those with Ts = 1/3, Ts = 1/2 and Ts = 10/9?

This lab was adapted from Lab1 of ECE438 Digital Signal Processing with Applications course at Purdue University, USA.

You might also like