0% found this document useful (0 votes)
30 views79 pages

Signal & Systems

Uploaded by

yousaf hameed
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)
30 views79 pages

Signal & Systems

Uploaded by

yousaf hameed
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/ 79

Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

FIFTH SEMESTER
SIGNAL & SYSTEMS

COMPUTER SIMULATION LAB


DEPARTMENT OF ELECTRICAL ENGINEERING

Prepared By: Checked By: Approved By:

Engr. Amir Ijaz Engr. M.Nasim Khan Dr.Noman Jafri


Lecturer (Lab) Electrical, Senior Lab Engineer Electrical, Dean,
FUUAST-Islamabad FUUAST-Islamabad FUUAST-Islamabad

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

Name: ____________________________________________

Registration No: ____________________________________

Roll No: ___________________________________________

Semester: _________________________________________

Batch: ____________________________________________

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

C
COON
NTTE
ENNT
TSS

Exp No List of Experiments

1
Introduction to MATLAB
2
Complex numbers and matrix manipulation
3
Loops and decision making
4
Basic signals
5 Signals transformations

6 Periodicity and harmonics

7 Sampling, playback & sound manipulation

8 Convolution and Laplace transforms

9 Z-transform

10 Fourier series representation of periodic signal

11 Discrete Fourier transforms

12
Introduction to graphical user interface
13 Introduction to Simulink -I

14
Introduction to Simulink -II

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXPERIMENT NO - 1

INTRODUCTION TO MATLAB

OVERVIEW…
Over the past few decades, the unbelievable progress in the field of digital signal
processing has altered the entire meaning of the word ‘impossible’, by thinking
the unthinkable and doing the un-doable. With an ever-increasing set of
applications, signal processing has crept into almost every sphere of science, and
today, we see the techniques and tools developed by DSP scientists and
engineers being utilized for a better understanding and investigation of the
universe, and hence the life. This field of knowledge has helped us to stretch our
limits--- the very limits of thinking and imagination; the most cherish-able
outcome of all the knowledge. As our first course on DSP, we’ll try to familiarize
ourselves with the concepts and theory of DSP, to have a look at and use the
tools to solve the practical problems, and to have a flavor of the problems and the
challenges faced by man in the past, as well as today.

MATLAB is the most popular tool used for Digital Signal Processing. It
provides one of the strongest environments for study and simulation of the real-
world problems and their solutions, especially in the field of engineering. For
Signal Processing, it has a very comprehensive and easy-to-use toolbox with lots
of DSP functions implemented. Moreover, with the aid of Simulink, we can
create much more complex situations very easily, and solve them.

In this lab…we would have an introduction to MATLAB and get started with
working in its wonderfully simple environment. This lab, as a matter of fact,
would lay the foundation for our next labs.

In the following paragraphs, you are provided with a tutorial on


MATLAB. In addition, we’ll go over a detailed introduction to MATLAB in our
first discussion session. This would enable to you to do the simple but useful
things in MATLAB.

DEFINITION OF VARIABLES

Variables are assigned numerical values by typing the expression directly, for
example, typing

a = 1+2;

yields: a = 3;

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

The answer will not be displayed when a semicolon is put at the end of an
expression, for example type

a = 1+2;

MATLAB utilizes the following arithmetic operators:

+ addition
- subtraction
* multiplication
/ division
^ power operator
' transpose

A variable can be assigned using a formula that utilizes these operators and
either numbers or previously defined variables. For example, since a was defined
previously, the following expression is valid

b = 2*a;

To determine the value of a previously defined quantity, type the quantity by


itself:

yields: b = 6;

If your expression does not fit on one line, use an ellipsis (three or more periods
at the end of the line) and continue on the next line.

c = 1+2+3+...
5+6+7;

There are several predefined variables which can be used at any time, in the
same manner as user-defined variables:

i sqrt(-1)
j sqrt(-1)
pi 3.1416...

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

For example,

y= 2*(1+4*j);

yields: y = 2.0000 + 8.0000i;

PRE DEFINED FUNCTIONS IN MATLAB

There are also a number of predefined functions that can be used when defining
a variable. Some common functions that are used in this text are:

magnitude of a number (absolute


abs
value for real numbers)
Angle of a complex number, in
angle
radians
cosine function, assumes
cos
argument is in radians
sine function, assumes argument
sin
is in radians
exp exponential function

For example, with y defined as above,

c = abs(y);

yields: c = 8.2462;

c = angle(y);

yields: c = 1.3258;

With a=3 as defined previously,


c = cos(a);
yields: c = -0.9900;
c = exp(a);
yields: c = 20.0855;

Note that exp can be used on complex numbers. For example, with y = 2+8i as
defined above,

c = exp(y);

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

yields: c = -1.0751 + 7.3104i;

which can be verified by using Euler's formula:

c = exp(2)*cos(8) + j*(exp)2*sin(8);

DEFINITION OF MATRICES

MATLAB is based on matrix and vector algebra; even scalars are treated as 1x1
matrices. Therefore, vector and matrix operations are as simple as common
calculator operations.
Vectors can be defined in two ways. The first method is used for arbitrary
elements

v = [1 3 5 7];

creates a 1x4 vector with elements 1, 3, 5 and 7. Note that commas could have
been used in place of spaces to separate the elements. Additional elements can be
added to the vector:
v(5)=8;
yields the vector v = [1 3 5 7 8]. Previously defined vectors can be used to define
a new vector.
For example, with ‘v’
a= [9 10];
b= [v a];
creates the vector
b = [1 3 5 7 8 9 10].
The second method is used for creating vectors with equally spaced elements:
t = 0: 0.1:10;
creates a 1x101 vector with the elements 0, .1, .2, .3,...,10. Note that the middle
number defines the increment. If only two numbers are given, then the increment
is set to a default of 1
k = 0:10;
creates a 1x11 vector with the elements 0, 1, 2, ..., 10.
Matrices are defined by entering the elements row by row:
M = [1 2 4; 3 6 8];

creates the matrix.

There are a number of special matrices that can be defined:

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

null matrix:

M = [ ];

nxm matrix of zeros:

M = zeros(n,m);

nxm matrix of ones:

M = ones(n,m);
nxn identity matrix:
M = eye(n);
A particular element of a matrix can be assigned:
M(1,2) = 5;

places the number 5 in the first row, second column.

Operations and functions that were defined for scalars in the previous section
can also be used on vectors and matrices. For example,

a= [1 2 3];
b= [3 4 5];
c= a + b;
yields: c =[5 7 9];

Functions are applied element by element. For example,


t=0:10;
x=cos(2*t);
creates a vector x with elements equal to cos(2t) for t = 0, 1, 2, ..., 10.
Operations that need to be performed element-by-element can be accomplished
by preceding the operation by a ".". For example, to obtain a vector x that
contains the elements of x(t) = tcos(t) at specific points in time, you cannot simply
multiply the vector t with the vector cos(t). Instead you multiply their elements
together:

t=0:10;
x = t .*cos(t);

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

M-Files
M-files are macros of MATLAB commands that are stored as ordinary text files
with the extension "m", that is ‘filename.m’. An M-file can be either a function
with input and output variables or a list of commands. MATLAB requires that
the M-file must be stored either in the working directory or in a directory that is
specified in the MATLAB path list. For example, consider using MATLAB on a
PC with a user-defined M-file stored in a directory called "\MATLAB\MFILES".
Then to access that M-file, either change the working directory by typing
cd\matlab\mfiles from within the MATLAB command window or by adding
the directory to the path. Permanent addition to the path is accomplished by
editing the \MATLAB\matlabrc.m file, while temporary modification to the
path is accomplished by typing

path(path,'\matlab\mfiles')

from within MATLAB. Or, this can easily be achieved through the path browser.
As example of an M-file that defines a function, create a file in your working
directory named yplusx.m that contains the following commands:

function yplusx(y,x)
z=y+x

The following commands typed from within MATLAB demonstrate how this M-
file is used:

x=2
y=3
z = yplusx(y,x)

All variables used in a MATLAB function are local to that function only.
Variables, which are used in a script m-file, which is not a function, are all global
variables.
MATLAB M-files are most efficient when written in a way that utilizes matrix or
vector operations. Loops and if statements are available, but should be used
sparingly since they are computationally inefficient. An example of the use of the
command for is

for k=1:10;
x(k) = cos(k);
end

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

This creates a 1 x 10 vector x containing the cosine of the positive integers from 1
to 10. This operation is performed more efficiently with the commands

k= 1:10;
x=cos(k);

which utilizes a function of a vector instead of a for loop. An if statement can be


used to define conditional statements. An example is

if(a<=2);
b=1;
elseif(a>=4)
b=2;
else
b=3;

The allowable comparisons between expressions are >=, <=, <, >, ==, and ~=.
Suppose that you want to run an M-file with different values of a variable T. The
following command line within the M-file defines the value:

T = input('Input the value of T: ')

Whatever comment is between the quotation marks, is displayed to the screen


when the M-file is running, and the user must enter an appropriate value.

Plotting

Commands: plot, xlabel, ylabel, title, grid, axis, axes, stem, subplot, zoom,
hold

The command most often used for plotting is plot, which creates linear plots of
vectors and matrices; plot(t,y) plots the vector t on the x-axis versus vector y on
the y-axis. There are options on the line type and the color of the plot which are
obtained using plot(t,y,'option'). The linetype options are '-' solid line (default), '--
' dashed line, '-.' dot dash line, ':' dotted line. The points in y can be left
unconnected and delineated by a variety of symbols: + . * o x. The following
colors are available options: r, g, b, k, y, m etc.
For example, plot(t,y,'--') uses a dashed line, plot(t,y,'*') uses * at all the points
defined in t and y without connecting the points, and plot(t,y,'g') uses a solid
green line. The options can also be used together, for example, plot(t,y,'g:') plots a
dotted green line.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

To plot two or more graphs on the same set of axes, use the command
plot(t1,y1,t2,y2), which plots y1 versus t1 and y2 versus t2.
To label your axes and give the plot a title, type

xlabel (‘time (sec)’)

ylabel(‘step response’)
title(‘my plot’)

Finally, add a grid to your plot to make it easier to read. Type

grid

The problem that you will encounter most often when plotting functions is that
MATLAB will scale the axes in a way that is different than you want them to
appear. You can easily override the autoscaling of the axes by using the axis
command after the plotting command

axis([xmin xmax ymin ymax]);

where xmin, xmax, ymin, and ymax are numbers corresponding to the limits you
desire for the axes. To return to the automatic scaling, simply type axis.
For discrete-time signals, use the command stem, which plots each point with a
small open circle and a straight line. To plot y[k] versus k, type

stem(k,y)

You can use stem(k,y,'filled') to get circles that are filled in.
To plot more than one graph on the screen, use the command subplot(mnp)
which partitions the screen into an m x n grid where p determines the position of
the particular graph counting the upper left corner as p=1. For example,

subplot(211),semilogx(w,magdb);

subplot(212),semilogx(w,phase);

plots the bode plot with the log-magnitude plot on top and the phase plot below.
Titles and labels can be inserted immediately after the appropriate semilogx

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

command or plot command. To return to a full screen plot, type

subplot(111).

Saving & Loading

When using MATLAB, you may wish to leave the program but save the vectors
and matrices you have defined. To save the file to the working directory, type

save filename

where "filename" is a name of your choice. To retrieve the data later, type

load filename

General Information

• MATLAB is case sensitive so "a" and "A" are two different names.
• Comment statements are preceded by a "%".
• You can make a keyword search by using the help command.
• The number of digits displayed is not related to the accuracy. To change
the format of the display, type
format short e for scientific notation with 5 decimal places,
format long e for scientific notation with 15 significant decimal places and
format bank for placing two significant digits to the right of the decimal.
• The commands who and whos give the names of the variables that have
been defined in the workspace.
• The command length(x) returns the length of a vector x and size(x)
returns the dimension of the matrix x.

Exercise:
E.1. Create a vector,
a. “A” of the even whole numbers between 31 and 75.
b. “B” of the odd whole numbers between 75 and 131.
c. “C” of the whole numbers between 1 and 100.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

E.2. Let x = [2 5 1 6].

a. Add 16 to each element


b. Add 3 to just the odd-index elements
c. Compute the square root of each element
d. Compute the square of each element

E.3. Let x = [3 2 6 8]' and y = [4 1 3 5]' (NB. x and y should be column


vectors).

a. Add the sum of the elements in x to y


b. Raise each element of x to the power specified by the corresponding
element in y.
c. Divide each element of y by the corresponding element in x
d. Multiply each element in x by the corresponding element in y, calling
the result "z".
e. Add up the elements in z and assign the result to a variable called
"w".
f. Compute x'*y - w and interpret the result.

E.4. Evaluate the following MATLAB expressions by hand and use


MATLAB to check the answers

a. 2/2*3
b. 6-2/5+7^2-1
c. 10 / 2 \ 5 - 3 + 2 * 4
d. 3^2/4
e. 3^2^2
f. 2 + round (6 / 9 + 3 * 2) / 2 - 3
g. 2 + floor (6 / 9 + 3 * 2) / 2 - 3
h. 2 + ceil (6 / 9 + 3 * 2) / 2 – 3

E.5. Create a vector x with the elements...

a. 2, 4, 6, 8…
b. 10, 8, 6, 4, 2, 0, -2, -4
c. 1, 1/2, 1/3, 1/4, 1/5...
d. 0, 1/2, 2/3, 3/4, 4/5...

E.6. Create a vector x with the elements,

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

Xn = (-1)n+1/(2n-1)

Add up the elements of the version of this vector that has 100 elements.

E.7. Write down the MATLAB expression that will


A. computes the length of the hypotenuse of a right triangle given the lengths
of the sides (try to do this for a vector of side-length values).
B. computes the length of the third side of a triangle given the lengths of the
other two sides, given the cosine rule
c2 = a2 + b2 - 2(a)(b)cos(t)
where t is the included angle between the given sides.

E.8. Given a vector, t, of length n, write down the MATLAB expressions that
will correctly compute the following:

a. ln(2 + t + t2)
b. et(1 + cos(3t))
c. cos2(t) + sin2(t)
d. tan-1(1) (this is the inverse tangent function)
e. cot(t)
f. sec2(t) + cot(t) - 1
Test that your solution works for t = 1:0.2:2

E.9. Plot the expression (determined in modeling the growth of the US


population)
P(t) = 197,273,000/(1 + e-0.0313(t - 1913.25))
where t is the date, in years AD, using t = 1790 to 2000.
What population is predicted in the year 2020?

E.10. Write the following matrix in Matlab and perform the given function.
A= [12 15 16; 23 26 27; 31 40 41]
B= [22 35 56; 63 66 78; 24 30 46]
Find out
a. A+B
b. B*A
c. A.*B
d. B-A

Note:
• The decision of the marks will be made after the demonstration of
the lab exercise.
• Late submission will deduce the obtain marks by 15% on each day.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXPERIMENT NO – 2

COMPLEX NUMBERS AND MATRIX MANIPULATION

INTRODUCTION
Informally the terms matrix & array are often used interchangeably.
Matrix is the two dimensional array of real & complex numbers.
Matlab contain many functions that create different type of matrices.

(1) Creating a Matrix:


⎛1 1 1 ⎞
⎜ ⎟
>> A= ⎜1 2 3 ⎟
⎜1 3 6 ⎟
⎝ ⎠

(2) Creating a Symmetric Matrix:


For a symmetric matrix ‘A’ the matrix ‘A’ and its
transpose A ' are equal i.e.
A = A'
⎛1 1 1 ⎞
A= ⎜⎜1 2 3 ⎟⎟
⎜1 3 6 ⎟
⎝ ⎠
⎛1 1 1 ⎞
A ' = ⎜⎜1 2 3 ⎟⎟
⎜1 3 6 ⎟
⎝ ⎠
Note: from above we can easily say that A & A ' are equal. So matrix A is
symmetric matrix.
In Matlab we can use the command pascal to create symmetric matrix;
>> help pascal

PASCAL Pascal matrix.


PASCAL (N) is the Pascal matrix of order N: a symmetric positive
definite matrix with integer entries, made up from Pascal's
triangle. Its inverse has integer entries.
PASCAL(N,1) is the lower triangular Cholesky factor (up to signs
of columns) of the Pascal matrix. It is involutary (is its own
inverse).
PASCAL(N,2) is a rotated version of PASCAL(N,1), with sign flipped
if N is even, which is a cube root of the identity.

>> A=pascal(3)

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

A= 1 1 1
1 2 3
1 3 6

(3) Creating a Non Symmetric Matrix:

If A ≠ A '
Then matrix is non-symmetric:
We use magic command in Matlab to create such type of matrix.
>> help magic

MAGIC Magic square.


MAGIC (N) is an N-by-N matrix constructed from the integers
1 through N^2 with equal row, column, and diagonal sums.
Produces valid magic squares for all N > 0 except N = 2.
>> B=magic(3)

⎛8 1 6⎞
⎜ ⎟
B = ⎜3 5 7⎟
⎜ 4 9 2⎟
⎝ ⎠
⎛8 3 4⎞
⎜ ⎟
B' = ⎜1 5 9⎟
⎜6 7 2⎟
⎝ ⎠

Since here B ≠ B ' hence the matrix B is non symmetric.

(4) Creating matrix with Random integer:


Type the following command in Matlab and read carefully the whole
help.
>>help rand:
>> help fix

FIX Round towards zero.


FIX(X) rounds the elements of X to the nearest integers towards zero.
See also FLOOR, ROUND, CEIL.
Now type
>> C=fix (10*rand (3, 2))

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

⎛9 4⎞
⎜ ⎟
C = ⎜ 2 8⎟
⎜6 7⎟
⎝ ⎠
Here C is the generated matrix with 3 rows and 2 columns.

(5) Matrix Addition and Multiplication:


Matrix Addition and Multiplication is done element by element. This
means that both matrices should have same number of rows and
columns, so they can be added and subtracted.
>> d= [1+2j 3; 7 5j]
>> c= [2+3j 5+5j; 0 1-8j]
⎛1+2j 3 ⎞
d =⎜ ⎟
⎝7 5j ⎠
⎛ 2 + 3 j 5+5j ⎞
c=⎜ ⎟
⎝0 1-8j ⎠
>>f=d+c
⎛ 3+5j 8+5j ⎞
f= ⎜ ⎟
⎝0 1-3j ⎠
Here also real part is added to real one and imaginary part with the
imaginary part.

(6) Calculating complex Conjugate:


We use conj command to calculate the complex conjugate of each
complex element in the matrix.
Type
>>help conj % for more information about conj command
>> g = [1+2j; 3+4j];
⎛1+2j ⎞
g =⎜ ⎟
⎝ 3+4j ⎠
>> h=conj (g)
⎛1 − 2 j ⎞
h=⎜ ⎟
⎝3− 4 j ⎠

>> g' Î return the conjugate transpose

Ans = [1- 2j 3- 4j]

>> g.' Î return the unconjugate transpose

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

Ans = [1+ 2j 3+ 4j]

(7) Matrix Powers:


Let suppose you want to multiply a matrix A with itself P times. Then
write the following command in matlab.
>>A = [1 2 3; 4 5 6; 7 8 9];
>>P=2;
>>B=A^P
⎛1 2 3 ⎞
⎜ ⎟
A = ⎜ 4 5 6⎟
⎜7 8 9⎟
⎝ ⎠
⎛ 30 36 42 ⎞
⎜ ⎟
B = ⎜ 66 81 96 ⎟
⎜ 102 126 150 ⎟
⎝ ⎠
(*) Note you can also multiply matrix and also vector element by element.
>>A = [1 2 3; 4 5 6; 7 8 9];
>>P=2;
>>C=A. ^P
⎛1 2 3 ⎞
⎜ ⎟
A = ⎜ 4 5 6⎟
⎜7 8 9⎟
⎝ ⎠
⎛1 4 9 ⎞
⎜ ⎟
C = ⎜16 25 36 ⎟
⎜ 49 64 81⎟
⎝ ⎠

(8) Extracting Real & Imaginary Part From Complex Number:


Let Z=A+Bj;
Then Z (real) =real (A);
And
Z (imaginary) = imag (A);
Try it yourself with numerical values, and write down the results.
Z=………………..
Z (real) =………
Z (imaginary) =………………

(9) Accessing elements in Vectors/Matrices:


Accessing the element from inside a vector or matrix is the most
powerful advantage of the matlab over any other computer
language. Let’s see how this is done by matlab

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

>> K=10:10:100;
K= [10 20 30 40 50 60 70 80 90 100]
>>K1=K (1:5)
K1= [10 20 30 40 50]
>>K2=K (3:8)
K2= [30 40 50 60 70 80]
>>P=Pascal (3)
⎛1 1 1 ⎞
⎜ ⎟
P= ⎜1 2 3 ⎟
⎜1 3 6 ⎟
⎝ ⎠
>> P1 =P(:, 2)
⎡1 ⎤
P1= ⎢⎢ 2 ⎥⎥
⎢⎣3 ⎥⎦
>>P2=P(1,:)
P2= [1 1 1]
M=[1:5; 2:2:10; 3:3:15]
⎡1 2 3 4 5 ⎤
M = ⎢⎢ 2 4 6 8 10 ⎥⎥
⎢⎣3 6 9 12 15 ⎥⎦
>>M1=M (2:3, 2:4)
⎡4 6 8 ⎤
M1 = ⎢ ⎥
⎣6 7 12 ⎦
>>M (:) % this command will convert the matrix into vectors such
that
columns are taken first. Try it yourself & also below given
commands.
>>K=1:2:10
>>f=5:5:50;
>>f(k)Æchoose position values within the ‘f’

(10) Evaluating Complex Variables & Expressions:


Consider the following two expressions that we want to write in
matlab.
M=ej11п/4 Æ j (cosө+jsinө)
M1= (1-j) 10
Now if we want to solve these expressions by using the matlab,
then we use these commands are techniques.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

>> M=j*exp (j*11*pi/4)%Æ Here pi is the predefined variable in


Matlab. If we want to change its value to 3, then we can use the
command
>>pi=3;
Now if we want to restore the default value then type
>> clear pi;
>>M1= (1-j) ^10;

(11) Plotting Complex value functions in Matlab:


Plots in MATLAB are generated using the plot function. Try the
below command.
>> help plot
Now as we already know from our previous knowledge that
complex numbers corresponds to x = a+bj
So MATLAB provides the ‘real’ & ‘imag’ function to separate the
real &imaginary parts of a complex number.
For more clearance of mind try to absorb the following example
in mind.
>> x=2+7j;
>> xr=real(x);
>> xi=imag(x);
>> Plot (xr, xi,'x'); % Here ‘x’ defines the shape of the data point.
Explain the above four lines.
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………
……………………………………………………………………………………

(12) Generating Complex Functions:


Generating complex function can be explained from the following
example.
f (t) =3ei3пt
For t=0Æ1 with 0.001 increments
We could use
t=0:0.001:1;
f=3*exp (j*3*pi*t);
Explain the above two lines your self
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………
………………………………………………………………………………………

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

Exercise:

E.1. Create a symmetric matrix ‘m’ with five rows and five columns. Calculate
its transpose and verify that the matrix is symmetric by observing that m=m’.

E.2. Create a random matrix with 3 rows and 5 columns. The matrix values
should be integer b/w (0—100), than change its all 2 rows values by ‘7’.
Hint: Use commands fix & rand

E.3. Use your knowledge to define a vector by incrementing and also how to
cascade the more than one vector to form a matrix try to make a matrix with the
following properties.
(1) Matrix should contain 2 rows.
(2) First row of matrix should contain even number b/w (0—10)
(3) Second row of matrix should contain odd number b/w (11—20)
How many columns are there in above matrix?
………………………………………………

E.4.
Define
⎡1+2j 3 ⎤ ⎡ 2+3j 5+5j⎤
d= ⎢ ⎥ & e= ⎢
⎣7 5j⎦ ⎣0 1-8j ⎥⎦
and calculate
(a) c=d+e
(b) c=d-e
(c) c=d*e
(d) c=d/e

Note: multiplication and division should be done element by element.

E.5. Plot the function


y=3*exp (3*pi*t);
Choose t from (0---150) with increment of 0.001

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXPERIMENT NO – 3

LOOPS & DECISION MAKING

INTRODUCTION
As well the concept of iteration or loop is concerned you are suppose to
be familiar with it. There are two most prominent types of loop in Matlab. These
are “for & while” loop. Also you are familiar with the conditional statement in
C++. That are “if & Switch Statement”. We will study some of them very soon
and some in next one lab.
“For” LOOP:
The ‘for’ loop allow us to repeat certain commands. If you want to repeat
some action in a pre determined way, you can use the ‘for’ loop. All of the loop
structures in MATLAB are started with a key word such as ‘for’ or while’ and
they all end with the word ‘end’.
The ‘for’ loop will loop ground some statement, and you must tell
MATLAB where to start and where to end. Basically you give a vector in the ‘for’
statement and MATLAB will loop through for each value in the vector.
For example a simple loop will go around 4 times like
Example 3.1:
>> for j=1:4,
j
end
Observe the above three lines result and write down them below
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
Note: once the MATLAB reads the ‘end’ statement, it will loop through and print
out J each Time.
For an other example, if we define a vector and data want to change the
entries , we can step through and change each individual entry.

Example 3.2:
>> v= 1:3:10
>>for j=1:4,
V (j) =j;
end
Comment on above lines after observing the result in Matlab.
………………………………………………………………………………………………
………………………………………………………………………………………………

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

………………………………………………………………………………………………
………………………………………………………………………………………………

Important Note: Using loops in MATLAB are not efficient. Loops cause the
programs to run very slow. Always use some alternative way so that programs
run fast.

Alternative approach for Example 2


Matlab provide us another efficient method to avoid from loops. That is its
advantage over other computer languages.

Example 3.3:
>>v=1:3:4
>>v (1:4) =1:4;
Comment on above lines after observing the result in Matlab.
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………

Now here is another example of using ‘for’ loops this example declares a
vector ‘x’ and then uses a ‘for’ loop to sum all of the values in ‘x’.
Example 3.4:
>>x=20:20:200
>>total =length(x);
>>temp=0;
>>for i=1:total
temp =temp+x(i);
end
>>temp

Comment on above lines after observing the result in Matlab.


………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
Prescription:
In example 4 we define a vector ‘x’ with values,
x = [20 to 60 80 100 120 140 160 180 200]
then we use “total = length(x)”. In this statement we are calling the “length”
function. This will return the total number of elements in ‘x’ & assign it to the

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

total variable. Then we define a ‘temp’ variable and set it to ‘o’, using “temp = 0”
statement.
Then we use a ‘for’ loop & using this ‘for’ loop we add the x (i) value to temp
and reassign that value to temp again.

While LOOP:
The “while” loop repeats a sequence of commands as long as some
condition is met. A simple example using a ‘while’ loop is

Example 3.5:
j= 1 ;
w h ile (j< = 4 )
a (j)= 1 0 * j;
j= j+ 1 ;
end
a
Comment on above lines after observing the result in Matlab.
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
The while loop continues to execute until the value of j becomes greater
than 4. When this happens the loop exits.
The above ‘while’ loop body will execute until the condition “j < = 4”.
When this condition becomes false the ‘while’ loop will stop executing.
Example 3.6:
a = 1 :1 0
i= 1 ;
w h ile (i< = 1 0 )
b (i)= 2 ^ a (i);
i= i+ 1 ;
end
b
Comment on above lines after observing the result in Matlab.
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………

Description: The above example declares a vector

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

a = [1 2 3 4 5 6 7 8 9 10].
Then using a “while” and tells that loop until the value of ‘i’ is less than or equal
to 10. Then using the statement “b (i) = 2 a (i)” & “i = i + 1" it makes another
vector having values.

b = [21 2
2
2
3
2
4
2
5
2
6
2
7
2
8
2
9
2
10] at the end it displays the value of ‘b’

IF Statement:
We can make decisions by using the ‘if’ statement. the ‘if’ must be terminated
with an ‘end’ statement

Example 3.7
c=2;
if(c==1)
disp('c is 1')
elseif (c==2)
disp('c is 2')
else
disp('c is neither 1 nor 2');
end

Comment on above lines after observing the result in Matlab.


………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………

The above example declares a variable ‘c’ & assigns it a value ‘2’ by using “c = 2”.
Then it uses ‘if’, ‘elseif’ and ‘else’ statements to check the value of ‘c’ and
displays the result using “disp” command.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXERCISE:
E.1. Define a vector a = [1 2 3 4 5] and then using a “for” loop create another
vector ‘b’ haring the following values corresponding to ‘a’

b = [11 22 33 44 55]

E.2. Without using a “for” loop and using some alternative method define
vector
a = [1 2 3 4 5] and create b1 = [11 22 33 44 55]

E.3. Indicate the error in the following. Tell the line # which contains the error
and what is the error.
1.......v=0:2:10
2.......for c=1:length(v)
3........ v(c-1)=v(c)+5;
4........end
5........v
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………

E.4. Refer to the “example 4”. We have vector


x = [20 40 60 80 100 120 140 160 180 200] and we want to multiply all
the elements of the vector ‘x’ e.g. (20 x 40 x 60 …………. x 200) and store the
result in temp. Modify the code for “example 4” so that it performs the above
task.

E.5. Refer to “example 6”. Change the code so that it creates vector ‘b’ having
values
b = [51 52 53 …………. 510].

E.6. What will be the output of “example 7” if we define C = 3 and other code
remains unchanged?

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXPERIMENT NO – 4

BASIC SIGNALS

DISCRETE SIGNALS:
Discrete time signals are defined only at discrete times, consequently for
these signal, the independent variable (usually time) takes on only a discrete set
of values e.g. stock market index.

UNIT IMPULSE:
One of the simplest discrete time signals is the unit impulse or unit
sample, which is defined as

⎛0 n ≠ 0⎞
δ [n] = ⎜ ⎟
⎝1 n=1 ⎠

0 n Æ

A delayed impulse by ‘no’ units can be defined as


⎛ 0 n ≠ no ⎞
δ [n − no] = ⎜ ⎟
⎝1 n=no ⎠

no nÆ
Any discrete time signal can be broken down into a series of individual
impulses which when added together give back the original discrete time signal.
y[n] = 2δ [n] + 5δ [n − 3] + 2δ [n + 1] + 7δ [n + 2]
722 5

-2 -1 0 3

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

MATLAB IMPLEMENTATION:

Example 1:
Write and save the following function in matlab.

Function [x, n] =duibt031018 (L, no)


n= -L: L;
x=zeros (1, length (n));
x (L+no+1) =1;
Now call the function in matlab command window by using the
following command.
>> [x,n]=duibt031018(10,1);
>> stem(n,x)
Give your comments on above example.
………………………………………………………………………………………………
………………………………………………………………………………………………

Example 2:
Now call the above function with the value of L=5 & no=2;
You will see the following result.
1

0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

0
-5 -4 -3 -2 -1 0 1 2 3 4 5

Here L + no + 1 = 5 + 2 + 1 = 8 & so at 8 index we place 1 , all other


indexes have value of ‘0’.
Since we are defining from “ – L:L” so the total length of this vector will
be 2L + 1 . Given below is another way of creating unit impulse.
(2 L + 1) − ( L + no) − 1 = L − no;
x = [ zeros (1, ( L + no)) 1 zeros(1,(L-no))];

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

-3 -2 -1 0 1 2 3

So we can implement the above function by another method.


Function [x, n] =dui2bt031018 (L, no)
n= -L: L;
x=[zeros(1, (L+no)) 1 zeros(1,(L-no))];
Now call the given function another time by the same values as you have
used before for L& no and write down the results below.
………………………………………………………………………………………………
………………………………………………………………………………………………
………………………………………………………………………………………………
UNIT STEP:
The following relation gives a discrete unit step function.

⎛1 n>=0 ⎞
U[n-no] = ⎜ ⎟
⎝ 0 n<0 ⎠

0 1 2 3 4 5Æ
The following relation gives the general form of a delayed unit step
function
⎛1 n>=no ⎞
U [n-no] = ⎜ ⎟
⎝0 n< no ⎠

noÆ

MATLAB IMPLITATION:
Example # 3:
Function [x,n]=dusbt031018(L,no)
n= -L:L;

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

len=length(n);
x=zeros(1,len);
x(L+no+1:len)=1;

Example#4:
Function [x,n]=dusbt031018(L,no)
n= -L:L;
len=length(n);
x=[zeros(1,l+no) 1 ones(1,L-no)]
% x=[zeros(1,l+no) ones(1,L-no+1)]

Example 4 & example 5 are basically the some implementation ( I.e. they
perform the same task of creating a discrete unit step function)
CONTINUOUS SIGNALS: In the case of continuous signals the independent
variable is continuous, and thus their signals are defined for a continuum of
values of time e.g. a speech signal that has values defined for all time.

UNIT IMPULSE in Continuous unit impulse is defined as


⎛1 t=0 ⎞
δ(t ) = ⎜ ⎟
⎝ 0 o.w ⎠

-3 -2 -1 0 1 2 3 Æ
Shifted impulse definition is given by.
⎛1 t-to ⎞
δ(t − to) = ⎜ ⎟
⎝0 ow ⎠

-3 -2 -1 0 1 2 3 Æ
here no=3;

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

MATLAB IMPLITATION:
Example #5:
Function [x,t]=cuibt031018(L,no,inc)
t= -L:inc:L;
x=zeros(1,length(t));
x((L+no)/inc+1)=1;

Example #6:
Function [x,t]=cuibt031018(L,no,inc)
t= -L:inc:L;
x=[zeros(1, L+no)/inc) 1 zeros(1, (L-no)/inc)];
Example 5 & 6 perform some task but in a deferent way of MATLAB
implementation.

UNIT STEP in Continuous time unit step function is defined in a manner similar
to it s discrete time counterpart i.e.
⎛1 t>=0 ⎞
u (t ) = ⎜ ⎟
⎝ 0 t<0 ⎠
The delayed singles general form will be
⎛ t>=to ⎞
u (t − to) = ⎜ ⎟
⎝ 0< t0 ⎠

MATLAB IMPLIMENTATION:
Example #7
Function [x,t]=cusbt031018(L,no,inc)
t=-L:inc:L;
Len=length(t);
x=zeros(1,len);
x(((L+no)/inc+1:len)=1;

Example #8
Function[x,t]=cusbt031018(L,no,inc)
t=-L:inc:L;
x=[zeros(1,(L+no)/inc) 1 ones(1,(L-no)/inc)];

EVEN & ODD SINGALS:


Another set of useful properties of signals relates to their symmetry under
time reversals. A signal x (t) or x [n] is referred to as even signal if it is identical
to its time reversal component, i.e. with its reflection about the origin.
A signal is even if

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

x(-t)=x(t)
or
x[-n]=x[n]
A signal is considered odd if
x(-t)= -x(t)
or
x[-n]= -x[n]
An important fact is that any signal can be broken into a sum of two
signals, one of which is even and one of which is odd.
The even part of a signal can be expressed
Xe(t ) = 1/ 2[ x(t ) + x(−t )]
The odd part representation of a signal I, given by
Xo(t ) = 1/ 2[ x(t ) − x(−t )]

EXERCISE
E.1. Implement the function y[n] given by the relation
y[n] = 2δ[n + 3] + δ[n − 1] + 5δ[n] + 6δ[n + 5] − 7δ[n − 2]
take L=6
E.2. Implement the continuous time function y(t)
y (t)=3u(t)+t*u(t)-(t-1)*u(t)-5u(t-2)
Take L=5, inc=0.01
E.3. Implement the function given below
(A) X1[n]=u[n+2]-u[n-2] (L=6)
(B) X2[n]=u[n+7]-u[n+5]-2u[n-7] (L=10)
(C) X1(t)=u(t+2)-u(n-2) (L=6; inc=0.01)
( D ) X 4(t ) = δ (t − 1) − 3δ (t − 4) + 2δ (t + 1) (L=5, inc=0.01)

E.4. Shown below is the graph for discrete function Use “for” loop to
implement the below graph. You can use function of “example 1 or Example 2”.
Take L=40. Also plot the given graph

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

40

30

20

10

-10

-20

-30

-40
-40 -30 -20 -10 0 10 20 30 40

E.5. Implement the function shown in the graph below


Take L=6

1.8

1.6

1.4

1.2

0.8

0.6

0.4

0.2

0
-4 -3 -2 -1 0 1 2 3 4

E.6. Shown below is the graph for discrete function Use “for” loop to
implement the below graph. You can use function of “example 3 or Example 4”.
Take L=20. Also plot the given graph

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXPERIMENT NO – 5

SIGNALS TANSFORMATIONS
TIME SHIFTING:
Consider a signal x(t). A time shifted version of this signal is

Y(t) = x(t – t0)

Where ‘to’ is a consistant

Note that y(t0) = x(0) – Hence if ‘t0’ is positive the shifted signal is delayed
in time (shifted to the right relative to x(t). If to is negative , y(t) is advanced in
time (shifted to the left).

MATLAB IMPLEMENTATION
Example # 1
in c = 0 .0 1 ;
t = - 1 0 : in c :1 0 ;
f = 0 .1 ; to = 2 ;
x = s in ( 2 * p i* f* t);
y = s in (2 * p i* f* (t - t0 ));
s u b p lo t ( 3 1 1 ) ;
h o ld o n ;
p lo t ( t,x );
p l o t ( t , y , 'r : ') ;
g rid
s u b p lo t( 3 1 2 ) ;
p lo t ( t,x );
g rid
s u b p lo t( 3 1 3 ) ;
p l o t ( t , y , 'r : ') ;
g rid

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXPLANATION
This code plots the function x = sin (2πf(t – t0)
Thus

Y(t) = x(t – t0)

Here t0 = 2 so “y(t)” with be delayed in time by 2 seconds

TIME SCALING:
Given a signal x(t) a time scaled version of this signal is

Y(t) = x(at) i

Where ‘a’ is a real constant

Consider a = 2 such that y(t) = x(2t) i


For any particular value of time t = t0, y(t0) = x(2t0) and x(t0) = (yt0/2). It is seen
that y(t0) = x(2t0) is a time compressed (speed up) version of x(t0).

Consider a = ½ such that y(t) = x(t/2)


For any particular value of time t = t0 y(t0) = x(t0/2) and x(t0) = y(2t0). It is
observed that y(t0) = x(t0/2) is a time (slowed down) version of x(t0)

EXPLANATION
This coding plots the function x = sin(2πft) and also plots the function

Y(t) = x(2t);
Here a = 2 so the function y(t) will be compressed in time (speed up) version of
the signal x(t). here the frequency will be doubled.

MATLAB EMPLEMENTATION
Example # 2

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

in c = 0 .0 1 ;
t = -1 0 : in c :1 0 ;
f = 0 .1 ; a = 2 ;
x = s in (2 * p i* f* t);
y = s in (2 * p i* f* (a 8 t));
s u b p lo t (3 1 1 );
h o ld o n ;
p lo t ( t,x );
p lo t ( t , y , 'r ; ') ;
g rid
su b p lo t (3 1 2 );
p lo t ( t,x );
g rid
s u b p lo t (3 1 3 );
p l o t ( t , y , 'r ; ') ;
g rid

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

3 – TIME REVERSAL
In time reversal, we create a new, transformed signal y(t) by replacing t with - t
in the original signal x(t)

Y(t) = x(-t)

For any particular value of time t = t0 y(t0) = x(-t0) and y(-t0) = x(t0)

MATLAB IMPLEMENTATION
Example # 3

in c = 0 .0 1 ;
t = -1 0 : in c : 1 0 ;
f = 0 .1 ; x = s in (2 * p i* f* t);
r x = f lip lr ( x ) ;
r t = f lip lr ( t) ;
rt = -1 * rt;
L = le n g th (x );
s u b p lo t ( 3 1 1 );
h o ld o n ;
p lo t ( t( L /2 :L ), x ( l/2 : L )) ;
p l o t ( r t ( 1 : L / 2 ) , r x ( 1 : L / 2 ) , 'r : ') ;
g rid
t i t l e ( 'e x a m p l e o f t i m e R e v e r s a l ') ;
s u b p lo t( 3 1 2 );
p lo t ( t,x ) ;
g r i d y l a b e l ( 'o r i g i n a l s i g n a l ') ;
s u b p lo t ( 3 1 3 );
p l o t ( t , r x , 'r ; ') ;
g rid
y l a b e l ( 'T i m e R e v e r s a l S i g n a l ') ;

EVEN & ODD SIGNAL REPRESENTATION


A signal is even if the condition below is satisfied
Xe(t) = xe(-t) A
An even function (signal) has the symmetry with respect to the vertical axis. The
signal for t<0 is the mirror image of the signal for t>0. the function cos(wt) is
even because cos(wt) = cos(-wt)

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

A function is odd if the condition


X0(t) = -x0(-t) B
An odd function has the symmetry with respect to the origin. The function x(t) =
sin(wt) is odd because sin(wt) = -sin(-wt).
Any signal can be expressed as the sum of even and an odd part, that is
X(t) = xe(t) + x0 (t) C
Where xe(t) is even and x0(t) is odd part of the signal x(t). Replacing ‘t’ with ‘-t’
in equation ©.
X(-t) = xe(-t) + x0(-t) = xe(t) – x0(t)
X(-t) = xe(t) – x0(t) D

Adding (C) & (D) & dividing by 2 yields


Xe(t) = ½ [x(t) + x(-t)]
X0(t) = ½ [x(t) – x(-t0]

Examples of even odd Signals

1 – cos(wt) x(t) = cos(wt), x(-t) = cos(-wt) = cos(wt)


since x(t) = x(-t) so cosine is even signal
2 – sin(wt) x(t) = sin(wt), x(-t) = sin (-wt) = -sin(wt)
since x(t) = -x (-t) so sine is odd signal

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

MATLAB IMPLEMENTATION (EVEN ODD SIGNALS


Example # 4 “even odd bt 02301.m”

function [even, odd] = evenodd bt 02301 (signal, time, cod);


revsignal = fliplr (signal);
rev time = fliplr (time);
revtime = -1* revtime;
even = 0.5* (signal + revsignal);
odd = 0.5* (signal - revsignal);
if (cod = = 'd')
subplot (511);
stem (time , signal , 'r');
ylable ('signal');
subplot (512);
stem (revtime, revsignal, 'k');
ylabel ('Time Reversal');
Subplot (513);
stem (time, odd);
ylabel ('odd');
subplot (515);
stem (time, even + odd');
elseif (cod = = 'c')

Copy paste the code from above


and replace "stem" by "plot" command.
no other changes required.
end

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

RUNNING THE FUNCTION OF EXAMPLE # 4


To run the function f example #4 we have to call the function and pass it the
values of “signal” “time” and “cod”.

Example # 5 “run even odd bt 02301.m”

inc = 0.01
t = -10 + inc :inc: 10-inc;
f = 0.2;
x = cos (2*pi*f*t);
[even,odd] = even odd bt 02301 (x, t, 'c');

Example #6: Run example # 5 for the following values of ‘x’

1 x = sin(2*pi*f*t);
2 x = sinc(t);
3 x = sin(2*pi*f*t). *sinc(2*pi*5*f*t*);
4 x= square (2*pi*f*t);
Change inc 0.1 +eps;
[even, odd] = even odd bt 02301 (x,t, 'd');
5 x = cos (2*pi*f*t) + square (2*pi*f*t);
Change inc = 0.01 + eps;
[even, odd] = even odd bt 02301 (x,t, 'c');
6 x = 2x sin(2*pi*f*t) + square (2*pi*f*t);
Change inc = 0.01 + eps
[even, odd] = even odd bt 02301 (x,t, 'd');
7 x = cos (2*pi*f*t) + sin (2*pi*10*f*t);
Change inc = 0.021 ;[even, odd] = even odd bt o2301 (x,t, 'c');
8 x = cos (2*pi*f*t) + sin(2*pi*f*30*t);
Change inc = 0.02

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXPERIMENT NO – 6

SIGNAL & SYSTEMS

PERIODICITY & HARMONICS


A very common class of signals that we encounter is the class of periodic
signals. A periodic continuous time signal has the property that there is a
positive value of ‘t’ for which
X(t) = x(t + aT) Æ A (continuous time)

Where a = 0,1,2,3,4 -----------


For all values of ‘t’. In other words a periodic signal has the property that
it is unchanged by a time shift of ‘T’. In this case we say that x(t) is periodic
signal is the sine signal.

X(t) = sin (wt)

Where w = 2πf; therefore

X(t) = sin (2πft)

Since sin(Q) = sin(Q + 2π) therefore the signal is periodic with the period
‘2π’.
A periodic signal in discrete time is defined similarly to continuous time
periodic signal. Specifically a discrete time periodic signal.
Specifically a discrete time signal x[n] is periodic with period ‘N’ where
‘N’ is positive integer, i:e if it is unchanged by a time shift of ‘N’ i:e.
X[n] = x[n + bN] Æ B (discrete time)
Where b = 0,1,2,3,4,-----------

FUNDAMENTAL PERIOD
The fundamental period is the smallest positive value of ‘T’(continuous)
or ‘N’ (discrete) for which the equations of periodicity held.
Consider the example of a sine function. In that case the signal is periodic on 2π,
4π, 6π and so on but the fundamental time period of the signal is 2π.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

MATLAB EXPLANATION OF PERIODIC SIGNALS

EXAMPLE # 1
t=0:0.01:6;
f=0.5;
x=sin(2*pi*f*t); hold on;
plot(t,x);
xL=[ 1/f 1/f];
yL=[1 1];
plot(xL, yL, 'r:')
hold off

Note that in the above example


x (2) = x(2 + 2/0.01) = x(202)

HARMONICS:
Harmonics are integral multiples of a signal. Consider the signal
cos (wot). its harmonics are cos(2 wot), cos (3 wot)is the fundamental harmonic.

EVEN, ODD HARMONICS:


If the harmonic of a signal function has an even co – efficient it is called an
even harmonic. Otherwise if the co – efficient is odd it is called an odd harmonic.
X(t) = Cos (wot)
Cos (2 wot), Cos (4 wot) , Cos (6 wot) …………………….. Even Harmonics
Cos (3 wot), Cos (5 wot) , Cos (7 wot) …………………….. Odd Harmonics
Eve, odd harmonic play an important role in signal construction. Different
waveforms can be obtained by adding one type of harmonic e.g adding infinite
odd harmonics of sine/cosine where generates a square wave. Both will have a
90 phase shift from each other.
Practically it is not possible to add all the (infinite) number of harmonics
to obtain a perfect square wave but as the number of harmonics is increased from
10 onwards we obtain a decent square wave.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXAMPLE # 2
inc = 0.01;
t = 0 : inc : 4;
f = 0.25;
w = 2 * pi * f ;
fundamental=sin(w *t);
harmaonic2=sin(2*w *t);
harmaonic3=sin(3*w *t);
harmaonic4=sin(4*w *t);
hold on;
plot(t,fundam ental,'ro');
plot(t,harm onic2,'k--');
plot(t,harm onic3,'m:');
plot(t,harm onic4,'y');
hold off

SQUARE WAVE GENERATION BY COS FUNCTIONS:


For square wave generation by ‘cos’ functions odd harmonics we use.
X(t)=4/π; (Cos (wt) – 1/3 Cos (3 wt) + 1/5 Cos (5 wt)+…….

Example#3
t = 0 : 0.01: 6
f = 0.5
w = 2* pi * f
% plot function x(t)

Exercise:
1. Write MATLAB function for plotting the 5 harmonics of a signal x(t) =
cos(wt) with a fundamental frequency of f = 0.5 Hz. Take t = 0:0.01:2

2. What are the frequencies and Time periods at the all 5 harmonics in
questions 1

3. Consider the general form of an equation


F(t) = 4/nπ sin(nwt) 1≤n≤∞

Take the case of 1 ≤ n ≤ 5, w = 2πf, f=2Hz

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

t = -2:0.01: 2. Plot all the 5 harmonics of the above function in same figure using
the subplot command. Subplot (511), subplot(512) --------
Subplot (513), Subplot(514), Subplot (515),

4. If the frequency of x(t) = sin(2πft) is f = o.2hz what will be the time period
of the 4rth harmonic of the above signal x(t). plot x(t) and its 4rth harmonic and
check the time period of 4rth harmonic in MATLAB. Do result agree with your
theoretical calculation of time period of 4rth harmonic?

5. Refer to general function f(t) in question 3 Now


inf

∑ (4 /(n8 pi)) *sin(n * w * t )


n =1

Plot the function f(t) in Matlab upto n=5 take f=1Hz, t=0:0.01:4

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXPERIMENT NO – 7

SAMPLING, PLAYBACK & SOUND MANIPULATION

SAMPLING:
A continuous time signal x (t) has its value defined for every value of
time ‘t’. on the other hand a discrete time signal has its value at regular fired
intervals. A continuous time signal can be represented by its values or samples at
points equally spaced in time.
Consider a continuous time signal x(t). This signal can be represented as a
discrete time signal having values of x(t)at ‘n’ number of intervals spaced by a
time interval ‘Ts’. We can’t obtain all the data in the signal by sampling it. Even if
we sample at a very high rate some data will still be lost but the results of course
will be very good.

NYQUIST CRITERIA:
If the greater the number of samples the greater the accuracy, then why
we don’t sample at a very high rate. There are two reasons for it. First of all high
number of samples require a lot of manipulation on the behalf of machine
software that we are using which results in move time. Secondly most of the vital
data that we need can easily be obtained from low sampling rates.
Therefore we need some criteria to define the rate at which the signals
should be sampled. This criterion is called the Nyquist criteria.
According to it, the sampling frequency must (at least) be twice the signal
frequency. When the sampling rate doesn’t satisfy the Nyquist. When the
sampling get the correct information about the signal.
Reducing the number of samples reduces information above original signal.
MATLAB is computer based simulation software. In MATLAB choosing a
very high sampling rate simulates analog signals. Normally the sampling
frequency chooses to be 10 times higher than the maximum frequency
component in a given signal.

Example#01:
f=50;
fs=10*50;
ts=1/fs;
t=1:ts:0.1;
x=sin(2*pi*f*t);
stem(t,x)

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXPLANATION:
This example generates a sinusoidal signal x(t) = sin wt = sin 2πft will f =
50 Hz. The sampling frequency ‘fs’ is chosen to be 10 times the signal frequency.
Note that in one complete cycle it contains 10 samples.

Example#02:

f=1;
w=2*pi*f;
a=4;
fs1=2*f; fs2=10*f; fs3=20*f; fs4=40*f;
ts1=1/fs1; ts2=1/fs2; ts3=1/fs3; ts4=1/fs4;
t1=0:ts1:a; t2=0:ts2:a; t3=0:ts3:a; t4=0:ts4:a;
x1=cos(w*t1); x2=cos(w*t2);
x13cos(w*t3); x4=cos(w*t4);
subplot(221); stem(t1,x1);
subplot(222); stem(t2,x2);
subplot(223); stem(t3,x3);
subplot(224); stem(t4,x4);

EXPLANATION:
The above example samples a cos(wt) signal of frequency 1 Hz With the
sampling frequencies 2Hz, 10Hz, 20Hz & to Hz and displays the four sampled
signals in & independent plots using the stem command. We can easily see that
as the closer and signal accuracy increases.

SOUND MANIPULATION IN MATLAB


PLAYBACK An analog signal input to the sound card is sampled and
digitized by sound software, such as the “sound recorder” in windows. The
recorded sound signal is saved in a wav file. This file can be retrieved in
MATLAB and played back.
Mono/StereoMatlab Understands The Mono & Stereo Sounds As Single
Column And 2 Column Vectors respectively. In STEREO different data using 2
hardware channels. STEREO was different data for left and right speakers. Mono
is represented by single column vector and stereo by two column vectors in
MATLAB.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

MANIPULATION:
We know that MATLAB treats all the inputs and outputs as
matrices/vector. Therefore for the manipulation of an audio signal, is no
different than altering the elements of a matrix. Avoid using loops in
manipulating a signal.

Example#03:
Make a folder in "c:" directory with name "soundlab" copy the file "testsound.wav" to the
"soundlab" folder.
[soundsig, fs, nbits]=wavread('c:\soundlab\testsound.wav');
left=soundsig(:,1);
right=soundig(:,2);
L=length(left);
half(left(1:2:L);
reverse=flipud(left);
wavplay(soundsig,fs);

EXPLANATION:

The ‘wavread’ function reads the “wav” file and returns the sampled
audio signal in variable “soundsig”, “fs” is the sampling frequency & “nbits”
represents the number of bits. The “wavplay” function plays the audio signal
with sampling frequency “fs”.

ECHO:

Consider the following diagram input signal = “sig”.


In echo generator the original signal is attenuated, delayed and then
added to the original signal. This is the signal that we hear and experience an
echo effect.

Example#04:
function[sigx, echosig, reflectedsig]=echobt031018(sig,fs,td,attn);
zer=zeros(td*fs,2);
sigx=[sig; zer];
refectedsig=[zer; sig*attn];
echosig=sigx+reflectedsig;

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

Example#05:
att=0.2;
td=1;
[signal, fs, nbits]= wavread('c:\soundlab\testsound.wav');
[signalx, echo, ref]=echobt031018(signal, fs, td,att);
wavplay(signalx,fs);
wavplay(echo, fs);
The coding in example # 5 produces a “achosignal” giving the reflected
sound an attenuation of 0.2 and time delay of 1 second. Next the
“warplay(signalx, fs” plays the original signal and “wavplayecho,fs” plays the
echo version of the original signal with sampling frequency “fs”.
EXAMPLE # 6:
Replace the last line in “example # 3” “wavplay (soundsignal,fs)” with
the following values. Run the “example # 3” for each value given below.
1 wavplay(sound signal, fs);
2 wavplay (sound signal, 0.5* fs);
3 wavplay (sound signal, 2 *fs);
4 wavplay (half, fs);
5 wavplay (half, 0.5*fs);
6 wavplay (half, 2*fs);
7 wavplay ( reverse, fs);
8 wavplay (reverse, o.5*fs);
9 wavplay (reverse, 2*fs);
Turn on your “speaker/headphones” and hear the result for all above values
numbered 1 through 9 by running the example # 3 & changing last line to the
above values separately & hearing the output.

ASSIGNMENT # 1 (5%) DUE DATE: NEXT LAB

1. Implement example # 4 example # 5. Run the example # 5 for the following


values of ‘att’ and ‘td’ and explain what is the difference between the following.
Speakers /Headphones should be on to hear the output.

1) att =0.5 2) att =0.5 3) att =0.3 4) att=0.1


td=.5 td=1 td=1.3 td=2

2. Do the example # 6 and hear the output for all the values numbered 1 to 9.
Give explanation for each from 1 to 9 that what we hear? 3 what is the effect of
0.5fs and 2fs?

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

Exercise:
1. Consider a signal y(t) = sin(wt) where “w=2πf” Take the case when
f = 23 (Hertz). What should be the sampling frequency (fs) according to Nyquist
Criteria?

2. If we use fs = 460 (sampling frequency) in above question then does it


satisfy the Nyquist Criteria?
For fs = 460 how many samples are there in each cycle of “sin(wt)”.

3. Consider a signal s(t) = 50 cos (20πt) + 10 cos (100πt) what is the maximum
frequency component in the above signal (Remember the relation cos wt = cos
2πft)
Fmax = Hz
What should be the sampling frequency according to the Nyquist Criteria in the
above signal “s(t)”?

4. Refer to the echo block diagram given at back of page # 2. we want to


modify & implement a new echo generator with its block diagram below. Modify
the codes of example # 4 and example # 5 so that it implements, the system given
below

+
Input Sig 2 td Output Sig
2
Reflected Sig

Where = attenuation
& td = Time Delay

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXPERIMENT NO - 8

CONVOLUTION & LAPLACE TRANSFORM


Consider a discrete time system with input x [n] and output y[n]. when input
x[n] = s[n] then output is denoted by h[n] where h[n] is called then impulse
response of that LTI discrete system

X[n] Y[n] ∂ [n] ∂ [n]


System System

If impulse response h[n] is given then we ear find the system output y[n] for any
input x[n] by the following relation.

+∞
y[ n ] = x[n] * h[n] = ∑
k = −∞
x[k] h [n-k]
+∞
= h[ n ] x[n] = ∑
k = −∞
h [k] x [n-k]

CONVOLUTION IMPLEMENTATION IN MATLAB


To perform discrete time convolution x[n] & h[n], define vectors xn and hn & are
the command
Yn = conv(xn, hn)
This command assumes that the Ist element in ‘x’ and the Ist element in ‘h’
correspond to n = 0, 50 that the Ist element in the resulting output corresponds to
n=0. if this is not the case, than the output vector will be computed correctly but
the index will have to be adjusted.
For example if the Ist element in ‘h’ corresponds to n=-2 and the Ist
element in ‘x’ corresponds to n = -3 then the Ist element in ‘y’ corresponds to n =
-5
The command “conv” can also be used to multiply polynomials = suppose
that the co – efficient of a are given in the vector ‘a’ and the coefficients of b are
given in ‘b’ then the co efficient of the polynomial a b can be found as the
element of the vector defined by
ab= conv (a,b)

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

For example a(S) =S+1;


B=S+2

Than a= [1 1];
b= [1 2]
ab= conv(a,b) gives
ab= [1 3 2]
Therefor
a(s)*b(s)= S2 + 3S +2

Example 1:

x[ n ] = [1,1,2,1,2]
h[n] = [1,2,-1,1 ]
y[n]= [1,3,3,5,3,5,-1,2]
x =[1 1 2 1 2];
h =[1 2 -1 1];
y = conv [x,h];
y

TRANSFER FUNCTION REPRESENTATION

Transfer functions are defined in MATLAB by storing the co efficents of the


numerator and the denominator in vectors. Given a continuous time transfer
function

H ( s ) = B(s) /A(s)

B = bmsM + bM-1 SM-1 + bM-2 Sm-2 + ………… + b0

A = SN + aN-1 SN-1 + aN-2 SN-2 + ……………..+a0

Store the coefficients of B and A in the vectors

Num = [ bm bM-1 bM-2 ……….b0]

Den = [1 aN-1 aN-2 ……………..a0]

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

the names of the vectors are generally chosen to be num and den but any other
name can be used.

For example

H = 2s+3
s3+4s2+5
so num = [2 3]

den = [1 4 0 5]

Note that tall the coefficents must be included in the vector, even zero co –
efficients.
To find the zero, poles and gain of a transfer function from the vectors
‘num’ and ‘den’ which contain the co efficients of the numerator and
denominator polynomials type.
[z,p,k]= tf2zp(num,den)
The zeros are stored in z, poles are stored in p and the gain is stored in k
To find the numerator and denominator polynomials from z, p and k type.

(num,den) =zp2tf [z,p,k]


To compute the frequency response it (w) of a transfer function, store the
numeratror and denominator of the transfer function in the vectors num
and den define a vector w that contains the frequencies for which H(w) is to be
computed for example
W = a:b:c where ‘a’ is the lowest frequency ‘c’ is the highest
frequency and ‘b’ is the increment n frequency.
The command it = freqs (num, den, w)
Returns a complex vector ‘H’ that contains the value of ‘H(w)’ for each frequency
in ‘w’. To draw a bode plot of a transfer function which has been stored in the vet
ors ‘num’ and ‘den’ type bode (num, den).

INVERSE LAPLACE TRANSFORM


Given a continuous time transfer function if(t)

Where B bMsM + bM-1 sM-1 + bM-2 sM-2 + ………….+b0

A = sN + aN-1 sN-1 + aN-2 sN-2 + ………………+ a0

If M> = N the polynomial is not in a proper form. To convert an improper


polynomial to a proper one we require long division. After long division and the

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

partial fraction expansion we can get the required inverse laplace


Transformation.
The command used to covert an improper H to proper H is
[Q,R]=deconv(B,A)

This deconvolves vector A out of vector B. The result is returned in vector Q and
the remainder in vector R such that
B= conv (A,Q) + R

If A and B are vectors of polynomial co – efficeints deconvolations is equivalent


to division. The result of dividing B/A is Quotient Q and the remainder R.
The command
[R,P,k] =residues (B,A)

Returns the residues in column vector R, poles in column vector P and direct
terms in row vector K. K vector is empty if length (B) < length (A) I;e if H is in
the proper form.

Example # 2

H=s - 5 b = [1 -5]
S2-4s+3 a = [1 -4 3]

b = [1 -5]

a = [1 -4 3];

[r, p, k] = residue (b,a)

Example # 2 gives r = [-1 2], p= [3 1], k=[ ]

The general expression is given below


B(S) = K(S) + R(1) + R(2) + …. + R(n) --------------------- (A1)
A(S) S-P(1) S-P(2) S-(n)

Putting values of r, p and k in eq A1, we have


B(S) = 0 + -1 + 2 ----------------------- (B1)
A(S) S-3 S-1

Therefore matlab makes the task of creating partial fractions much more easier.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXAMPLE # 3
W e know e-at has Laplace 1/(s+a). We verify this from MATLAB using the
following code

Syms f t a;
f=exp (-1*t)
F=laplace(f)
F

EXAMPLE # 4
Laplace transform of t is 1/S2
Syms f t;
f=t;
F=laplace(t)
F

EXAMPLE # 5
Consider
H(s) = S -5 = B(s) = -1 + 2 .
S -4S+3 A(s) (S-3) (S-1)
2

This transfer function was considered in example # 2.


The inverse Laplace transform of this function is given by h(t) = -1 e3t + 2et. This
can be done by MATLAB command ‘ilaplace’ which computer inverse Laplace
transform
Syms f F;
F=2/(S-1) – 1/(S-3)
f=ilaplace(F)

EXAMPLE # 6
H(S)= S3+2S+1
S2-2S-1
This is an improper function we can convert this into proper function by
following method.
Quotient + Remainder . = q(s) + r(s) = H(s) = B(s)
Denominator a(s) A(s)
B=[ 1 0 2 1];
A=[1 -2 -1];
[q, r] = deconv (B, A);
Matlab gives q=[1 2]; and r= [0 0 7 3];applying formula
H(s)= (S+2) + (7S + 3) .
(S2-2S-1)
Thus this example shows how to use MATLAB to convert from an improper
transfer function to proper transfer function representation.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

Example # 7

H(s)= (S3-4S2+3S + 6)
(S2-4S+1)

B= [1 -4 3 6];
A= [1 -4 3]
[r p k]=residue(b, a)

Matlab gives r=[3 -3], p=[3 1], k=[1 0]

By using eq A1

B(S) = K(S) + R(1) + R(2) + …. + R(n) --------------------- (A1)


A(S) S-P(1) S-P(2) S-(n)

H(s)=(1S +0) + 3 . + -3 .
(S-3) (S-1)

H(s)= S + 3 . - 3 .
(S-3) (S-1)

Example # 8

H(S)= 3S+1 .
S2+2S+5

B=[3 1];
A=[1 2 5];
[r, p, k]= residue(B,A)
This code gives
r=[1.5+0.5j 1.5- 0.5j]; p=[-1+2j -1-2j]; k=[ ]

syms f s
f=(3S +1 )/ (S2 +2S +5);
ilaplace(f)

the inverse laplace is found to be


3e-tcos(2t)- e-tsin(2t)

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXPERIMENT NO – 9

Z TRANSFORM
When we work with continuous time signals & systems in the transform domain,
we use the Laplace Transform. When we work with discrete time signals we use
a different transform, called the Z transform. This transform is a function of the
complex variable ‘z’ which has real and imaginary parts.

∂ = α + jw ; z= est ; z=e(α + jw )T = e αT . e jwT

Ö z = |z| <z
where |z| =e αT , <z= wT

Consider a discrete time system which is given an input f[n]. The Z transform of
this input signal is defined as F[z] and is given below

+∞

Z(f[n]) =F(z) = ∑ f[n] z-n -----------------------------(A)


N=-∞

Z-1(F(z)) =f[n] = 1/(2ΠJ) § F(z) z n-1 dz -----------------(B)

f(n) Z F(z)

-1 -2 -n
b(1) + b(2) z + b(3) z +……………….+ b(n+1) z
H(z) = ___________________________________________ -------------------(C)
-1 -2 -m
a(1) + a(2) z + a(3) z +……………….+ a(m+1) z

H(z) = B(z) = R(1) . + R(2) . + …. + R(n) …+K(1)z-1+ K(2)z-1+…----


(D)
A(z) 1-P(1)z-1 1-P(2)z-1 1-P(n)z-1

Equation C & and D give the general form of a transfer function.


In equation D r1, r2, r3,…………..r(n) are the residues and p(1), p(2), p(3),
………..p(n) are the poles and k(1), k(2) ……… are the coefficant of given vector
‘k’.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

FUNCTIONS RELATED TO ZTYNSFORM


Following are the MATLAB commands which are related to the Z transform

1- freqz Æ used for calculating / displaying frequency response


2- impz Æ used for calculating / displaying impulse response
3- z place Æ plots the zeros and poles with unit circle
4- tf2zp Æ finds zeros, poles and gain from H = B/A
5- ZP2tf Æ Transforms from zero, poles , gain back to t = B/A
6- Residuez Æ Finds residues, poles, direct terms of partial fraction
7- Poly Æ convert roots to polynomial
8- Roots Æ computes roots of a polynomial
9- Conv Æ used for multiplying 2 polynomials A & B.

Example#1:
clc
syms a n;

xn=1^n;
% xn=a^n;
Xz=ztrans(xn);

disp('x[n] = ') ; pretty(xn);


disp('X(z) = ') ; pretty(Xz);

Example # 2:
Z Transform of x [n] = ( 1/2 ) U[n] + ( - 1/3 ) U[n]
clc
syms n ;

xn=(1/2)^n + (-1/3)^n ;
Xz=ztrans(xn);

disp('x[n] = ') ; pretty(xn);


disp('X(z) = ') ; pretty(Xz);

disp('X(z) = ');
[rXz,how]=simple(Xz);
pretty(rXz);

disp('X(z) = ');
pretty(simplify(Xz));

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

Example # 3:
X (z ) = 1 Æ Equation F
_____________________
-1 -1
( 1 – (1/4) z ) ( 1 – (1/2) z )

Inverse Z Transform of Equation F

clc
syms a z;

Xz= 1 /(( 1 - (1/4)*( z^-1 ) ) * ( 1 - (1/2)*( z^-1 ) ));


% Xz= 1 /( 1 - a*( z^-1 ) );
% Xz=z/(z-a);

disp('X(z) = ') ; pretty(Xz);

xn=iztrans(Xz);
disp('x[n] = ') ; pretty(xn);

Example # 4:
Zero Pole Plot for Equation F

close all;

b=[1];
a=[1 (-3/4) (1/8) ];
zplane(b,a);grid;
title('algebric manipulation for evaluating vector " a "');

p1=[1 (-1/4)];
p2=[1 (-1/2)];
p3=conv(p1,p2);
b=1;a=p3;
figure;zplane(b,a);grid;
title('Using " conv " for evaluating vector " a "');

r=[(1/2) (1/4)];
p=poly(r);
b=1;a=p;
figure;zplane(b,a);grid;
title('Using " poly " for evaluating vector " a "');

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

Example # 5:
Partial Fraction Expansion for Equation G

clc;
b=1;
a=[1 (-3/4) (1/8)];
[r,p,k]=residuez(b,a)

Example # 6:
Finding z , p & k from b & a

Consider a sequence x [ n ] with Z Transform X ( z ) given below

-1 -2
( 1+2z+z )
X(z) = ___________________ Æ Equation L
-1 -2
( 1 – (3/2) z + (1/2) z )

b=[1 2 1];
a=[1 (-3/2) (1/2)];
[z,p,k]=tf2zp(b,a)
zplane(b,a)

Put the values of z , p & k from coding above into the Equation M to find an equivalent
expression for X ( z ) involving Zeros , Poles and Gain. Therefore we have the Equation L
reduced to Equation M

k ( z – z(1) ) ( z – z(2) ) …………( z – z(n) )


X(z) = _______________________________________ Æ Equation M
( z – p(1) ) ( z – p(2) ) …………( z – p(n) )

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

Exercise:

1. Evaluate the z-transform of the following


a) u[n]
b) n
c) n2
d) an
e) n2 an
f) sin(bn)
g) cos(bn)
h) ansin(bn)
i) ancos(bn)
2. Consider the Z Transform X ( z ) defined by the following expression

X(z) = z Æ Equation P
______________
2
(3 z – 4 z + 1)

A) Evaluate the values of the vectors ‘b’ and ‘a’ for Equation P using
“Equations H , I & J ”

B) Evaluate the Partial Fraction expansion for Equation P & reduce it to form
given by Equation K. Use the “residuez” command for this purpose

C) Plot the Zeros and Poles of X( z ) .

D) Compare the Locations for Poles and Zeros from the Graph in Part C with
the partial fraction expansion done in part B

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXPERIMENT NO – 10

FOURIER SERIES
The French mathematician fourier found that any periodic waveform, that is a
waveform that repeats itself after sometime, can be expressed as a series of
harmonically related sinusoids. i:e sinusoids whose frequencies are multiples of a
fundamentals frequency (or first harmonic). For example a series of sinosids with
frequencies MHZ, 2MHZ, 3MHZ and so on, contains the fundamental frequency
of of 1MHZ, a 2nd harmonic of 2MHZ, a third harmonic of 3MHZ, and so on. In
general any periodic waveform can be expressed as.

X(t)= a0 + a1 + cos (1w0t) + a2cos(2w0t) + --------


+ b1 sin(1w0t) + b2 sin (2w0t) + ----

Therefore the general equation of a fourier series expansion of a signal x(t) can be
written as

(A) => x(t) = a0 + ∑ (an cos (nw0t) + bn sin(nw0t) )


n=1
t1+ T0

(B) => a0 =1/T0 § x(t) dt


t1

t1+ T0
(C) => an =2/T0 § x(t) cos ( nwot) dt n=1, 2, 3, ….
t1

t1+ T0
(D) => bn =2/T0 § x(t) sin ( nwot) dt n=1, 2, 3, ….
t1

This if we have any periodic signal “x(t)” we can find the values of a0, an and ba
and put these values in equation A to get the fourier series expansion of a signal.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXAMPLE # 1
FOURIER SERIES

X (t)
1

-4Π -3Π -2Π -1Π 1Π 2Π 3Π 4Π

The founer series representation of the signal x(t) shown above can be written as

x(t) = a0 + ∑ (an + cos (nw0t) + bn sin(nw0t) )


n=1

So we have to find a0, an and bn to represent x(t) by the above expression.

Put T0 = 2Π and w0 = 2Π/T0 =1 in eq (B), eq (C) and eq (D)

Solving results in a0 =1/2;


an= (2 / n Π) * sin (n Π / 2);
and bn= 0;

Thus x(t) = ½ + ∑ [(2/nΠ ) sin(nΠ/2) cos(nw0t) ] -------- (E)


n=1

by expanding the equation (E)

x(t) =1/2 + 2/Π [ cos (w0t) – 1/3 cos(3w0t) +1/5 cos(5w0t) -1/7 cos (7w0t) +
1/9 cos (9w0 t) + ….]

Thus the waveform only contain the odd harmonics i:e 1w0, 3w0, 5w0, 7w0, 9w0
and so on. If we continue the equation (f) given above upper infinity we get the
ideal waveform x(t) as shown in the figure. If we discard some upper harmonics
we will get ripples in the waveform.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXAMPLE # 2 (MATLAB IMPLEMENTATION)


“Squarewavefourierseries.m”

function [t, fourierseries]= squarwavefourierseries(har,tmax);


fo= 1/(2*pi);
wo= 2*pi*fo;
t=-tmax : 0.05 : tmax;
n=1: har;
ao=1/2;
an=(2./(n*pi)).* sin(n*pi/2);
bn= zeros (1, length(n));
y= zeros (har,length(t));
for n=1:har
y(n,:) = an(n) * cos( n* wo*t) + bn (n) * sin(n*wo*t);
end
fourierseries = ao + sum (y,1)

Save this function with the some name i:e “squarewavefourierseries”. This
function takes the harmonics in variable “har”. These are the number of
harmonics to be computed in the fourier series expansion. The variable “tmax”
contains the maximum time limit of the signal. The function returned
“fourierseries” variable which contains fourier series expansion and ‘t’ contains
the time.

Example # 2:
Close All;
tmax= 2*Pi;
[tx1,fx1] =squarewavefourierseries(2000, tmax);
Figure;
Plot(tx1,fx1); grid;
[tx2,fx2] =squarewavefourierseries(50, tmax);
Figure;
Plot(tx2,fx2); grid;
[tx3,fx3] =squarewavefourierseries(20, tmax);
Figure;
Plot(tx3,fx3); grid;
[tx4,fx4] =squarewavefourierseries(10, tmax);
Figure;
Plot(tx4,fx4); grid;
[tx5,fx5] =squarewavefourierseries(3, tmax);
Figure;
Plot(tx5,fx5); grid;

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

[tx6,fx6] =squarewavefourierseries(1, tmax);


Figure;
Plot(tx6,fx6); grid;

This coding calls the function “squarewavefourierseries” with different values


for “har” variable and plots the fourier series representation for each. Note that
as “har” values increases the waveform becomes more closer to the ideal one.

EXAMPLE # 3:
Function[t, fourierseries]= sawtoothfourierseries(har,tmax);
a=5;
fo=1/(2*pi);
wo= 2*pi*fo;
t= -tmax : 0.05 : tmax;
n=1: har;
ao=0;
an= zeros(1,length(n));
bn=(2*a)./(n.*n.*pi.*pi).*(sin(n*pi)-(n*pi).*cos(n*pi));
y=zeros(har,length(t));
for n=1:har
y(n,:)= an(n) * cos(n* wo*t)+ bn(n) * sin( n*wo*t);
end
fourierseries= ao+ sum(y,1);

Save this function with the some name i:e “sawtoothfourierseries”. This
function takes the harmonics in variable “har” and the max time limit in variable
tmax. These are the number of harmonics to be computed in the fourier series
expansion. The variable “tmax” contains the maximum time limit of the signal.
The function returned “fourierseries” variable which contains fourier series
representation for the sawtooth waveform. The ‘t’ variable is the axis for that
signal. As ‘n’ approaches to infinity, the waveform approaches to an ideal wave
form of sawtooth.

EXAMPLE # 4:
Close All;
tmax= 2*Pi;
[tx1,fx1] = sawtoothfourierseries (2000, tmax);
Figure;
Plot(tx1,fx1); grid;
[tx2,fx2] = sawtoothfourierseries (50, tmax);
Figure;
Plot(tx2,fx2); grid;
[tx3,fx3] = sawtoothfourierseries (20, tmax);

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

Figure;
Plot(tx3,fx3); grid;
[tx4,fx4] = sawtoothfourierseries (10, tmax);
Figure;
Plot(tx4,fx4); grid;
[tx5,fx5] = sawtoothfourierseries (3, tmax);
Figure;
Plot(tx5,fx5); grid;
[tx6,fx6] = sawtoothfourierseries (1, tmax);
Figure;
Plot(tx6,fx6); grid;

This coding calls the function “squarewavefourierseries” with different values


for “har” variable and plots the fourier series representation for each. Note that
as “har” values increases the waveform becomes closer to the ideal one.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXPERIMENT NO – 11

DISCRETE FOURIER TRANSFORM


DISCRETE TIME FOURIER TRANSFORM (DTFT)

DTFT of a signal x[n] can be defined as:


N − 1
X (ω ) = ∑ n = 0
x [ n ]e − jw n

It gives frequency spectrum of the signal. Since ω is continuous, its range is


continuous. So it is impossible to get a vector in MATLAB which covers entire
range of the DTFT.

DISCRETE FOURIER TRANSFORM (DFT)

N point DFT of a signal x[n] is defined as:

N − 1
j ( 2 π

− ) k n
X ( K ) = x [ n ]e N

n = 0

and IDFT can be obtained by the relation:

N − 1
1 − j ( 2 π

) kn
x[n ] = X (k )e N
N k = 0

N point DFT means taking N equidistant samples (Sampling in frequency


domain). DFT is a sampled version of DTFT.

Now you are dividing 2π in N samples.

2 π
Width =
N

In order to estimate DTFT of the signal, we may take DFT of the signal, while
taking large number of DFT points.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

FAST FOURIER TRANSFORM (FFT)

FFT is an efficient algorithm of calculation of DFT. It requires much less


computation, but it requires the number of samples to be an integer power of 2.
MATLAB provides FFT command to compute DFT using FFT algorithm

PROBLEM # 1:

The following functions are required in this lab

rectpuls ,length , abs , fft , ifft , fftshift

Read the help of these Matlab functions understanding the input and output
parameters.

PROBLEM # 2:

Consider the time vector given below

inc = 0.001;
t = -0.2+inc:inc:0.2;

Plot the magnitude of DFT of Rectangular pulse having width 0.025 and defined
for the time given above. Use the Matlab functions “ rectpuls ” & “ fft ”.The DFT
should be centered around zero. Use function “ fftshift ” to shift zero-frequency
component to center of spectrum

PROBLEM # 3:

Consider a continuous time signal xc ( t ) = cos ( 4000 π t ) defined for the time
given below

t = -5*To : To/50 : 5*To;

Here To is the Time Period of xc ( t ). Plot the signal and the magnitude of DFT
for this cosine signal versus frequency values. Choose a value of “ N ” which is
higher than the signal length

Remember that cosine signal has Fourier Transform given by

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

Xc ( w ) = π ∂ [ ω – ωo ] + π ∂ [ ω + ωo ] .

Thus you will get the DFT graph having 2 impulses at +2000 Hertz and -2000
Hertz.

PROBLEM # 4:

Plot the Inverse DFT for problem 3.Graphs for Problem 3 and 4 are shown below

-1
-2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5
Cosine Signal xt = cos(4000*pi*t) -3
x 10
300

200

100

0
-50 -40 -30 -20 -10 0 10 20 30 40 50
DFT of cosine ( Frequency Axis in KHz )
1

0.5

-0.5

-1
-2.5 -2 -1.5 -1 -0.5 0 0.5 1 1.5 2 2.5
Inverse Discrete Fourier Transform IDFT -3
x 10

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXPERIMENT NO - 12

INTRODUCTION TO GRAPHICAL USER INTERFACE


GUIDE — GUI Development Environment
GUIDE, the MATLAB Graphical User Interface development environment, provides a set of tools
for creating GUIs. These tools greatly simplify the process of laying out and programming a GUI.
This section introduces you to GUIDE and the layout tools it provides. When you open a GUI in
GUIDE, it is displayed in the Layout Editor, which is the control panel for all of the GUIDE tools.
The Layout Editor enables you to lay out a GUI quickly and easily by dragging components, such
as push buttons, pop-up menus, or axes, from the component palette into the layout area. The
following picture shows the Layout Editor.

Once you lay out your GUI and set each component’s properties, using the tools in the
Layout Editor, you can program the GUI with the M-file Editor. Finally, when you press the Run
button on the toolbar, the functioning GUI appears outside the Layout Editor window.
GUIDE Toolset
For more information on the full set of GUIDE development tools, see the following sections:
•“Laying Out GUIs — The Layout Editor” on page 4-2 — add and arrange objects in the figure
window.
•“Aligning Components in the Layout Editor” on page 4-9 — align objects with respect to each
other.
•“Setting Component Properties — The Property Inspector” on page 4-14 — inspect and set
property values.
•“Viewing the Object Hierarchy — The Object Browser” on page 4-16 — observe a hierarchical
list of the Handle Graphics objects in the current
MATLAB session.
•“Creating Menus — The Menu Editor” on page 4-17 — create a menu bar or a context menu for
any component in your layout.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

•“Setting the Tab Order — The Tab Order Editor” on page 4-27 — change the order in which
components are selected by tabbing.

GUI FIG-Files and M-Files


GUIDE stores GUIs in two files, which are generated the first time you save or run the GUI:
•FIG-file — a file with extension .fig that contains a complete description of the GUI figure layout
and the components of the GUI: push buttons, menus, axes, and so on. When you make changes
to the GUI layout in the Layout Editor, your changes are saved in the FIG-file.
•M-file — a file with extension .m that contains the code that controls the GUI, including the
callbacks for its components. This file is referred to as the GUI M-file. When you first run or save a
GUI from the Layout Editor, GUIDE generates the GUI M-file with blank stubs for each of the
callbacks. You can than program the callbacks using the M-file editor.
Note In the documentation for releases prior to Release 13, the GUI M-file was referred to as the
“application M-file.”

Using GUIDE Templates


GUIDE provides templates for several basic types of GUIs. You can modify these templates to
make your own GUIs. The advantage of using the templates is that you can create GUIs more
quickly and easily.
To view the templates, enter guide at the MATLAB prompt. This displays the GUIDE Quick Start
dialog, as shown in the following figure.

When you select a template in the left pane, a preview of it appears in the right pane. Clicking
OK opens the template in the Layout editor. See “Using GUIDE Templates” on page 3-6 for more
information about GUIDE templates. The next section, “Getting Started with GUIDE” on page 1-
1, provides a detailed example of how to create a GUI using GUIDE.

NOTE: for details about GUI, refer to the PDF document, buildgui.pdf available
in LAB folder.

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

Example:
The matlab code below builds a GUI which helps the user to create some basic
signals using graphical interface. Copy the code to a M-file and save it as
comgui2. Running this code will generate the GUI.

function varargout = comgui2(varargin)


% COMGUI2 M-file for comgui2.fig
% COMGUI2, by itself, creates a new COMGUI2 or raises the existing
% singleton*.
%
% H = COMGUI2 returns the handle to a new COMGUI2 or the handle to
% the existing singleton*.
%
% COMGUI2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in COMGUI2.M with the given input
arguments.
%
% COMGUI2('Property','Value',...) creates a new COMGUI2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before comgui2_OpeningFunction gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to comgui2_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help comgui2

% Last Modified by GUIDE v2.5 10-Dec-2004 14:32:39

% Begin initialization code - DO NOT EDIT


gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @comgui2_OpeningFcn, ...
'gui_OutputFcn', @comgui2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin & isstr(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

end

if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before comgui2 is made visible.


function comgui2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to comgui2 (see VARARGIN)

% Choose default command line output for comgui2


handles.output = hObject;

t=-1:0.01:1;
f=1;
w=2*pi*f;

handles.t=t;
handles.sin=sin(w*t);
handles.cos=cos(w*t);
handles.sinc=sinc(w*t);
handles.square=square(w*t);

% Update handles structure


guidata(hObject, handles);

% UIWAIT makes comgui2 wait for user response (see UIRESUME)


% uiwait(handles.MainWindow);

% --- Outputs from this function are returned to the command line.
function varargout = comgui2_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

% handles structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure


varargout{1} = handles.output;

% --- Executes on button press in SineButton.


function SineButton_Callback(hObject, eventdata, handles)
% hObject handle to SineButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

plot(handles.t,handles.sin);

% --- Executes on button press in CosineButton.


function CosineButton_Callback(hObject, eventdata, handles)
% hObject handle to CosineButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

plot(handles.t,handles.cos);
% --- Executes on button press in SquareButton.
function SquareButton_Callback(hObject, eventdata, handles)
% hObject handle to SquareButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

plot(handles.t,handles.square,'b*:');

% --- Executes on button press in SincButton.


function SincButton_Callback(hObject, eventdata, handles)
% hObject handle to SincButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)

plot(handles.t,handles.sinc);

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXPERIMENT NO – 13

INTRODUCTION TO SIMULINK - I
AMPLITUDE MODULATION

MATLAB Implementation (Coding) Of Amplitude Modulation

Amplitude Modulation
ts=1e-3;
a=2;
t=-2:ts:2;
fm=1;
fc=15;
wm=2*pi*fm;
wc=2*pi*fc;
message=sin(wm*t);
carrier=cos(wc*t);
am=(a+message).*carrier;
subplot(311);
plot(t,message);
xlabel('Message Signal');
subplot(312);
plot(t,carrier);
xlabel('Carrier Signal');
subplot(313);
plot(t,am);
xlabel('Amplitude Modulated (AM) Signal');

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

SIMULINK IMPLEMENTATION OF AMPLITUDE MODULATION

Example # 1

Co n sta n t

Mes s age Signal

S i n e Wa ve
S co p e

Am plitude Modulat ion (AM) SIgnal


P ro d u ct

C arrier Signal

S i n e Wa ve 1

AM = (A + message) * Carrier ÆA

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

The above is the block diagram for AM (amplitude modulation) in SIMULINK.


We have to implement equation A

Message Signal: fm=1 Hz, wm=2fm, phase = 0


Carrier Signal: fc=1 Hz, wm=2fc, phase = 0
Sample Time: 1e-3 for both message and carrier signal
Scope: Number of axis = 3
Simulation Parameters: Start time = -2, Stop time = 2
Sine Type: Time based, Amplitude = 1;

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

EXPERIMENT NO – 14

INTRODUCTION TO SIMULINK -II


TRIGNOMETRIC FOURIER SERIES (TFS)

Example # 1:

In Lab 10 we implemented above square wave using Fourier series expansion. In


this lab we will do it in SIMULINK but we will add up to 9 harmonics.
The general equation of above square wave was found in Lab 4 to be
1 2 ⎛ 1 1 1 1 ⎞
w (t ) = + ⎜ cos wO t − cos 3 wO t + cos 5 wO t − cos 7 wO t + cos 9 wO t + ... ⎟
2 π ⎝ 3 5 7 9 ⎠

Wo = 1

We have to implement the above equation up till 9Wot and see the output in
simulink.
So we require 5 sine wave blocks and 1 constant block of value 1/2. “Cosine” is
implemented by giving “sine” a phase shift of

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

SIMULINK IMPLEMENTATION

Example # 2:

Wo = 1

Because To= (Time Period)

fo = 1/To =

Wo = fo = 1

1/2 , coswot , cos3wot , cos5wot

cos7wot, cos9wot

Signal & Systems


Federal Urdu University of Arts, Science & Technology Islamabad – Pakistan Electrical Engineering

Simulink parameters

Start time = -15


Stop time = +15

Phase = pi/2 for all sine blocks

Sample time 1e^-3 for all sine blocks

Frequency (rad/sec) 1,3,5,7,9

Amplitude 2/pi, -2/3pi, 2/5pi, -2/7pi, 2/9pi

Signal & Systems

You might also like