EGR3305-Lab-1-Fall 2023
EGR3305-Lab-1-Fall 2023
EGR3305-Lab-1-Fall 2023
FALL 2023
1
Signals and Systems Laboratory
Objectives:
Signals and Systems Labs introduce students to modeling and analysis of signals and
systems. The laboratory experiments are designed to provide students with a practical
experience to apply signals and systems theory introduced in lectures and tutorials. In this
regard, MATLAB and Simulink will be used to
1. Laptop
2. MATLAB and SIMULINK
2
Introduction:
MATLAB provides a variety of commands that allow controlling the flow of commands
in a program. The most common construct is the if-elseif-else structure. With these
commands, we can allow different blocks of code to be executed depending on some
condition. The format of this construct is
if condition1
command1
elseif condition2
command2
else
command3
end
Another common control flow construct is for ... end loop. It is simply an iteration loop
that tells the computer to repeat some task a given number of times. The format of a
for...end loop is
Scripts
MATLAB Scripts are implemented using a script file called an m-file (with an
extension .m), which is only a text file that contains each line of the file as though you
typed them at the command prompt. These scripts are created using MATLAB’s built-in
editor, which also provides for context-sensitive colors and indents for making fewer
mistakes and for easy reading. The script is executed by typing the name of the script at
the command prompt. The script file must be in the current directory on in the directory
of the path environment.
3
Elementary Sequences:
We use several elementary sequences in signals and systems for analysis purposes. Their
definitions and MATLAB representations are as follows:
4
MATLAB Functions
The second construct of creating a block of code is through subroutines. These are called
functions, which also allow us to extend the capabilities of MATLAB. In fact, a major
portion of MATLAB is assembled using function files in several categories and using
special collections called toolboxes. Functions are also m-files (with extension .m).
A major difference between script and function files is that the first executable line in a
function file begins with the keyword function followed by an output-input variable
declaration.
Plotting
One of the most powerful features of MATLAB for signal and data analysis is its
graphical data plotting. MATLAB provides several types of plots, starting with simple
two-dimensional (2D) graphs to complex, higher dimensional plots with full-color
capability. We will examine only the 2D plotting, which is the plotting of one vector
versus another in a 2D coordinate system. The basic plotting command is the plot(t, x)
command, which generates a plot of x values versus t values in a separate figure window.
The arrays t and x should be the same length and orientation. Optionally, some additional
formatting keywords can also be provided in the plot function. The commands xlabel and
ylabel are used to add text to the axis, and the command title is used to provide a title on
the top of the graph. When plotting data, one should get into the habit of always labeling
the axis and providing a title. Almost all aspects of a plot (style, size, color, etc.) can be
changed by appropriate commands embedded in the program.
The following set of commands creates a list of sample points, evaluates the sine function
at those points, and then generates a plot of a simple sinusoidal wave, putting axis labels
and title on the plot.
Example-1:
t = 0:0.01:2; % sample points from 0 to 2 in steps of 0.01
x = sin(2*pi*t); % Evaluate sin(2 pi t)
plot(t,x, 'b '); % Create plot with blue line
xlabel('t in sec '); ylabel('x(t) '); % Label axis
title('Plot of sin(2\pi t) '); % Title plot
5
For plotting a set of discrete numbers (or discrete-time signals), we will use the stem
command which displays data values as a stem, that is, a small circle at the end of a line
connecting it to the horizontal axis. The circle can be open (default) or filled (using the
option ’filled’). Using Handle Graphics (MATLAB’s extensive manipulation of graphics
primitives), we can resize circle markers. The following set of commands displays a
discrete-time sine function using these constructs.
Example-2:
n = 0:1:40; % sample index from 0 to 40
x = sin(0.1*pi*n); % Evaluate sin(0.2 pi n)
Hs = stem(n,x, 'b', 'filled'); % Stem-plot with handle Hs
set(Hs, 'markersize',4); % Change circle size
xlabel('n'); ylabel('x(n) '); % Label axis
title('Stem Plot of sin(0.2 \pi n) '); % Title plot
MATLAB provides an ability to display more than one graph in the same figure window.
By means of the hold on command, several graphs can be plotted on the same set of axes.
The hold off command stops the simultaneous plotting. The following MATLAB
fragment displays graphs in as one plot, depicting a “sampling” operation.
Another approach is to use the subplot command, which displays several graphs in each
individual set of axes arranged in a grid, using the parameters in the subplot command.
The following fragment displays graphs in as two separate plots in two rows.
Example-3:
clc;
t = 0:0.01:2; % sample points from 0 to 2 in steps of 0.01
x = sin(2*pi*t); % Evaluate sin(2 pi t)
subplot(2,1,1); % Two rows, one column, first plot
plot(t,x, 'b '); % Create plot with blue line
xlabel('t in sec '); ylabel('x(t) '); % Label axis
title('Plot of sin(2\pi t) '); % Title plot
hold on
n = 0:1:40; % sample index from 0 to 20
x = sin(0.1*pi*n); % Evaluate sin(0.2 pi n)
subplot(2,1,2); % Two rows, one column, second plot
Hs = stem(n,x, 'b', 'filled'); % Stem-plot with handle Hs
set(Hs, 'markersize',4); % Change circle size
6
xlabel('n'); ylabel('x(n) '); % Label axis
title('Stem Plot of sin(0.2 \pi n) '); % Title plot
hold off
The plotting environment provided by MATLAB is very rich in its complexity and
usefulness. Therefore, readers are strongly recommended to consult MATLAB’s manuals
on plotting. Many of these constructs will be used throughout this book. In this brief
review, we have barely made a dent in the enormous capabilities and functionalities in
MATLAB. Using its basic integrated help system, detailed help browser, and tutorials, it
is possible to acquire sufficient skills in MATLAB in a reasonable amount of time.
7
Purpose
The goal of this lab is to become familiar with computations and plotting using
MATLAB and Simulink software commands. You will investigate operations on signals
and delve into several elementary continuous and discrete-time signals. These signals are
essential for modeling numerous physical phenomena found in nature.
- Accessing MATLAB:
(1) Click on the Start button in the lower left portion of the screen.
(2) Select Programs -> Matlab -> MATLAB from the cascading menus. The MATLAB Command
Window will appear. This is the window in which you can enter commands.
Create an M-file
This activates the MATLAB “M-File Editor/Debugger” program. A separate window will appear in which
you can type your m-file. This is like a specialized “word processor” for writing MATLAB m-files. Note
that you now have two different windows to work in: the MATLAB command window and the M-file
Editor/Debugger window.
An m-file, or script file, is a simple text file where you can place Matlab commands. When the file is run,
Matlab reads the commands and executes them exactly as it would if you had typed each command
sequentially at the Matlab prompt. All m-file names must end with the extension '.m' (e.g. plot.m). If you
create a new m-file with the same name as an existing m-file, Matlab will choose the one which appears
first in the path order (help path for more information). To make life easier, choose a name for your m-file
which doesn't already exist. To see if a filename.m exists, type helps filename at the Matlab prompt.
8
Procedure-1
Answers-1
9
Procedure-2: Signal Operations
The primary objective of this problem is to delve into the foundational concepts of signal
processing, specifically focusing on the effects of time delays and scaling on signals.
By experimenting with various values of time shifts ( t0) and scaling factors (a), you will
gain a hands-on understanding of how these transformations impact the signal's shape
and behavior.
To achieve this understanding, you will utilize two MATLAB codes: input_signal.m,
which defines the signal, and signal_operations.m, which applies the transformations
and visualizes the results.
input_signal.m
function y = input_signal(t)
if t < -1 || t > 1
y = 0;
elseif t == 1
y = 1;
else
y = 0.5*t + 0.5;
end
end
signal_operations.m
a = 1; t0 = 0; % initial values
% Compute x as f(a*t+t0)
for k = 1:L
x(k) = input_signal(a*T(k) + t0);
end
10
figure;
plot(T, x);
xlabel('Time');
ylabel('Amplitude');
axis([-5 5 -3 3 ])
grid on;
1. Setup:
o Ensure both input_signal.m and signal_operations.m are saved in the
same directory.
2. Experimentation:
o Modify the values of a and t0 in the signal_operations.m script to
study the effects of different time shifts and scaling factors on the signal.
o Recommended values to experiment with:
a=1, t0=0
a=1, t0=+2
a=1, t0=-2
a=-1, t0=0
a=-1, t0=2
a=-1, t0=-2
a=1/2, t0=0
a=2, t0=0
3. Visualization:
o After modifying the values of a and t0, run the signal_operations.m
script to visualize the transformed signal and analyse the obtained result.
11
Answers-2
12
Procedure-3
The primary objective of this experiment is to determine whether a given system is linear.
A system is deemed linear if it satisfies two fundamental conditions: homogeneity and
additivity. If the system does not meet even one of these conditions for any given input, it
is termed non-linear.
y(t) =2 x(t) + 1
In this equation, x(t) represents the input signal, while y(t) denotes the output signal.
System Setup:
o Amplitude: 1V,
o Frequency (rad/sec): 1, Phase: 0
o Sample time: 0
Drag a "Gain" block from the "Math Operations" library for the
multiplication operation.
Drag a "Sum" block from the "Math Operations" library for the addition
operation.
Connect these blocks in the following sequence: Sine Wave -> Gain ->
Sum.
Set the value of the Gain block to 3.
For the Sum block, configure one of its inputs to have a constant value of
1.
13
Figure Q-3
4. Create a Subsystem: Highlight all the blocks, right-click, and select "Create
Subsystem."
Questions:
1. Using the Simulink model of the system, determine if the system is homogeneous.
Provide justification for your answer.
2. Evaluate if the system exhibits additivity. Elaborate on your response with
appropriate reasoning.
Answers-3
14
Procedure-4
y(t)=x(t) × sin(2πt)
where y(t) is the output signal, x(t) is the baseband input signal, and sin(2πt) is the sine
wave with a frequency of 1.
For the time-invariance test, the system's output is delayed by τ and the input is also
delayed by τ. The system is considered time-invariant if the outputs from both these
setups match. In this case, they did not, leading to the conclusion that the system is time
varying.
System Setup:
15
A constant block, set to the value of 3, feeds into the delay block to
establish the delay.
o The delayed output is then visualized using a scope.
Figure Q-4
Create a Subsystem: Select the appropriate blocks to create the mean Subsystem
and the delay Subsystem.
Question:
Using the Simulink model of the system, determine if the system is time-invariance.
Provide justification for your answer.
Answers-4
16