Question

Signal Processing First Lab 02: Introduction to Complex Exponentials - Multipath T Pre-Lab and Warm-Up: You should read at least the Pre-Lab and Warm-up sections of this lab assignment and

go over all exercises in the Pre-Lab section before going to your assigned lab session. Verification: The Warm-up section of each lab must be completed during your assigned Lab time and the steps marked Instructor Verification must also be signed off during the lab time. One of the laboratory instructors must verify the appropriate steps by signing on the Instructor Verification line. When you have completed a step that requires verification, simply demonstrate the step to the TA or instructor. Turn in the completed verification sheet to your TA when you leave the lab. Lab Report: It is only necessary to turn in a report on Sections 5 and 6 with graphs and explanations. You are asked to label the axes of your plots and include a title for every plot. In order to keep track of plots, include your plot inlined within your report. If you are unsure about what is expected, ask the TA who will grade your report. 1 Introduction The goal of this laboratory is to gain familiarity with complex numbers and their use in representing sinu- soidal signals such as x(t) = A cos(wt + ) as complex exponentials z(t) Aejejut. The key is to use the appropriate complex amplitude together with the real part operator as follows: = x(t) = A cos(wt + p) = Re{AeÏ¢µjwt} 2 Overview Manipulating sinusoidal functions using complex exponentials turns trigonometric problems into simple arithmetic and algebra. In this lab, we first review the complex exponential signal and the phasor addition property needed for adding cosine waves. Then we will use MATLAB to make plots of phasor diagrams that show the vector addition needed when adding sinusoids. 2.1 Complex Numbers in MATLAB SP First MATLAB can be used to compute complex-valued formulas and also to display the results as vector or “phasor” diagrams. For this purpose several new MATLAB functions have been written and are available on the SP First CD-ROM. Look for the "MATLAB Files" link just below the link for this lab. Make sure that this toolbox has been installed¹ by doing help on the new M-files: zvect, zcat, ucplot, zcoords, MATLAB and zprint. Each of these functions can plot (or print) several complex numbers at once, when the input is formed into a vector of complex numbers. For example, the following function call will plot five vectors all on one graph: Toolbox zvect ( [ 1+j, j, 3-4*j, exp (j*pi), exp (2j*pi/3)]) ¹Correct installation means that the spfirst directory will be on the MATLAB path. Try help path if you need more information. McClellan, Schafer, and Yoder, Signal Processing First, ISBN 0-13-065562-7. Prentice Hall, Upper Saddle River, NJ 07458. ©2003 Pearson Education, Inc. CD-ROM Here are some of MATLAB's built-in complex number operators: conj Complex conjugate abs Magnitude angle real imag i,j X = 3+4i exp (j*theta) Angle (or phase) in radians Real part Imaginary part pre-defined as √−1 i suffix defines imaginary constant (same for j suffix) Function for the complex exponential eje Each of these functions takes a vector (or matrix) as its input argument and operates on each element of the vector. Notice that the function names mag () and phase () do not exist in MATLAB.² When unsure about a command, use help. 2.2 Sinusoid Addition Using Complex Exponentials Recall that sinusoids may be expressed as the real part of a complex exponential: x(t) = A cos (2π fot + ¢) = Re { Ae³0, :{Aci 12π fot} Þej The Phasor Addition Rule shows how to add several sinusoids: N x(t) = Σ Ak cos (2π fot + Øk) k=1 T Then the complex amplitude of the sum is N Xs = ΣXk = Açe¹Øs k=1 assuming that each sinusoid in the sum has the same frequency, fo. This sum is difficult to simplify using trigonometric identities, but it reduces to an algebraic sum of complex numbers when solved using complex exponentials. If we represent each sinusoid with its complex amplitude X = Akelok (1) (2) McClellan, Schafer, and Yoder, Signal Processing First, ISBN 0-13-065562-7. Prentice Hall, Upper Saddle River, NJ 07458. ©2003 Pearson Education, Inc. (3) (4) Based on this complex number manipulation, the Phasor Addition Rule implies that the amplitude and phase of x (t) in equation (2) are As and Øs, so x(t) = A¸ cos(2π fot + Øs) (5) We see that the sum signal x(t) in (2) and (5) is a single sinusoid that still has the same frequency, fo, and it is periodic with period To = 1/fo. ²In the latest release of MATLAB a function called phase () is defined in a rarely used toolbox; it does more or less the same thing as angle () but also attempts to add multiples of 27 when processing a vector. 2 2.3 Harmonic Sinusoids There is an important extension where x(t) is the sum of N cosine waves whose frequencies (ƒÂ) are different. If we concentrate on the case where the (fr) are all multiples of one basic frequency fo, i.e., fk = k fo (HARMONIC FREQUENCIES) then the sum of N cosine waves given by (2) becomes T Pre-Lab N N xh(t) = Σ Ak cos(2πk fot + Øk) = Re { ΣXk eÏ 2πk fot 50% {£ X6 V²+Afut} k=1 k=1 This particular signal ä(t) has the property that it is also periodic with period To = 1/fo, because each of the cosines in the sum repeats with period To. The frequency fo is called the fundamental frequency, and To is called the fundamental period. (Unlike the single frequency case, there is no phasor addition theorem here to combine the harmonic sinusoids.) 3 Do all the exercises in this section before attending the regularly scheduled lab section. 3.1 Complex Numbers This section will test your understanding of complex numbers. Use z1 = 10e-j2/3 and z2 = −5+ j5 for all parts of this section. (6) (a) Enter the complex numbers z₁ and 22 in MATLAB. Plot them with zvect () and print them with zprint (). When unsure about a command, use help. Whenever you make a plot with zvect () or zcat (), it is helpful to provide axes for reference. An x-y axis and the unit circle can be superimposed on your zvect () plot by doing the following: hold on, zcoords, ucplot, hold off (b) The function zcat () can be used to plot vectors in a "head-to-tail" format. Execute the statement zcat ( [j,-1,-2j,1]); to see how zcat () works when its input is a vector of complex num- bers. (c) Compute z₁ + z2 and plot the sum using zvect (). Then use zcat () to plot z₁ and 22 as 2 vectors head-to-tail, thus illustrating the vector sum. Use hold on to put all 3 vectors on the same plot. If you want to see the numerical value of the sum, use zprint () to display it. 21 (d) Compute 2₁22 and plot the answer using zvect () to show how the angles of the angle of the product. Use zprint () to display the result numerically. and 22 McClellan, Schafer, and Yoder, Signal Processing First, ISBN 0-13-065562-7. Prentice Hall, Upper Saddle River, NJ 07458. ©2003 Pearson Education, Inc. determine (e) Compute 22/2₁ and plot the answers using zvect () to show how the angles of 2₁ and 22 determine the angle of the quotient. Use zprint () to display the result numerically. (f) Compute the conjugate z* for both z₁ and 22 and plot the results. In MATLAB, see help conj. Display the results numerically with zprint. 3 (g) Compute the inverse 1/2 for both z₁ and 22 and plot the results. Display the results numerically with zprint. (h) Make a 2 × 2 subplot that displays the following four plots in one figure window: (i) z₁ and 22 (ii) z† and on the same plot; (iii) 1/z₁ and 1/z2 on the same plot; and (iv) 2₁22. Add a unit circle and x-y axis to each plot for reference. 3.2 ZDrill There is a complex numbers drill program called: which uses a GUI to generate complex number problems and check your answers. Go to Appendix A demos to download and run. Please spend some time with this drill since it is very useful in helping you to get a feel for complex arithmetic. M = 200; for k=1:M 3.3 Vectorization The power of MATLAB comes from its matrix-vector syntax. In most cases, loops can be replaced with vector operations because functions such as exp () and cos () are defined for vector inputs, e.g., cos (vv) = [cos (vv (1)), cos (vv (2)), cos (vv (3)), cos (vv (N))] where vv is an N-element row vector. Vectorization can be used to simplify your code. If you have the following code that plots a certain signal, x (k) = k; y (k) = cos( 0.001*pi*x (k) *x (k) ); end plot ( x, Y, 'ro-' ) zdrill then you can replace the for loop and get the same result with 3 lines of code: M = 200; y = cos (0.001*pi* (1:M) .* (1:M) ); plot (1:M, y, ro-' ) T --- make a plot of a weird signal N = 200; for k=1: N xk(k) rk (k) = sig (k) = exp (j*2*pi*rk (k)); Use this vectorization idea to write 2 or 3 lines of code that will perform the same task as the following MATLAB script without using a for loop. (Note: there is a difference between the two operations xx*xx and xx. *xx when xx is a vector.) k/50; sqrt( xk(k)*xk(k) + 2.25 ); end plot( xk, real(sig), ‘mo-’ ) McClellan, Schafer, and Yoder, Signal Processing First, ISBN 0-13-065562-7. Prentice Hall, Upper Saddle River, NJ 07458. ©2003 Pearson Education, Inc. CD-ROM Appendix A Demos ZDrill 3.4 Functions Functions are a special type of M-file that can accept inputs (matrices and vectors) and also return outputs. The keyword function must appear as the first word in the ASCII file that defines the function, and the first line of the M-file defines how the function will pass input and output arguments. The file extension must be lower case "m" as in my_func.m. See Section B.5 in Appendix B for more discussion. The following function has a few mistakes. Before looking at the correct one below, try to find these mistakes (there are at least three): matlab mfile [xx,tt] = badcos (ff, dur) %BADCOS Function to generate a cosine wave 응 응 응 응 % usage: XX = badcos (ff, dur) ff = desired frequency in Hz dur= duration of the waveform in seconds. -- gives 100 samples per period tt = 0:1/(100*ff):dur; badcos = cos (2*pi*freeq*tt); The corrected function should look something like: function [xx,tt] = goodcos (ff, dur) 0:1/ (100* ff) : dur; %-- gives 100 samples per period tt = XX = cos (2*pi*ff*tt); Notice the word "function" in the first line. Also, "freeq” has not been defined before being used. Finally, the function has "xx" as an output and hence “xx” should appear in the left-hand side of at least one assignment line within the function body. The function name is not used to hold values produced in the function. 4 Warmup: Complex Exponentials In the Pre-Lab part of this lab, you learned how to write M-files. In this section, you will write two functions that can generate sinusoids or sums of sinusoids. T 4.1 M-file to Generate a Sinusoid = Write a function that will generate a single sinusoid, x(t) A cos(wt + o), by using four input arguments: amplitude (A), frequency (w), phase (ø) and duration (dur). The function should return two outputs: the values of the sinusoidal signal (x) and corresponding times (t) at which the sinusoid values are known. Make sure that the function generates 20 values of the sinusoid per period. Call this function one_cos (). Hint: use goodcos () from part (a) as a starting point. Demonstrate that your one_cos () function works by plotting the output for the following parameters: A = 95, w = 200 rad/sec, d = π/5 radians, and dur = 0.025 seconds. Be prepared to explain to the lab instructor features on the plot that indicate how the plot has the correct period and phase. What is the expected period in millisec? Instructor Verification (separate page) 4.2 Sinusoidal Synthesis with an M-file: Different Frequencies Since we will generate many functions that are a “sum of sinusoids," it will be convenient to have a function for this operation. To be general, we will allow the frequency of each component (f) to be different. The following expressions are equivalent if we define the complex amplitudes X₁ as X₁ = Akejøk ¸ x(t) = N De{Σ Xej2π] he√ 2 + fx! } k=1 McClellan, Schafer, and Yoder, Signal Processing First, ISBN 0-13-065562-7. Prentice Hall, Upper Saddle River, NJ 07458. ©2003 Pearson Education, Inc. (7) 5