Lab 3
Lab 3
Lab 3
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:
Lab Instructions
This lab activity comprises of three parts: Pre-lab, Lab Exercises, and Post-Lab Viva session.
The Pre-lab tasks should be completed before coming to the lab. The reports can be submitted in
PDF format on LMS.
The students should perform and demonstrate each lab task separately for step-wise evaluation
Only those tasks that completed during the allocated lab time will be credited to the students.
Students are however encouraged to practice on their own in spare time for enhancing their
skills.
Lab Report Instructions
All questions should be answered precisely to get maximum credit. Lab report must ensure following
items:
Lab objectives
MATLAB codes
Results (graphs/tables) duly commented and discussed
Conclusion
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.
Assuming that each sinusoid in the sum has the same frequency, f0. 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
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 T0. 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.
(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.
(g) Compute the inverse 1/z for both z1 and z2 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) z1 and z2 (ii) z1*
and z2* on the same plot; (iii) 1/z1 and 1/z2 on the same plot; and (iv) z1z2.
3.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.
3.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.
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,
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:
M = 200;
y = cos(0.001*pi*(1:M).*(1:M) );
plot(1:M, y, ‘ro-‘)
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.)
%--- make a plot of a weird signal
N = 200;
for k=1:N
xk(k) = k/50;
rk(k) = sqrt( xk(k)*xk(k) + 2.25 );
sig(k) = exp(j*2*pi*rk(k));
end
plot( xk, real(sig), ’mo-’)
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.
3.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://users.ece.gatech.edu/mcclella/SPFirst/Updates/SPFirstMATLAB.html