lab2 dsp
lab2 dsp
Objectives
The goal of this Part 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
✓ 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.
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.
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.
𝑥ሺ𝑡ሻ = ∑𝑁
𝑘=1 𝐴𝑘 𝑐𝑜𝑠ሺ2𝜋𝑓𝑜 𝑡 + ∅ሻ (2)
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
𝑋𝑘 = 𝐴𝑘 𝑒 𝑗∅𝑘 (3)
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
𝑥ሺ𝑡ሻ = 𝐴𝑠 𝑐𝑜𝑠ሺ2𝜋𝑓𝑜 𝑡 + ∅𝑠 ሻ (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.
Harmonic Sinusoids
There is an important extension where x(t) is the sum of N cosine waves whose frequencies (fk) are
different. If we concentrate on the case where the fk are all multiples of one basic frequency 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.
Sinusoids in MatLab
Following is a MatLab function to create a sinusoid with frequency ff and duration dur.
2. Lab Tasks:
Complex Exponentials
In the previous lab, you learned how to write M-files. In this section, you will write functions that can
generate sinusoids or sums of sinusoids.
𝑥ሺ𝑡ሻ = 𝑅𝑒𝑎𝑙{∑𝑁
𝑘=1 𝑋𝑘 𝑒
𝑗2𝜋𝑓𝑘 𝑡
} (7)
.
𝑁
𝑥ሺ𝑡ሻ = ∑𝑘=1 𝐴𝑘 𝑐𝑜𝑠ሺ2𝜋𝑓𝑘 𝑡 + ∅𝑘 ሻ (8)
Write a function to synthesize a waveform in form of (7) or (8). The function must take as input a set of
amplitudes, a set of frequencies, a set of phase angles, sampling frequency, the start time and the total
duration. The function should compute the sum and output the time vector and the output of the sum. You
must call this function in a separate main function and then plot the resulting output. Although for loops
are inefficient, you will have to write one in this task. The start of the function looks like below:
You should provide error checking to ensure that the lengths of fk, Xk and phi_k 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.
tt = tstart:1/fs:dur;
xyz = zeros(size(tt));
for i = 1:length(Xk)
xyz = xyz + Xk(i) * exp(1i * 2 * pi * fk(i) * tt);
end
end
plot(tt,xx, 'b-')
xlabel('time')
ylabel('x(t)')
title('added sinosoids')
Output:
(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).
plot(tt,xx, 'b-')
xlabel('time')
ylabel('x(t)')
title('added sinosoids')
Output: