Signal & System: Laboratory Manual
Signal & System: Laboratory Manual
Laboratory Manual
Theory:
Introduction to MATLAB
MATLAB is a high-level technical computing language equipped with a user-friendly interface. Its
name stems from the words MATrix and LABoratory as it is based on the use of matrices.
MATLAB is an extremely powerful tool useful for scientists and engineers from various disciplines.
For example, MATLAB can be used in a wide range of applications, such as telecommunications,
signal and image processing, control, mathematics, financial modelling, bioengineering, aeronautics,
and many more.
M-Files
In order to write many commands that are executed all together, the program must be written in a text
editor. In this editor, one can type all the needed commands to form a program, save the program, and
execute it any time he or she wants. The text files are called M-files due to their suffice * .m.
There are two categories of M-files: the Scripts and the Functions.
Scripts
Scripts are the M-files with MATLAB commands. Their name must have a .m suffix. Scripts are
suitable for solving the problems that require many commands. The advantage of the scripts
is that they are implemented very easily.
Functions
Function are also M-files, That is, are files with extension .m and must be saved in the current
Directory of MATLAB. The difference between functions and scripts is that a function accepts one
or more input arguments and returns one or more output arguments. To declare that an M-file is
a function the first line of the m file must contain the syntax definition. More specifically, the first
line of the M-file must be of the form function[y1,y2,y3,…yn]=name{x1,x2,x3… xm}.
The variable y1,y2,…yn are the outputs of the function while x1,x2,…xm are the input arguments.
In case there is only one output, the square brackets are not necessary. The “name” specifies the
name of the function. In order to execute a function, first the M-File is saved in Current Directory.
Useful Commands
Here we will learn and practice useful (when working with vectors and matrices) commands. As
already discussed, the command sumreturns the sum of the elements of a vector. The command
cumsum returns a vector whose elements are the cumulative sum of the previous elements, the
command prodis the product of the vector elements, while the command diffreturns a vector in
Department of EEE, Premier University, Chittagong | Signal & System 2
which each element is given by its subtraction with the previous element. The command max
and min return the largest and smallest elements of the vector, respectively, as well as their index.
The command sortsorts the vector elements in ascending (by default) or descending order. The
command mean computes the mean value, while the command medianreturns the median value.
All these commands are suitable also for matrices by slightly changing their syntax.
Lab Tasks:
1. Write a script file and execute.
2. Write a function file and execute
Discussion:
Experiment no: 02
Theory:
Matrices
The MATLAB environment uses the term matrix to indicate a variable containing real or complex
numbers arranged in a two-dimensional grid. An array is, more generally, a vector, matrix, or higher
dimensional grid of numbers. All arrays in MATLAB are rectangular, in the sense that the component
vectors along any dimension are all the same length.
Addition and subtraction of matrices is defined just as it is for arrays, element by element. Adding A to
B, and then subtracting A from the result recovers B:
A = pascal(3);
B = magic(3);
X=A+B
X=
9 2 7
4 7 10
5 12 8
Y=X-A
Y=
8 1 6
3 5 7
4 9 2
A row vector and a column vector of the same length can be multiplied in either order. The result is
either a scalar, the inner product, or a matrix, the outer product:
u = [3; 1; 4];
v = [2 0 -1];
Department of EEE, Premier University, Chittagong | Signal & System 4
x = v*u
x=
X = u*v
X=
6 0 -3
2 0 -1
8 0 -4
x = v'
x=
-1
If x and y are both real column vectors, the product x*y is not defined, but the two products
x'*y
and
y'*x
are the same scalar. This quantity is used so frequently, it has three different names: inner product,
scalar product, or dot product.
Multiplying Matrices
MATLAB uses a single asterisk to denote matrix multiplication. The next two examples illustrate the
fact that matrix multiplication is not commutative; AB is usually not equal to BA:
X = A*B
X=
15 15 15
26 38 26
41 70 39
Y = B*A
Y=
15 28 47
15 34 60
15 28 43
A matrix can be multiplied on the right by a column vector and on the left by a row vector:
u = [3; 1; 4];
x = A*u
x=
17
30
v = [2 0 -1];
y = v*B
y=
12 -7 10
Generally accepted mathematical notation uses the capital letter I to denote identity matrices, matrices
of various sizes with ones on the main diagonal and zeros elsewhere. These matrices have the property
that AI = A and IA = A whenever the dimensions are compatible. The original version of MATLAB
could not use I for this purpose because it did not distinguish between uppercase and lowercase letters
and i already served as a subscript and as the complex unit. So an English language pun was
introduced. The function
eye(m,n)
returns an m-by-n rectangular identity matrix and eye(n) returns an n-by-n square identity matrix.
Lab Tasks:
1. Execute addition and subtraction of matrics
2. Execute multiplication of matrics
Discussion:
Objective: To understand the process of plotting & sub plotting of different signals in
MATLAB
Theory:
It is possible to plot more than one function in the name of figure by employing a different syntax
of command Plot.
Formatting a Figure
The command grid on adds lines to the graph, while the command grid off removes the grid lines.
Simply typing gridis switch between the two modes. Text besides the x-axis and y-axis can be
added using the commands xlabel and ylabel, respectively. A graph title is inserted by the
command title .
Lab Tasks:
1. Plot the following function y(x) = x^2 , -2 ≥ 0 ≥ 2
2. Plot the y(x) = x^2 cos(x), g(x) = xcos (x) and f(x) = 2^xsin (x), 0 ≤ x ≤ 2π in the same figure
3. Plot the discrete function f (n) = n^2, where -2 ≤ x ≤ 2
Discussion:
Theory:
Digital Signals :
Digital signals are the signals that both independent and dependent variables take values from a
discrete set.
In the following example, the signal y [n] = cos[n] is again plotted but we use the command round
to limit the set values that y[n] can take , that is , y[n] can be -1, 0 or 1
Lab Tasks:
1. Form gate signal
2. Plot triangular Signal with amplitude 10 frequency 50 Hz and for 2 cycles
3. Plot Rectangular Signal with amplitude 10&15 frequency 50Hz, duty cycle 60% and 40% and
for
2 cycles.
4. Plot Sinusoidal Signal with amplitude 10, frequency 50hz and for 2 cycles.
5. Plot the unit step function
6. Plot the ramp function
7. Plot the impulse function
Discussion
Theory:
Symbolic Variables
In MATLAB, a variable type is the symbolic variable (or object). A symbolic variable is defined by
the command sym and syms. The use of symbolic variables allows the computation of limits,
integrals, derivatives etc.
diff
Differentiate symbolic expression or function
Syntax
diff(F)
diff(F,var)
diff (F,n)
diff(F,var,n)
diff(F,n,var)
diff(F,var1,...,varN)
Description
diff(F) - differentiates F with respect to the variable determined by symvar.
diff(F,var) - differentiates F with respect to the variable var.
diff(F,n) - computes the nth derivative of F with respect to the variable determined by symvar.
diff(F,var,n)- computes the nth derivative of F with respect to the variable var. This syntax is
equivalent to diff(F,n,var).
diff(F,var1,...,varN) - differentiates F with respect to the variables var1,...,varN.
diff(diff(x*y))
In the first call, diff differentiate x*y with respect to x, and returns y. In the second call, diff differentiates
y with respect to y, and returns 1.
Thus, diff(x*y, 2) is equivalent to diff(x*y, x, x), and diff(diff(x*y)) is equivalent to diff(x*y, x, y).
Lab Tasks:
Find out first, second and third derivative of the following functions:
i. 𝑒 −𝑎𝑡
ii. 𝑒 −𝑎𝑡 sin ωt
iii. 𝑒 −𝑎𝑡 cos ωt
iv. cos ωt
v. sin ωt
vi. cos ωt sin ωt
vii. t^2
Discussion:
Theory:
Int
Definite and indefinite integrals
Syntax
int(expr,var)
int(expr,var,a,b)
int(___,Name,Value)
Description
int(expr,var) - computes the indefinite integral of expr with respect to the symbolic scalar variable
var. Specifying the variable var is optional. If you do not specify it, int uses the default variable
determined by symvar. If expr is a constant, then the default variable is x.
int(expr,var,a,b) - computes the definite integral of expr with respect to var from a to b. If you
do not specify it, int uses the default variable determined by symvar. If expr is a constant, then the
default variable is x.
int(expr,var,[a,b]), int(expr,var,[a b]), and int(expr,var,[a;b]) are equivalent to int(expr,var,a,b).
int(___,Name,Value) - uses additional options specified by one or more Name,Value pair
arguments.
Indefinite Integral of a Univariate Expression
Find an indefinite integral of this univariate expression:
syms x
int(-2*x/(1 + x^2)^2)
Discussion:
Theory:
Continuous-Time Convolution
The impulse response of a linear time-invariant system completely specifies the system. More specifically,
if the impulse response of a system is known one can compute the system output for any input signal. We
now present on of most important topics in signals and systems theory.
The response (or output) of a system to any input signal is computed by the convolution
Suppose that y(t) denotes the output of the system, x(t) is the input signal, and h(t) is the impulse response
of the system. The mathematical expression of the convolution relationship is
Where, the symbol * denotes convolution, and it must not be confused with the multiplication symbol. The
calculation of the convolution between two signals involves the computation of an integral. The complete
form of is
∞
∫−∞ 𝑥(𝜏) ℎ(𝑡 − 𝜏)𝑑𝜏
By alternation variables τ and t, becomes
∞
∫−∞ 𝑥(𝜏 − 𝑡) ℎ(𝜏)𝑑𝜏
The computational process followed in the previous lab is the analytical way of deriving the convolution
between two signals. In MATLAB, the command ‘conv’ allows the direct computation of the
convolution between two signals.
Lab Task:
A linear time-invariant system is described by the impulse response
Department of EEE, Premier University, Chittagong | Signal & System 16
ℎ(𝑡) = { 1−𝑡
0
0≤𝑡≤1
𝑒𝑙𝑠𝑒𝑤ℎ𝑒𝑟𝑒
If the response is y(t), calculate the response of the system to the input signal using convolution integral
𝑥(𝑡) = { 10 0≤𝑡≤2
𝑒𝑙𝑠𝑒𝑤ℎ𝑒𝑟𝑒
Discussion:
Experiment no: 08
Theory:
Fourier transform
The Fourier transform of the expression f = f(x) with respect to the variable x at the point w is
defined as follows
∞
𝐹(𝜔) = 𝑐 ∫−∞ 𝑓(𝑥)𝑒^𝑖𝑠𝑤𝑥 𝑑𝑥
Here, c and s are parameters of the Fourier transform. The fourier function uses c = 1, s = –1.
Syntax
fourier(f,trans_var,eval_point)
Description
Input Arguments
trans_var -Symbolic variable representing the transformation variable. This variable is often
called the "time variable" or the "space variable".
Default: The variable x. If f does not contain x, then the default variable is determined by symvar.
eval_point- Symbolic variable, expression, vector or matrix representing the evaluation point.
This variable is often called the "frequency variable".
Default: The variable w. If w is the transformation variable of f, then the default evaluation point
is the variable v.
Examples
Compute the Fourier transform of this expression with respect to the variable x at the evaluation
point y:
f = exp(-x^2);
fourier(f, x, y)
Lab Tasks:
1. Execute Fourier transform of complex exponential
2. Execute Fourier transform of rectangular gate
3. Execute Fourier transform of sinc function
Discussion:
Experiment no: 09
Theory:
Laplace transform
Syntax
laplace(f,trans_var,eval_point)
Description
Input Arguments
trans_var- Symbolic variable representing the transformation variable. This variable is often called the
"time variable".
Default: The variable t. If f does not contain t, then the default variable is determined by symvar.
eval_point- Symbolic variable or expression representing the evaluation point. This variable is often called
the "complex frequency variable".
Default: The variable s. If s is the transformation variable of f, then the default evaluation point is the
variable z.
Examples
Compute the Laplace transform of this expression with respect to the variable x at the evaluation point y:
syms x y
f = 1/sqrt(x);
laplace(f, x, y)
Lab Tasks:
Find out the laplace transform of the following functions:
i. 𝑒 −𝑎𝑡
ii. 𝑒 −𝑎𝑡 sin ωt
Discussion:
Theory:
Z-transform
𝐹(𝑧) = ∑∞
𝑛=0 𝑓(𝑛) ∗ 𝑧^ − 𝑛
Syntax
ztrans(f,trans_index,eval_point)
Description
Input Arguments
trans_index- Symbolic variable representing the transformation index. This variable is often called the
"discrete time variable".
Default: The variable n. If f does not contain n, then the default variable is determined by symvar.
eval_point- Symbolic variable or expression representing the evaluation point. This variable is often
called the "complex frequency variable".
Default: The variable z. If z is the transformation index of f, then the default evaluation point is the
variable w.
Examples
syms k x
f = sin(k);
ztrans(f, k, x)
Lab Tasks:
Find the Z transformation of the following function:
i. 𝑒 𝑗𝜔𝑡
ii. sinh bt
iii. cosh bt
iv. sinωt
v. cosωt
vi. t
vii. t^2
viii. U(t)
Discussion: