0% found this document useful (0 votes)
44 views

DSP Lab2

The document discusses a digital signal processing lab on complex exponentials and sinusoids. It provides objectives, an introduction and outlines 4 tasks - writing an M-file to generate a sinusoid, writing an M-file to synthesize waveforms, testing the function created, and representing sinusoids with complex exponentials.

Uploaded by

Maryam Shafiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

DSP Lab2

The document discusses a digital signal processing lab on complex exponentials and sinusoids. It provides objectives, an introduction and outlines 4 tasks - writing an M-file to generate a sinusoid, writing an M-file to synthesize waveforms, testing the function created, and representing sinusoids with complex exponentials.

Uploaded by

Maryam Shafiq
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

DEPARTMENT OF ELECTRICAL ENGINEERING

Faculty Member: Arbab Latif Dated: Feb 20, 23

Course/Section: BEE 12B Semester: Spring Semester

EE-330 Digital Signal Processing

Lab2: Complex Exponentials and Sinusoids

PLO4-CLO4 PLO5- PLO8- PLO9-


CLO5 CLO6 CLO7

Name Reg. No Viva / Analysis Modern Ethics Individua


Quiz / of data Tool and l and
Lab in Lab Usage Safety Team
Performa Report Work
nce

5 Marks 5 Marks 5 Marks 5 Marks 5 Marks

Yusra Ahmed 341836

Rakhmeen Gul 344190

Maryam Shafiq 352203

1
Lab2
Complex Exponentials and Sinusoids
Objectives:
The goal of this Part is to:

• Gain familiarity with complex numbers.


• Learn use in representing sinusoidal signals such as 𝒙(𝒕) = 𝑨𝒄𝒐𝒔(𝝎𝒕 + ∅) As complex
exponentials 𝒛(𝒕) = 𝑨𝒆𝒋𝜽 𝒆𝒋𝝎𝒕 .
• Use the appropriate complex amplitude together with the real part operator as follows:
o 𝒙(𝒕) = 𝑨𝒄𝒐𝒔(𝝎𝒕 + ∅) = 𝑹𝒆𝒂𝒍{𝑨𝒆𝒋𝜽 𝒆𝒋𝝎𝒕 }
• Work with Complex Numbers in MATLAB
• Familiarization with MATLAB Function and commands for Complex Exponentials
• Sinusoid Addition Using Complex Exponentials

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.

Lab Tasks
Lab Task 1: M-file to generate a Sinusoid.
Write a function that will generate a single sinusoid,𝑥(𝑡) = 𝐴𝑐𝑜𝑠(𝜔𝑡 + ∅) 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?

Code:
function main()
amplitude=95;

2
w = 200;
phase = pi/5;
d = 0.025;
[x,t]=one_cos(amplitude, w, phase, d);
plot(t,x,'b')
title('one_cos function')
end

Output:

Lab task 2: 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)

3
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 defines the number of samples per second for the cosine generation; in other
words, we are no longer constrained to using 20 samples per period.

Code:

function [x_t, t] = syn_sin(fk, Xk, fs, dur, tstart)


% syn_sin: Function to synthesize a sum of cosine waves
% If tstart is not specified, default tstart = 0
if margin < 5
tstart = 0;
end

t = linspace(tstart, dur - abs(tstart), fs);

assert(length(fk) == length(Xk), "Input fk and Xk must " + ...


"have the same length.")

x_t = 0;

for k = 1:length(fk)
x_t = x_t + real(Xk(k) * exp(2 * pi * 1i * fk(k) * t));
end

end

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?
Code:
t = 0:0.0001:0.1;
a = 10*cos(2*pi*0*t + 0) + 14*cos(2*pi*100*t + (-pi/3)) + 8*cos(2*pi*250*t + (pi/2));
plot(a);
title('Sum of Three Sinusoids');
xlabel('t(s)');
ylabel('x(t)');

Output:

4
Lab Task 4: 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.

Code:
t=-0.5:0.1:2;
y = (2*exp(1j*pi*t))+(2*exp(1j*pi*(t-1.25)))+((1-1j)*exp(1j*pi*t));
x=real(y);
plot(x);

5
(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.

Here, from figure:


Frequency = 0.5 Hz
Phase = phase is 1.25*(pi/180) = 0.007pi
Amplitude is 1.6

(c) Use the phasor addition theorem and MATLAB to determine the magnitude and phase of x(t).

Code:
x1 = real(2*exp(1j*0));
x2 = real(2*exp(1j*0));
x3 = real(1.41421*exp(-1j*pi/4));
x4 = x1 + x2 + x3;
magx4 = abs(x4)
thetax4 = angle(x4)
thetarad = thetax4 * 180/pi

6
Conclusion:
From this lab, we learnt about complex numbers and their representation. We worked with
MATLAB and learnt about its function and commands. Moreover, it cleared our concept of
complex exponential and sinusoidal functions through visual representation.

You might also like