Department of Electrical Engineering
EE-330 Digital Signal Processing
Lab2: Complex Exponentials and Sinusoids
Lab2: Complex Exponentials and Sinusoids
Objectives
The goal of this Part is to gain familiarity with complex numbers and their use in representing sinusoidal signals
such as x (t )= Acos ( ωt +∅ ) as complex exponentials z (t )= A e jθ e jωt . The key is to use the appropriate complex
amplitude together with the real part operator as follows:
jθ jωt
x (t )= Acos ( ωt +∅ )=Real { A e e }
How to work with Complex Numbers in MATLAB
Familiarization with Matalb Function and commands for Complex Exponentials
Sinusoid Addition Using Complex Exponentials
Lab Instructions
The students should perform and demonstrate each lab task separately for step-wise evaluation
Each group shall submit one lab report on LMS within 5 days after lab is conducted. Lab report
submitted via email will not be graded.
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
2 Introduction to Complex Exponentials
2.1 Introduction
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.1 Complex Numbers in MATLAB
Here are some of MATLAB’s built-in complex number operators:
conj Complex conjugate
abs Magnitude
angle Angle (or phase) in radians
real Real part
imag Imaginary part
i,j pre-defined as √ −1
x = 3 + 4i , i suffix defines imaginary constant (same for j suffix)
exp(j*theta) Function for the complex exponential e jθ
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.
2.1.2 Sinusoid Addition Using Complex Exponentials
Recall that sinusoids may be expressed as the real part of a complex exponential:
jθ 2 jπ f 0 t
x (t )= Acos ( 2 π f o t+ ∅ ) =Real { A e e } (1)
The Phasor Addition Rule shows how to add several sinusoids:
N
x (t )=∑ A k cos ( 2 π f o t+∅ ) (2)
k=1
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
j∅
X k = Ak e k
(3)
Then the complex amplitude of the sum is
N
X s=∑ X k=¿ A s e j ∅ ¿ s
(4)
k=1
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 s cos ( 2 π f o t+∅ s ) (5)
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.
2.1.3 Harmonic Sinusoids
There is an important extension where x(t) is the sum of N cosine waves whose frequencies (f k) are
different. If we concentrate on the case where the fk are all multiples of one basic frequency f0.
fk = k f0 (HARMONIC FREQUENCIES),
Then the sum of N cosine waves given by (2) becomes:
N
x h (t )= A k cos ( 2 π f o t+∅ k ) =Real {∑ X k e j 2 πk f t }
0
(6)
k =1
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.
2.1.4 Sinusoid in Matlab
Following is matlab function create sinusoid if frequency ff and duration dur.
The corrected function should look something like:
function [xx,tt] = goodcos(ff,dur)
tt = 0:1/(100*ff):dur; %-- gives 100 samples per period
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.
2.2 Lab Tasks
2.2.1 Complex Exponentials
In the Previous lab, you learned how to write M-files. In this section, you will write two functions that
can generate sinusoids or sums of sinusoids.
2.2.2 Lab Task 1:
2.2.2.1 M-file to generate a Sinusoid
Write a function that will generate a single sinusoid, x (t )= Acos ( ωt +∅ )by using four input arguments:
amplitude (A), frequency (ω), 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 previous Lab as a starting point. Demonstrate that your one_cos() function
works by plotting the output for the following parameters: A = 95, ω = 200 rad/sec, ɸ = π/5 radians, and
dur=0.025 seconds. Be prepared to explain to the lab instructor features on the plot that indicates how the
plot has the correct period and phase. What is the expected period in millisecond?
2.2.2.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 (fk) to be
j∅
different. The following expressions are equivalent if we define the complex amplitudes X k = Ak e k
N
x (t )=Real {∑ X k e j2 π f t } (7)
k
k=1
.
N
x (t )=∑ A k cos ( 2 π f k t+∅ k ) (8)
k=1
2.2.3 Lab task 2:
2.2.3.1 Write the Function M-file
Write an M-file called syn sin.m that will synthesize a waveform in the form of (7).Although for loops are
rather inefficient in MATLAB but you must write the function with one loop in this lab. The first few
statements of the M-file are the comment lines—they should look like:
function [xx,tt] = syn_sin(fk, Xk, fs, dur, tstart)
%SYN_SIN Function to synthesize a sum of cosine waves
% usage:
% [xx,tt] = syn_sin(fk, Xk, fs, dur, tstart)
% fk = vector of frequencies
% (these could be negative or positive)
% Xk = vector of complex amplitudes: Amp*eˆ(j*phase)
% fs = the number of samples per second for the time axis
% dur = total time duration of the signal
% tstart = starting time (default is zero, if you make this input optional)
% xx = vector of sinusoidal values
% tt = vector of times, for the time axis
% Note: fk and Xk must be the same length.
% Xk(1) corresponds to frequency fk(1),
% Xk(2) corresponds to frequency fk(2), etc.
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.
2.2.4 Lab task 3: Test the function you created in task 2
In order to use this M-file to synthesize harmonic waveforms, you must choose the entries in the
frequency vector to be 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?
2.2.5 Lab Task 4:
2.2.5.1 Representation of Sinusoids with Complex Exponentials
(a) Generate the signal
and make a plot versus t. Use the syn_sin function and take a range for t that will cover three periods
starting at t = −0.5 secs. Include the MATLAB code with your report.
(b) From the plot of x(t) versus t, measure the frequency, phase and amplitude of the sinusoidal signal by
hand. Show annotations on the plots to indicate how these measurements were made and what the values
are.
(c) Use the phasor addition theorem and MATLAB to determine the magnitude and phase of x(t).