Lab 4
Lab 4
Lab 4
Objectives
The goal of this laboratory is to gain familiarity with complex numbers and their use in representing sinusoidal
signals such as as complex exponentials . The key is to use the appropriate
complex amplitude together with the real part operator as follows:
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.
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
We see that the sum signal x(t) in (2) and (5) is a single sinusoid that still has the same frequency, f0, and it
is periodic with period T0 = 1/f0.
fk = k f0 (HARMONIC FREQUENCIES),
This particular signal xh(t) has the property that it is also periodic with period T0 = 1/f0, because each of the
cosines in the sum repeats with period T 0. The frequency f0 is called the fundamental frequency, and T0 is
called the fundamental period. (Unlike the single frequency case, there is no phasor addition theorem here
to combine the harmonic sinusoids). Do all the exercises in this section before attending the regularly
scheduled lab section.
(a) Enter the complex numbers z1 and z2 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
(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 numbers.
(c) Compute z1 + z2 and plot the sum using zvect(). Then use zcat() to plot z1 and z2 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.
(d) Compute z1z2 and plot the answer using zvect() to show how the angles of z1 and z2 determine the
angle of the product. Use zprint() to display the result numerically.
(e) Compute z2/z1 and plot the answers using zvect() to show how the angles of z1 and z2 determine the
angle of the quotient. Use zprint() to display the result numerically.
(f) Compute the conjugate z* for both z1 and z2 and plot the results. In MATLAB, see help conj. Display
the results numerically with zprint.
(g) Compute the inverse 1/z for both z1 and z2 and plot the results. Display the results numerically with
zprint.
1.1.6 ZDrill
There is a complex numbers drill program called:
zdrill
This 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.
1.1.7 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.
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,
M = 200;
for k=1:M
x(k) = k;
y(k) = cos(0.001*pi*x(k)*x(k) );
end
plot( x, y, ‘ro-‘)
then you can replace the for loop and get the same result with 3 lines of code:
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.)
1.1.8 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. The following function has a few mistakes. Before looking at the correct one below, try to
find these mistakes (there are at least three):
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.
The MATLAB syntax length(fk) returns the number of elements in the vector fk, so we do not need a separate
input argument for the number of frequencies. On the other hand, the programmer (that’s you) should provide
error checking to make sure that the lengths of fk and Xk are the same. See help error. Finally, notice that the
input fs define the number of samples per second for the cosine generation; in other words, we are no longer
constrained to using 20 samples per period. Include a copy of the MATLAB code with your lab report.
1.2.3.3 Testing
In order to use this M-file to synthesize harmonic waveforms, you must choose the entries in the frequency
vector tobbe integer multiples of some desired fundamental frequency. Try the following test and plot the result.
[xx0,tt0] = syn_sin([0,100,250],[10,14*exp(-j*pi/3),8*j],10000,0.1,0);
%-Period = ?
Measure the period of xx0 by hand. Then compare the period of xx0 to the periods of the three sinusoids that
make up xx0, and write an explanation on the verification sheet of why the period of xx0 is longer.
http://spfirst.gatech.edu/matlab/SPFirstMATLAB.html
http://dspfirst.gatech.edu/matlab/toolbox/