Lab-02 Complex Numbers (DSP)
Lab-02 Complex Numbers (DSP)
OBJECTIVE:
To understand the complex numbers and visualize them in a complex plane.
INTRODUCTION:
Complex numbers: a combination of real and imaginary numbers, though they seem mysterious
and unreal, play a pivotal role in signal processing and circuit analysis in electrical engineering. A
complex number 𝑧 can be represented using the Cartesian form 𝑧 = 𝑎 + 𝑗𝑏 or the polar form
𝑧 = 𝑟𝑒 𝑗𝜃 . Conversion from one form to the other can be done through the following set of
equations.
2. Write a code that takes any real and imaginary part of the complex number from the user
using the input command and then displays the complex number and its conjugate,
absolute value and angle. You will not use built-in functions for finding these values and
no hard coding is allowed. The output after taking the inputs should be displayed like this:
3. The code below plots 4 random complex numbers in a complex plane where real and
imaginary numbers could be any integer value in the (-10,10) range.
clc % clears command window
clear all % clear workspace
close all % closes all previously opened figures
for i = 1:4
z = complex(randi([-10,10]),randi([-10,10])); % ???
plot(real(z),imag(z),'Color','r','Marker','o','MarkerSize',5,'LineWidth',3);
text(real(z),imag(z),num2str(z),'FontSize',20); % ???
hold on
end
% The below mentioned commands are used to improve the visual appearance of
% the figure for better understanding.
grid on;
axis square; % ???
box on; % ???
title('Argand Diagram')
axis([-10.5 10.5 -10.5 10.5]) % ???
xline(0,'b','Imaginary Axis','LineWidth',2,'LabelHorizontalAlignment','right')
% ???
yline(0,'k','Real Axis','LineWidth',2,'LabelVerticalAlignment','top') % ????
xticks(-10:10) % ???
yticks(-10:10) % ???
a = gca;
a.XRuler.TickLabelGapOffset = -220; % ???
a.YRuler.TickLabelGapOffset = -220; % ???
set(gcf,'units','normalized','outerposition',[0 0 1 1]) % ???
a. Copy the code in a new m-file and run the code. Run the code 2-3 times to
understand the representation of a complex number in a complex plane.
b. Explain different commands used in the code by writing comments.
c. Modify the code to generate 4 random complex numbers in 4 different quadrants.
4. Initialize the special complex number 𝑒 𝑗𝜃 in cartesian form, i.e., 𝑧 = 𝑐𝑜𝑠𝜃 + 𝑗𝑠𝑖𝑛𝜃. Use
the linspace command to produce sufficient points for 𝜃 in range [0, 2𝜋].
a. Visualize 𝑒 𝑗𝜃 and 𝑒 −𝑗𝜃 using the plot3 command. Are these 2D signals or 3D?
What is the difference between both in terms of visualization?
b. Copy the complete code below in your m-file and run the code for better
visualization of part-a. Use Rotate 3D button from the Tools menu to view all three
X-Y, X-Z and Y-Z 2D planes. Identify the functions in 2D planes and save them to
include in your report.
c. Modify the code to include one more cycle of 𝑒 𝑗𝜃 . Make sure to modify the z-axis
as well for proper visualization.
5. The code below uses the exp command to initialize the special complex number 𝑒 𝑗𝜃 in
polar form, i.e., 𝑧 = 𝑟𝑒 𝑗𝜃 , where 𝑟 = 1.
clc; clear all; close all
theta = 0;
r = 1;
z1 = r*exp(1j*theta);
plot(real(z1), imag(z1), 'ro','LineWidth',3);
% The below mentioned commands are used to improve the visual appearance of
% the figure for better understanding.
title('Plotting e^{j\theta} in polar representation')
grid on; axis square;
xlabel('\Re (e^{j\theta})','FontSize',20)
ylabel('\Im (e^{j\theta})','FontSize',20)
axis([-1.1 1.1 -1.1 1.1])
yline(0,'LineWidth',2)
xline(0,'LineWidth',2)
ax = gca;
ax.XAxis.FontSize = 12;
ax.YAxis.FontSize = 12;
set(gcf,'units','normalized','outerposition',[0 0 1 1])
a. Copy code in a new m-file and run the code. Visualize the value of 𝑒 𝑗𝜃 for 𝜃 = 0.
b. Update the code to display values of 𝑒 𝑗𝜃 for 𝜃 = 0𝑜 , 90𝑜 , 180𝑜 , 270𝑜 in a single
figure.
c. Update the code to display all values of 𝑒 𝑗𝜃 in a single figure for 𝜃 in range [0, 360]
with ∆𝜃 = 2.