You are on page 1of 72

SIGNAL AND SYSTEMS

ab vaqar
[COMPANY NAME] [Company address]
LAB#1 Introduction to MATLAB
MATLAB:

 is a high-performance, high-level language.


 stands for Matrix Laboratory
 can be used as a fancy calculator
 allows you to easily work with entire matrices rather than one number at a time
 is useful for anything that requires matrix and vector manipulations such as:
o Mathematical, scientific, engineering, statistical and financial problems
o Anything that requires plotting/visualizing and analyzing data
 comes with a basic set of tools for visualizing data and for performing calculations on
matrices and vectors
o For specific technologies, MATLAB provides toolboxes, which add to the basic
MATLAB functionality. We have the Image Processing Toolbox. Some other
toolboxes include:
 Statistics Toolbox
 Neural Network Toolbox
 Fuzzy Logic Toolbox
 Signal Processing Toolbox
 Wavelet Toolbox
 Financial Toolbox
 Bioinformatics Toolbox
 Database Toolbox

Matlab's graphical interface is written in Java and should be look similar on any OS. It is divided
into 4 main parts:
1. Command Window—this is where you type commands. Output or error messages
usually appear here too.
2. Workspace window—as you define new variables, they should be listed here.
3. Command History window—this is where past commands are remembered. If you want
to re-run a previous command or to edit it you can drag it from this window to the
command window or double click to re-run it.
4. Current Directory window—shows the files in the Current Directory.

Basic Operation

Creating Matrices

Everything in MATLAB is stored as a matrix or an array. To create a 1x1 array, or a scalar, you
can write:

a=5

To see that a is a 1x1 array, you can execute the command:

whos

who lists all of the variables, and whos lists the variables and the size.
To create a 4 x 4 matrix, you can use the following syntax.

b=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16]

or

b=[
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
]

 opening and closing square brackets are required for matrices larger than 1x1
 entries in a row are separated by white space or commas
 semicolons (;) mark the ends of rows in the matrix
 if you end any of the above statements with a semicolon (;), you will notice that
MATLAB does not echo the matrix to the display
 if you want to remind yourself of what is in b, you can type b ↩

You can also use MATLAB functions to generate matrices. The following table summarizes a few
of these functions:

Function Description

zeros(i,j) creates an i x j matrix of zeros

ones(i,j) creates an i x j matrix of ones

rand(i,j) creates an i x j matrix of random elements (between 0 and 1)

creates an i x j matrix of random elements drawn from a normal distribution with mean 0
randn(i,j)
and standard deviation 1

eye(i) creates an i x i identity matrix (a matrix of zeros with ones on the diagonal)

In the above table, i is the number of rows and j is the number of columns. Be careful, this may
confuse you if you are used to thinking in terms of x and y.

If you have a large amount of data, you may want to store your data in a file and load it into
MATLAB. When you are typing the contents of the file, each column value is separated by
horizontal white space and each row is on a new line. You require the same number of
elements in each row. For instance, you can create a text file (called myData.dat) with the
following contents:

5.0 3.2 6.8


1.4 12.6 3.9
7.1 16.4 8.3
9.3 5.2 4.7

To read in the file and store it in a variable called myData, you use the following command:

load myData.dat
Basic Matrix operations

Given b (below), the following table summarizes a few basic matrix operations and the results
of these functions:

b=
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Function Description Results

sum(b) Sums the values of each column* ans =


28 32 36 40
ans =
1
diag(b) Produces all of the elements along the diagonal
6
11
16
ans =
Transpose operator. The first column becomes the first row, the 1 5 9 13
b'
second column becomes the second row and so on 2 6 10 14
3 7 11 15
4 8 12 16
max(b) Produces the maximum values for each column* ans =
13 14 15 16
min(b) Produces the minimum values for each column* ans =
1 2 3 4
mean(b) Produces the average of each column ans =
7 8 9 10
mean2(b) Produces the average of all the numbers in the matrix ans =
8.5000

*It is great to know the maximum, minimum or sum of each column, but sometimes you want it
for the entire matrix. If you have a multidimensional matrix, you can turn it into a single column
matrix using the notation b(:) (discussed in the "Colon Operator" section below). For instance,
to get the sum of all the values in b, you can write:

sum(b(:))

which generates the answer below:

ans =
136
Subscripts

Given b (from above), if you want to access the element in the first row and the forth column,
you can use the notation

b(1,4)

The results are the following:

ans =
4

Notice two things:

 you don't start at index 0


 and the order is in row,column format.

You can also use a single index. In this case, MATLAB regards your matrix as one long column
that is the concatenation of each of the original columns. So, the first four entries in b when you
use a single index are: 1 5 9 13. So to access the same element as above, b(1,4), using a single
index, you would write: b(13).

MATLAB provides error checking so that you do read past the bounds of the matrix. For
instance, typing:b(1,5) results in:

??? Index exceeds matrix dimensions.

However, you can assign a value to a position that exceeds a matix's dimensions. For example if
you typeb(1,5)=17, you get the following matrix:
b=

1 2 3 4 17
5 6 7 8 0
9 10 11 12 0
13 14 15 16 0

In this case, a new (fifth) column has been created and it has been padded with zeros where no
values were specified.

Colon Operator

The colon operator(:) can be used to select ranges or blocks of values.

By typing b and hitting return, you will see the values in b:

b=
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

If you only want a subset of b, for instance, the inner square, you can type: b(2:3,2:3)

ans =
6 7
10 11

Let's break down the b(2:3,2:3) a little. MATLAB is accessing row two to three (2:3) inclusive
and column two to three (2:3) inclusive. Other than that, we are looking at the
same (row,column) notation that you are used to.

Outside of the subscript notation, the colon operator will create a row vector containing the
integers in the range specified. For instance, typing 2:8 results in:

ans =
2 3 4 5 6 7 8

You can also specify an increment or decrement (as a middle argument) with the colon
operator. For instance, typing 1:2:8 results in:

ans =
1 3 5 7

In this case, the increment is 2. Effectively, you are getting all of the odd numbers between 1
and 8.

Just to give you more examples, the following table summarizes the results of using the colon
operator both as a subscript to b and on its own:

Expression Result Description

ans =
Values 5 to 10.
5:10 5 6 7
8 9 10
ans =
Values 10 to 5 (using decrement of 1)
10:-1:5 10 9 8
7 6 5
ans = Row 1 to 2, Column 3 to 4 (gets the postage stamp values in the upper
b(1:2,3:4) 3 4 right hand corner of b)
7 8
This is using the single index notation. Remember it is a concatenation
ans =
of one column after the other. In this case, you are getting the values
b(5:8) 2 6 10
14 of the second column.

end is a special value which indicates the end of the matrix. This is
ans = single index notation. You could have also writtenb(end,end) to
b(end)
16 specify the ending row and the ending column.

This gets the values of the last column (rows 1 to 4 and column 4).
ans = Since you are specify all rows and the final column, you could also use
4 the notation:
b(1:4,4) 8 b(1:end,end)
12 or the colon operator on its own, which specifies the entire range of
16 rows
b(:,end)
ans =
13 14 15 Reverses the order of the rows so that the last row is first and the first
16 row is last.
b(end:-
9 10 11
1:1,:)
12 Read this as: Rows last to first with a decrement of 1 (end:-1:1) and all
5 6 7 columns(:)
8
1 2 3
4
ans =
1
5
9
13
2
6
10 Produces one long column, which is a concatenation of the individual
b(:) 14 columns. This is useful when using functions such asmax, min, or sum
3
7
11
15
4
8
12
16

Variables

 When you are declaring a variable, you do not need to specify the type or the
dimensions (think about how this compares to creating an array in C++)
 If there is another assignment statement using the same variable name, then MATLAB
will change the contents and, where necessary, allocate new storage.
 Variable names are case sensitive (b and B are two separate variables).
 Variable names are a letter followed by any number of letters, digits, or underscores.
Notice that there are no special characters allowed, for instance, the dollar sign ($) is
not valid in variable names as it is in some other languages
 Variable names can be any length, but only the first N characters of the variable will be
used. To see what N is, you can type:
namelengthmax
 Every variable declared is an array/matrix. For instance, myVar=2 creates a 1x1 matrix
with a single element (2).

Predefined Functions and Help

The basic MATLAB package provides you with a number of functions that are useful for working
with numbers and arrays. All of the following functions are part of the MATLAB
base: sin, cos, sqrt, abs, ceil, isprime,gcd, lcm, find, rot90, size, length, isempty, and isequal.
For a more extensive list of some of the functions available to the basic MATLAB package, you
can type the following commands:

help elfun (for more on elementary mathematical functions)

help specfun (for more on specialized mathematical functions)

help elmat (for more on elementary matrices and matrix manipulations)

For a list of the functions available to MATLAB's Image Processing Toolbox, you can type the
following command:

help images (for more on Image Processing Toolbox)

If you want more help on a specific function, you can type:

help functionName

For instance, if you want to know more about the sin function. You can type:

help sin

or better yet:

doc sin

which opens up an indexed help window with further details about sin.

sin is a built-in function, which means that it is part of the MATLAB core and you cannot see the
code behind this function. By contrast, there are functions such as acosd that are implemented
in M-files. The file (acosd.m) is stored
in: matlabroot\toolbox\matlab\elfun (where matlabroot is the installation directory of
MATLAB). You can view this file directly from MATLAB using:

type acosd.m (typing .m, in this case, is optional)

or to edit, you can type:

Concatenation
The concatenation operator is indicated with square brackets []. Without even knowing it, you
have been concatenating simple 1x1 arrays into multidimensional arrays with statements like
the following that creates a 2x2 array called A:

A=[1 2 ; 3 4]

To create an 4x4 array, which repeats the elements of A three times, you can write:

[A A; A A]

that results in:

ans =
1 2 1 2
3 4 3 4
1 2 1 2
3 4 3 4

You can also have two occurances of A (one below the other) and at the same time reverse the
second occurance of A as in:

[A;A(end:-1:1,:)]

that results in:

ans =
1 2
3 4
3 4
1 2
Modifying Matrices

Let's say you have created b as below:

b=
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16

If you want to change an element, you can do so using subscripts and assignment as in:

b(3,1)=3
which results in:

b=
1 2 3 4
5 6 7 8
3 10 11 12
13 14 15 16

You can change an entire row by specifying:

b(3,:)=3

which results in:

b=
1 2 3 4
5 6 7 8
3 3 3 3
13 14 15 16

You can replace the values in a row or column by using a 1 dimensional array with exactly the
correct dimension. For example, to replace the second column of b you can write:

b(:, 2) = [1 2 3 4]

which results in:

b=

1 1 3 4
5 2 7 8
3 3 3 3
13 4 15 16

You can also replace a subset of values in a row or column as follows:

b(3,1:3) = [1 2 3]

which results in:

b=

1 1 3 4
5 2 7 1
1 2 3 3
13 4 15 16

If you decide to delete anything, you can use [] (empty square brackets). For instance, to delete
the third row of b, you can write:

b(3,:)=[]

which results in:

b=

1 1 3 4
5 2 7 8
13 4 15 16

However, you cannot partially remove or replace a row or column. For instance, the following
statements:

b(1:2,2)=[]
b(3,2) = []

produces this error:

??? Subscripted assignment dimension mismatch.

You could, however, remove every fourth element and collapse the remaining elements into a
row matrix as in:

b(1:4:end)=[]

which results in:

b=
5 3 2 3 3 7 4 8 3
Operators

In MATLAB, there are both matrix operations and array operations. Addition (+)and subtraction
(-) for both arrays and matrices are done element by element. With other operations, for
instance multiplication, linear algebra specifies how matrices are multiplied; it is not element by
element multiplication.
Sometimes, you may want to use the element by element array operations. MATLAB separates
matrix operations from array operations by using a dot notation in front of the operators. The
following is a list and description of array operations. The corresponding matrix operations omit
the dot.

Matrix multiply.
If one side is a scalar all elements of the other side are multiplied by the scalar.
Otherwise, a matrix multiplication is performed and the number of columns in A must match the
A*B
number of rows in B.

See: help mtimes


Array multiply
If one side is a scalar all elements of the other side are multiplied by the scalar.
Otherwise corresponding entries in each side are multiplied together and both sides must be
A.*B
matrices with identical dimensions.

See: help times


Right matrix divide
If B is a scalar all elements of A are divided by the scalar.
A/B Otherwise, if possible, the equivalent of the following multiplication is performed: AB-1
Specifically this operation is performed: (B' \ A' ) '

See: help mrdivide


Right array divide
If B is a scalar all elements of A are divided by the scalar.
A./B Otherwise entries in A are divided by the corresponding entry in B and both sides must be
matrices with identical dimensions.

See: help rdivide


Left matrix divide
If A is a scalar all elements of B are divided by the scalar.
A\B Otherwise, if possible, the equivalent of the following multiplication is performed: A-1B. A
warning will be printed if the result is likely to be unreliable.

See: help mldivide


Left array divide
If A is a scalar all elements of B are divided by the scalar.
A.\B Otherwise entries in B are divided by the corresponding entry in A and both sides must be
matrices with identical dimensions.

See: help ldivide


Matrix power
If A is a matrix, it must be square and B must be a scalar. Then A^3 would be equivalent to AAA.
A^B
If B is a matrix, it must be square and A must be a scalar. Then A is raised to the matrix power B.

See: help mpower


Array Power
If A and B are scalars, A is raised to the power B.
If A is a scalar and B is a matrix, a matrix results where the elements are A raised to the power of
the equivalent entry in B.
A.^B If B is a scalar and A is a matrix, a matrix results where the elements are the elements of A raised
to the power of B.
If A and B are matrices, both must be the same size and a matrix results where the elements are
the elements of A raised to the equivalent element of B.

See: help power


Complex conjugate transpose
Array elements are flipped across the diagonal. In effect, in a 2D matrix the indices for an
element are exchanged.
A' The value of the imaginary portion of complex numbers is negated—the complex conjugate is
taken for all entries in the matrix.

Transpose
Array elements are flipped across the diagonal. In effect, in a 2D matrix the indices for an
A.' element are exchanged.
Complex numbers are merely transposed, their values are not affected.

See: help transpose

For a complete list of MATLAB operators type help ops into the command window. You can get
a description of the behaviour of an individual operator by typing help operator_mnemonic.

Moving Around

You may have already noticed that MATLAB provides you ways of cycling through commands
you have already typed and editing those commands. A list of commands is on the bottom, left
hand side, you can double click on any of those commands to repeat them.

In addition, you can use the up arrow or down arrow to move through previously typed
commands.

 Getting Started
 Matrices and Arrays
 Controlling Command Window Input and Output
 Using Semicolons (;)

In MATLAB, semicolons “;” have two main uses. They:

 define the end of row, etc. A=[1,2;3,4].


 tell Matlab not to display any output when you end a line with a semicolon ( ; ). This is
particularly useful when you generate large matrices or perform long scripted
computations.
LAB #2 Script files and Plotting Complex Function
Plotting Multiple figures

To plot more than one graph on the screen, use the command subplot(mnp) which partitions the
screen into an mxn grid where p determines the position of the particular graph counting the
upper left corner as p=1. For example,

≫ (211), ( , );
≫ (212), ( , ℎ );
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 command or plot
command. To return to a full screen plot, type
≫ (111)

If multiple plots are required to be plotted on the same figure, ‘hold on’ command is used. After
plotting one figure type ‘hold on’ and then plot the other figures. E.g. the above mentioned plots
using subplot command can be plotted on the same figure using the following:

≫ ( , );
≫ℎ ;
≫ ( , ℎ );

Plotting digital Data:

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

≫ ( , )

You can use stem(k,y,'filled') to get circles that are filled in.

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 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’ within 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');’ within MATLAB. Or, this can easily
be achieved through the path browser.

MATLAB M-files can either be ‘scripts’ or ‘functions’. Scripts are simply files containing a
sequence of MATLAB statements. Functions make use of their own local variables and accept
input arguments.Variables,which are used in a script m-file are all global variables.An example of
the use ofthe command for is:

≫ = 1: 10;
( )= ( );

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

≫ = 1: 10;
≫ = ( );
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

≫ ( <= 2)
= 1;
( >= 4)
= 2;

= 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:

≫ = (′ ℎ :′
Whatever the command is between the quotation marks, is displayed on the screen when the
M-file is run, and asks the user to enter an appropriate value.

Function M-files

As example of an M-file that defined a function, create a file in your working directory named
‘yplusx.m’ that contains the following commands:

≫ ( , )
≫ = + ;
The following commands typed within MATLAB demonstrate how this M-file is used:

≫ = 2;
≫ = 3;
≫ ( , )
MATLAB M-files can either be ‘scripts’ or ‘functions’. Scripts are simply files containing a
sequence of MATLAB statements. Functions make use of their own local variables and accept
input arguments. Variables,which are used in a script m-file are all global variables. The name of
a function, as defined in the first line of the M-file, should be the same as the name of the file
without the .m extension. Function M-files start with the command function. 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:

≫ = 1: 10;
( )= ( );

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

≫ = 1: 10;
≫ = ( );
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

≫ ( <= 2)
= 1;
( >= 4)
= 2;

= 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:
≫ = (′ ℎ :′
Whatever the command is between the quotation marks, is displayed on the screen when the
M-file is run, and asks the user to enter an appropriate value.

Loops in Matlab:

From other computer programming experiences, you should be familiar with the idea of creating
a loop to repeat the same task for different values. Here's an example of how to do that using
Matlab.

Suppose we want to generate an output vector where each element is the sum of the current
element and the element from 10 places back in an input vector. The task to be repeated is the
sum of two elements,we need to repeat this for each element in the vector past 10. The elements
of the vector x define the input ramp function to be integrated, y will hold the result, and k is the
loop index:
≫ = 3 ∗ (0: 0.01: 5) + 2;
≫ = ( ) ;
≫ = 11: ℎ( )
( ) = ( − 10) + ( )

As you all know that loops are inefficient in Matlab so we can rewrite this problem to use vector
addition by creating two new vectors. One which is x offset by 10 and the other which is x padded
with 10 zeros (since we can only add vectors of similar lengths)

≫ = 3 ∗ (0: 0.1: 5) + 2
≫ 1=[ (1,10) ];
≫ 2=[ (1,10)];
≫ = 1 + 2;
≫ (1: 10) = 0;
≫ = (1: 51);
Check the above two approaches. Which one is better?

Getting Started:

To define a symbolic variable, x, one can either use the command


or
≫ = (′ )
The first command is shorter; the second command issomewhat more versatile. One can define
other symbols in terms of symbolsthat have already been defined. If one gives MATLAB the
commands
≫ = (′ )
≫ = 1/(1 + ^2)
MATLAB responds with

≫ =
1/(1 + ^2)
The extremely observant reader will note that when MATLAB printed itsoutput here, it did not
indent the answer. The symbolic value 1/(1+x^2) is printed flush with the margin. In general,
MATLAB indents numericalvalues but prints symbolic values flush with the margin.

Plotting Symbolic Functions:

MATLAB has a separate command for plotting symbolic functions: ezplot.

The simplest way to use ezplot is to give the command ezplot(y). MATLABthen plots the function
over a default interval. With y as defined above,giving the command ezplot(y) causes MATLAB to
respond with the following figure:

If one would like to cause ezplot to plot from xmin to xmax, one can use the command

≫ ( ,[ ])

Substitutions:

MATLAB provides a command that allows us to substitute values, symbolicor otherwise, for
symbolic values. The command that performs the substitution is called ‘subs’. To substitute 2 in
y we defined previously, one givesMATLAB the command:
≫ ( , 2)
When given thiscommand, MATLABresponds with:

≫ =
0.2000
Note that, based on the indentation, we find that MATLAB has given us a numeric value and not
a symbolic one. It is possible to tell MATLAB thatour intention is to substitute the symbol 2and
not some representation of 2.(That is, we tell MATLAB to use an “ideal” 2 and to try to keep
everythingas ideal as possible.) To do this one uses sym(’2’) rather than 2. GivingMATLAB the
command subs(y, sym(’2’)) causes MATLAB to respondwith

≫ =
1/5
Note that the answer here is not indented. MATLAB knows that the symbolit is giving is the
correct and precise value it should be returning.It is also possible to have MATLAB substitute one
symbolic value foranother. The command subs(y,x^2), for example, causes MATLAB torespond
with

≫ =
1/(1 + ^4)
 Define the symbolic variable x. Make use of this variable to define the symbolic function
sin(1/x). Then, plot the function from 0 to 2. What do you see?

 Plot the following symbolic functions and substitute the value 2 and find result
 1/(1 + ^2)
 1/(1 + 2 ^2)
 (1 + ^2)

 Write the function that calculate the factorial of user defined input
LAB#3 CONTINUOUS AND DISCRETE SIGNALS IN MATLAB

Objective:
PLOT:2-D line plot

Syntax

plot(Y)

Description

plot(Y) plots the columns of Y versus the index of each value when Y is a real number. For
complex Y, plot(Y) is equivalent to plot(real(Y),imag(Y)).

STEM : Plot discrete sequence data

Syntax

stem(Y)
stem(X,Y)

Description

A two-dimensional stem plot displays data as lines extending from a baseline along the x-axis. A
circle (the default) or other marker whose y-position represents the data value terminates each
stem.

 stem(Y) plots the data sequence Y as stems that extend from equally spaced and
automatically generated values along the x-axis. When Y is a matrix, stem plots all
elements in a row against the same x value.
 stem(X,Y) plots X versus the columns of Y. X and Y must be vectors or matrices of the
same size. Additionally, X can be a row or a column vector and Y a matrix with length(X)
rows.
 Task:

Use ‘plot’ command to plot

 d=Sin(x), x = -pi:.1:pi;
 d= tan(sin(x)) - sin(tan(x)); x= -pi:pi/10:pi;
LAB #4 Properties of Signals and Various Systems
The purpose of this lab is to study the properties of continuous time and discrete time signals,
properties of systems and to corroborate them with the help of MATLAB.

Signal

In the most precise form a signal can be defined as information which is contained in a pattern
of variations of some form.

There are two types of signals; Discrete Time signals and Continuous Time signals.

Discrete time signals

Discrete time signals are defined only at discrete intervals of time, and consequently, for these
signals the independent variable takes on only a discrete set of values e.g. stock market index.

Continuous time signals

In case of continuous time signals the independent variable is continuous, and thus these signals
are defined for a continuum of values of time e.g. a speech signal as a function of time.

Transformation of the independent variable

1) Time Shift:

A simple example of transforming the independent variable of a signal is Time Shift which means
shifting a signal w.r.t time. There are 2 types of time shift; time delay and time advance.

Consider the following discrete time signals:

5.1 Original Signal x[n]


5.2 Time delayed Signal x[n-no]5.3 Time advanced Signal x[n+no]

The above three signals are identical in shape but are displaced or shifted in time relative to each
other.

Time delay:

If in a discrete time signal x[n-no], no is greater than zero (no> 0) then x[n-no] represents time
delayed version of the discrete time signal x[n].

Time advance:

If in a discrete time signal x[n+no], no is lesser than zero (no< 0) then it represents time advanced
version of the discrete time signal x[n].

Similar is the case with continuous time signals.


The following program can be used to achieve time shift: Let

≫ = [1 2 3 4 5 6 7];

≫ = [0 1 2 3 2 1 0];

≫ ( , )% gives the signal x[n]

≫ = + 3;

≫ ( , ) % gives time advance signal

≫ = − 3;

≫ ( , ) % gives the time delay signal

Applications:
Applications of time shift occur in radar and seismic signal processing, in which several receivers
at different locations observe a signal being transmitted through a medium (water, rock, air etc).
The difference in propagation time from the point of origin of the transmitted signal to any 2
receivers results in a time shift between the signals at the 2 receivers.

2) Time Reversal:

Time reversal is another basic transformation of the time axis.

Consider the following discrete time signal:

5.4 Original Signal x[n]

The time reversal version of x[n] is obtained by reflecting x[n] about n = 0. The time reversal signal
so produced is represented by x[-n].

5.5 Time reversed signal x[-n]

This can be achieved using the ‘fliplr’ command. Let,

≫ = [−3 − 2 − 1 0 1 2 3];

≫ = [ −1 1 2 2 3 4 5];

≫ ( , ) % gives the original signal

≫ 1= ( );


≫ ( , 1) % gives the time reversed signal

Similar is the case with continuous time signals.


Example:

If x(t) represents an audio tape recording then x(-t) is the same recording being played
backwards.

3) Time Scaling:

There are two types of time scaling; compression in time and expansion in time. Consider the
following continuous time signals:

5.6 Original signal x(t)

5.7 Expansion in time x(t/2) 5.8 Compression in time x(t/2)

When a signal is expanded in time it is compressed compared to the original signal and vice versa.

A simple program to demonstrate time scaling is:

≫ = [−2 − 1.5 − 1 − 0.5 0 0.5 1 1.5 2];

≫ = [1 1 1 1.5 1.5 1.5 1 1 1];


≫ (311), ( , ) % plots the original signal

≫ 1=2∗ ;

≫ (312), ( 1, ) % plots the compressed in time signal

1
≫ 2= ∗ ;
2

≫ (313), ( 2, )% plots the expanded in time signal

Example:

Let x(t) represent an audio tape recording then x(2t) represents the same audio tape being played
at twice the speed and x(t/2) represents the same audio tape being played at half the speed.

4) Amplitude Scaling:

Consider an amplitude scaled signal x(t). It’s amplitude scaled versions 2x(t) and 1/2x(t) can be
drawn as:

5.9 Original signal x(t)

5.10 Amplitude scaled signal 2*x(t)5.11 Amplitude scaled signal ½*x(t)


A simple program to demonstrate amplitude scaling is:

≫ = 1: 0.1: 100;

≫ = sin( ) ;

≫ (311), ( , ) % plots the original signal

≫ 1= 2∗ ;

≫ (312), ( , 1) % plots the scaled signal

1
≫ 2= ∗ ;
2

≫ (313), ( , 2) % plots the scaled signal

Note:

If we perform all of the above on a signal the shape of the signal remains the same. If we
encounter reflecting and time shifting together then first reflect the signal and then perform time
shifting.

Exercise

1) Given the following signal:

In MATLAB sketch:
a) x(t+1)
b) x(-t+1)
c) x(3/2t)
d) x(3/2t + 1)
LAB# 5 Signals, Periodicity and Harmonics
Signals are represented mathematically as functions of one or more independent variables; one
of the independent variables usually, is time. The purpose of this lab is to study two other
properties of signals i.e. periodicity and harmonics.

Even and Odd Signals

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

Hence a signal is even if:


( )= (− )
[ ]= [− ]
And a signal is odd if:

( ) = − (− )
[ ] = − [− ]
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.

Theeven part of a signal is give by:


1
( ) = [ ( )+ (− )]
2
The odd part of a signal is given by:

1
( ) = [ ( )− (− )]
2
Periodic signals

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

( )= ( + ) ( = 0,1,2,3, … )
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 with time T. A very commonexample of a
periodic signal is ‘sin’.
6.1 sin waveform periodic at 2π

The vertical (thin) line marked shows that the signal repeats itself after a time period T which in
this case is 2π.

A periodic signal in discrete time is defined analogously. Specifically, a discrete time signal x[n] is
periodic with period N where N is a positive integer, it is unchanged by a time shift of ‘N’ i.e.
[ ]= ( + ) ( = 0,1,2,3, … )
An example of a discrete time periodic signals can be:

6.2 discrete signal periodic at 7


The vertical line indicates the time period N = 7 after which the signal repeats itself.

A signal which is not periodic is called Aperiodic Signal.

Fundamental Period:

The fundamental period is the smallest positive value of ‘T’ (continuous) or ‘N’ (discrete) for
which the equations of periodicity hold.

Consider the above example of a sine function. In this case the signal is periodic at 2π, 4π, 6π and
so on, but the fundamental time period of the signal is 2π.Similarly in the case of discrete time
signal the signal is periodic at N=7, 14, 21 and so on but the fundamental period of the signal is
7.
Harmonics

Harmonics are integral multiples of a signal. Consider the signal cos(t + aT). Its harmonics will be
cos(2t), cos(3t), cos(4t) and so on. Similar is the case with discrete time signals where for sin[n +
bN] the harmonics can be sin[n], sin[2n], sin[3n] and so on. The fundamental harmonic is the first
harmonic of the signal i.e. when a=0 or b = 0. In the above examplescos(t) and sin[n] are
Fundamental harmonics.

6.3 harmonics of the sine function i.e. sin(t), sin(2t), sin(3t).

Even and Odd harmonics

If the harmonic of a signal/function has an odd co-efficient it is called as an odd harmonic. If the
coefficient is even, it's called an even harmonic.

Even, odd harmonics play an important role in signal construction. Different waveforms can be
obtained by adding one type of harmonic to another e.g. adding (infinite) odd harmonics of
sine/cosine waves generates a square wave. Both the waves will have a 90 degree phase shift
from each other.
Practically it's not possible to add up all the (infinite) number of harmonics to obtain a perfect
square wave but we can see as the number of harmonics is increased from 10 onwards, we can
obtain a decent enoughsquare wave.

6.4 Squre wave by odd sine harmonics


For a square wave to be generated (as shown in figure) from the odd harmonics of sine function
we use the following expression:
4
( )= (sin( ))

For a wave generation by odd harmonics of a cos function we use:

1 1 1
( ) = 4/ (cos( ) − cos(3 ) + cos(5 ) − cos(7 ) + ⋯
3 5 7

6.5 Square wave by first 4 harmonics of cos

Complex Exponential & Sinusoidal Signals

The most general case of complex exponential can be expressed and interpreted in terms of two
cases we normally come across i.e. real exponential and periodic complex exponential.

6.6 anexponantial signal

Complex signals:

If the coefficients of the exponential have an imaginary part then we have a complex signal at
our hands i.e. if in eat.

= +
= +
using Euler's equation we get:

= ( )+ ( )
Hence for r = 0 the real and imaginary parts of a complex exponential are sinusoidal. For r > 0
they correspond to growing exponential sinusoidal signals and for r <0 decreasing exponential
sinusoidal signals.

6.7 Decreasing Sinusoidal

6.8 Increasing Sinusoidal

Exercise

1. Decompose the following signal into its even and odd parts
[ ] = 1; ≥0
= 0; <0
2. Use Matlab to produce a plot of a discrete-time sinusoid with amplitude 2.0 and a
periodof T = 7.
3. Use Matlab to produce a plot of an exponentially-decaying sinusoid with the decay rate,
amplitude,and period selected by you.
4. Add up first 10 harmonics of ‘sin’ and plot the waveform. What is it like?
5. Add up first 10 harmonics of ‘cos’ and plot the waveform. What is it like?
LAB#6 ENERGY AND POWER
The Purpose Of This Lab Is To Calculate The Energy And Powers Of Given Signals

Energy:

In signal processing, the energy of a continuous-time signal x(t) is defined as

the energy of a discrete-time signal x(t) is defined as

Power:

The average power of a continuous-time signal x(t) and discrete time signal is defined as

For x(t) to be a power signal,

And

To understand why this is, think about a signal that has nonzero but finite power. Integrating
the power over all time gives you energy. When you integrate nonzero finite power over
infinite time, you get infinite energy.

An energy signal requires

and
Yet some signals are neither power nor energy types because they have unbounded power and
energy. For these,

Tasks:

For the following compute energy and power in matlab?

x[n]=(-0.5)^n u[n]

x[n]=2 e^(j3n) u[n]

x[n]=Sin(10n) u[n]

x[n]=3(-.2)^n u[n-3]
LAB# 7 Convolution
The purpose of this lab is to learn how to perform convolution in MATLAB and the different ways
in which Transfer Function of a system can be manipulated.

Transfer Function Representation

Transfer functions are defined in MATLAB by storing the coefficients of the numerator and the
denominator in vectors. Given a continuous-time transfer function:

( ) = ( )/ ( )
Where B(s) = bMsM+bM-1 sM-1+…+ b0 and A(s) = sN+ aN-1 sN-1+…+a0.Store the coefficients of B(s) and
A(s) in the vectors in order of descending powers of s:

≫ =[ … ]
≫ = [1 … ]
In this text, the names of the vectors aregenerally chosen to be num and den, but any other name
could be used. For example,

( ) = (2 + 3)/( +4 + 5)
is defined by:

≫ = [2 3];
≫ = [1 4 0 5];
Note that all coefficients must be included in the vector, even zero coefficients. Now, the
command:

≫ = ( , )
creates a LTI object H representing the Transfer Function.

To plot the step response, type:


≫ ( , )
To plot the impulse response, type:
≫ ( , )

Convolution

The convolution theorem states that convolving two sequences is the same as multiplying their
Fourier Transforms.

To perform discrete time convolution, y[n] = x[n]*h[n], define the vectors x and h with elements
in the sequences x[n] and h[n].Then use the command:
≫ = ( , ℎ)
This command assumes that the first element in x and the first element in h correspond to n= 0,
so that the first element in the resulting output vector corresponds to n=0. If this is not the case,
then the output vector will be computed correctly, but the index will have to be adjusted.For
example:

≫ = [1 1 1 1 1 1];

≫ ℎ = [0 1 2 3];

≫ = ( , ℎ);

[0 1 3 6 6 6 5 3]
x is indexed as described above, then:

[0] = 0, [1] = 1, …
In general, total up the index of the first element in h and the index of the first element in x, this
is the index of the first element in y.

For example, if the firstelement in h corresponds to n = -2 and the first element in x corresponds
to n= -3 then the firstelement in y corresponds to n = -5.
The command ‘conv’can also be used to multiply polynomials: suppose that the coefficients of
a(s) are given in the vector a and the coefficients of b(s) are given in the vector b, then the
coefficients of the polynomial a(s)b(s) can be found as the elements of the vector defined by:
≫ = ( , )
Given a continuous time Transfer function:
( ) = ( )/ ( )
Where B(s) = bMsM+bM-1 sM-1+…+ b0 and A(s) = sN+ aN-1 sN-1+…+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 partial fraction expansion we can get the
required Inverse Laplace Transformation as explained in the next lab.The command used to
convert an improperH(s) to a proper H(s) is:
≫[ , ]= ( , )
This deconvolves vector Aout of vector B. The result is returned in vector Q and the remainder in
vector R such that:
= ( , )+
If A and B are vectors of polynomial coefficients, deconvolution is equivalent to polynomial
division. The result of dividing B by A is quotient Q and remainder R.
Exercise

1) Plot the convolution of a unit pulse,


( )=1 0< <1

0 ℎ

with itself.Then, deconv the signal to check if you get the original signal back or not.

2) Assume an LTI system has an impulse response:


ℎ[ ] = 1 = ±1
2 =0
0 =0
determine the output of the system is the input of the system is:
[ ]=2 =0
3 =1
−2 =2
0 ℎ
LAB#8 Laplace Transform
The purpose of this lab is to explore the concepts of poles and zeros, partial fraction expansions
and the Laplace Transform using MATLAB.

Poles and Zeros

Poles are the roots of the denominator of a transfer function and zeros are the roots of the
numerator of a transfer function. The command:

≫ = ( )
finds the roots of a polynomial described by the vector a, and thus maybe used to determine the
zeros and poles of a Laplace Transform expressed as a ratio of polynomials in s. The elements of
a vector a correspond to descending powers of s.

For example consider the following Laplace Transform:


4 +6
( )=
+ −2
We may find the poles and zeros of this equation using the following commands:

≫ = ([4, 0, 6])

0 + 1.2247

0 − 1.2247

≫ = ([1, 1, 0, −2])

−1.0000 + 1.0000

−1.0000 − 1.0000

1.0000
Hence we identify a pair of complex conjugate zeros at s = + j1.2247, a pole at s = 1 and a pair of
complex conjugate poles at s = -1 + j.

The command poly(r) uses the poles and zeros specified in the vector r to determine the
coefficients of the corresponding polynomial.
To find the zeros, poles and gain of a transfer function from the vectors num and den which
contain the coefficients of the numerator and denominator polynomials, type:
≫[ , , ]= 2 ( , )
The zeros are stored in z, the 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:

≫[ , ]= 2 ( , , )
or
≫ = ( , , )
which creates a LTI object representing the pole-zero-gain form of system description.

Plotting the poles and zeros

The commands:
≫ ( )
≫ ( )
find the zeros and poles of an LTI object H. In order to plot these poles and zeros in the s-plane,
the following command is used:
≫ ( )
Consider a system

Partial Fraction Expansions

The ‘residue’ command finds the partial fraction expansion for a ratio of two polynomials. The
syntax is:
≫[ , , ]= ( , )
where b = the numerator polynomial,
a = the denominator polynomial,
r = the partial fraction expansion coefficients or residues,
p = the poles
k = a vector describing any terms in power of s.
If the order of the numerator is lesser than the order of the denominator, then k is an empty
matrix.
To illustrate this command, we find the partial fraction expansion for the Laplace Transform:
3 +4
( )=
+5 +8 +4
using the commands:
≫[ , , ]= ([3, 4], [1, 5, 8, 4])
=
−1.0000
2.0000
1.0000
=
−2.0000
−2.0000
−1.0000
=
[]
Hence, the residue r(1) = -1 corresponds to the pole at s = -2 given by p(1), the residue r(2) = 2
corresponds to a double pole at s = -2 given by p(2) and the residue r(3) = 1 corresponds to the
pole at s = -1 given by p(3). The partial fraction expansion is therefore:
−1 2 1
( )= + +
+ 2 ( + 2) +1
Consider a system having zeros at s = 0, s = ± j10 and poles at s = -0.5 ± j5, s = -3 and s = -4 with
gain = 2. We may determine the transfer function representation for this system, plot the pole
and zero representations in the s-plane and plot the system’s magnitude response using the
MATLAB commands:
≫ = [0, ∗ 10, − ∗ 10];
≫ = [−0.5 + ∗ 5, −0.5 − ∗ 5, −3, −4];
≫ = 2;
≫ = [ , , ]
/ /
2 ( ^2 + 100)
( + 4)( + 3)( ^2 + + 25.25)
≫ = ( )

2 ^3 + 200
+8 + 44.25 + 188.8 + 303
≫ ( )
The command ‘pzmap’ is used to plot the poles and zeros of the transfer function in the s-plane.
The following figure depicts the pole-zero plot:
8.1 Pole and Zero locations in the s-plane

Laplace Transform

It is commonly used to produce an easily solvable algebraic equation from an ordinary differential
equation. It has many important applications in mathematics, physics, optics, electrical
engineering, control engineering, signal processing, and probability theory.
The Laplace Transform of a system is given by:

( )= ( )

MATLAB uses the following command to find the laplace transform:


≫ = ( )
Where F is a continuous time function whoselaplace transform is to be found. In order to find
the inverse Laplace Transform:
≫ = ( )

Exercise

1) Determine the poles and zeros of the following systems and plot them in the s-plane:

a) ( )=

b) ( )=

c) ( )=

2) Find the Laplace Transform of the following signals:


a) u(t)
b) (t)
c) e-atu(t)
d) cos( t) u(t)
3) Instead of using the command ‘laplace’ create a function that will calculate the Laplace
transform and solve the problems given in part 1 using this function.
LAB#9 FOURIER SERIES
The purpose of this lab is to represent fourier series of signal

Theory

In mathematics, a Fourier series (English pronunciation: /ˈfɔərieɪ/) is a way to represent a (wave-like)


function as the sum of simple sine waves. More formally, it decomposes any periodic function or
periodic signal into the sum of a (possibly infinite) set of simple oscillating functions, namely sines and
cosines (or, equivalently, complex exponentials). The discrete-time Fourier transform is a periodic
function, often defined in terms of a Fourier series. The Z-transform, another example of application,
reduces to a Fourier series for the important case |z|=1. Fourier series are also central to the original
proof of the Nyquist–Shannon sampling theorem. The study of Fourier series is a branch of Fourier
analysis.

Formula:

the mathematical expression

is called a Fourier series.

Tasks:

Find the Fourier series of

Find the Fourier series of the function function


LAB# 10 Fourier Transform
Fourier Transform (FT)

The Fourier transform of a signal tells us not only about the frequency components of a signal,
but also about the magnitude of these components. To find the Fourier transform of a signal we
multiply the signal with e-jwt and take its integral over infinity (the reason and mathematics is
beyond ,the scope of this lab).

The formula used to calculate the Fourier Integral is:

( )= ( )

and the formula to find the inverse Fourier Transform is:


1
( )= ( )
2
Similarly for discrete time signals, the formula used to calculate the Fourier Transform is:

= ( )

and the inverse is given by:


1
[ ]= ( )
2

Fourier Transform in MATLAB

Let ( )=1 | | ≤ /2
=0 ℎ
Then the Fourier Transform of the signal can be found using the following MATLAB commands:

≫ = / ( );

≫ = (1 + )/2;

≫ = ( , + /2)– ( , − /2)

1/2 ∗ ( + 1/2 ∗ )/ ( + 1/2 ∗ ) − 1/2 ∗ ( − 1/2 ∗ )/ (− + 1/2 ∗ )

≫ ( )
1 + 1/2 1 − 1/2

2 | + 1/2 | 2 | − + 1\2 |

≫% ℎ ℎ

≫% ℎ ℎ

≫ = (exp(− ∗ 2 ∗ ∗ ∗ )∗ ( , , 1), , − , inf)

−1/2 ∗ ∗ (− exp(− ∗ ∗ ) + exp( ∗ ∗ ))/ /

≫ ( ( ))

sin

The simple command looks for a simple form of X(jw). The ezplot command wont give optimal
results so in order to plot the above Fourier Transform:
≫ = −20.005: 0.01: 20.005

≫ = ( , );

≫ ( , , , ( ( ))

Exercise

1) Find the Fourier Integral of the following signals:


a) ( )= >0
=0 ℎ
b) ℎ( ) = ( )
2) Find the Discrete Fourier Transform of the following signals:
a) [ ]
b)
c) sin( )

3) Write a program to calculate the inverse fourier transform/ integral of a signal and apply it
on part 1 and 2to recover the original signals given.
LAB#11 Discrete Fourier Transform
The purpose of transforms is to gain information about signal frequencies and their relative
magnitude. Fourier Series can be applied to periodic signals only otherwise the sinusoids shall
generate the mirror of the original signal, even if it be at infinity. Fourier Transform can be applied
to both periodic and aperiodic signals.
The purpose of this lab is to study the application of Discrete Fourier Transform on various signals,
in MATLAB.

Discrete Fourier Transform (DFT)

Input: Sampled

Output: Discrete

Discrete Fourier transform is for discrete time i.e. sampled signals in MATLAB. The transform
gives us the frequency spectrum of the frequencies present inside the signal.
The most important thing while taking the DFT of a signal is the ‘number of points' or ‘N’. It
indicates the number of points we are taking (from the signal) for the transform. As the number
of points increases so does the frequency response. There are three cases with regards to N
1. N < Length of signal
2. N = Length of signal
3. N > Length of signal
For the first case, we shall get a frequency response which doesn't show all the frequency
components present in the signal. For the second case we shall get a frequency response that
gives all the frequency components in the signal at their exact frequency value. As N exceeds the
length of signal, the DFT of the sampled signal becomes close to the transform of the original
signal i.e. DTFT.
14.1 DFT of a signal having frequencies of 60, 120 ans 240 Hz.
The DFT of a signal is given by the following equation:

( )= ( )/
( )

and the inverse DFT is given by:

1 ( )/
( )= ( )

Discrete Fourier Transform in MATLAB

When we sample a signal and want to get its Fourier transform we perform what is called a
Discrete Fourier Transform (DFT). The Discrete Fourier Transform of a sampled pulse will be a
scaled version of the Fourier transform of a pulse in continuous time.
MATLAB doesn’t have a built in command to compute the DFT. So in order to compute the DFT
we need to construct a function manually. Matlab uses FFT to compute the DFT as will be
discussed in a lab later on.
To represent the equation in MATLAB avoiding loops we transform the summation into its
equivalent matrix operations by:

≫ = 0: − 1;

≫ = 0: − 1;

≫ = − ℎ( );

≫ =[ (1, )];

≫ =2∗ ∗ / ;
≫ = ∗ exp(− ∗ ∗ );

The zero padding in the code is for case three only i.e. when the length of the signal is smaller
than the number of points on which we want to take the dft.

Exercise

1) Find the Discrete Fourier Transform of the following signals:


d) [ ] = [1,1,0,1]
e) = [5,4,3,2,1,0]
2) Write a program to calculate the inverse discrete fourier transform of a signal and apply it on
part 1 to recover the original signal given.
LAB# 12 Z-TRANSFORM

 Z transform in matlab
 Inverse z transform in matlab
 Inverse z transform using partial fraction
 Z transform and convolution
 Pole zero diagram

Ztrans: z-transform

Syntax

F = ztrans(f)

F = ztrans(f,w)

F = ztrans(f,k,w)

Description

F = ztrans(f) computes the z-transform of the symbolic expression f. This syntax assumes that f
is a function of the variable n, and the returned value F as a function of z.
Inverse z transform in matlab

iztrans: Inverse z-transform

Syntax

f = iztrans(F)

f = iztrans(F,k)

Description

f = iztrans(F) computes the inverse z-transform of the symbolic expression F. This syntax
assumes that F is a function of the variable z, and the returned value f is a function of n.
Examples:

POLE ZERO DIAGRAM IN MATLAB:

Zplane: Zero-pole plot

Syntax

zplane(z,p)

zplane(b,a)
zplane(Hd)

Description

This function displays the poles and zeros of discrete-time systems

Residuez: z-transform partial-fraction expansion

Syntax [r,p,k]=residuez(b,a)

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

Description:residuez converts a discrete time system, expressed as the ratio of two


polynomials, to partial fraction expansion, or residue, form. It also converts the partial fraction
expansion back to the original polynomial coefficients
LAB# 13 Introduction to Simulink
Simulink is a software package for modeling, simulating, and analyzing
dynamical systems. It supports linear and nonlinear systems, modeled in continuous time,
sampled time, or a hybrid of the two. Systems can also be multirate, i.e., have different parts that
are sampled or updated at different rates.

For modeling, Simulink provides a graphical user interface (GUI) for building models as block
diagrams, using click-and-drag mouse operations. With this interface, you can draw the models
just as you would with pencil and paper (or as most textbooks depict them).

Simulink includes a comprehensive block library of sinks, sources, linear and nonlinear
components, and connectors. You can also customize and create your own blocks

Models are hierarchical. This approach provides insight into how a model is organized and how
its parts interact. After you define a model, you can simulate it, using a choice of integration
methods, either from the Simulink menus or by entering commands in

MATLAB's command window. The menus are particularly convenient for interactive work, while
the command-line approach is very useful for running a batch of simulations (for example, if you
are doing Monte Carlo simulations or want to sweep a parameter across a range of values).
Using scopes and other display blocks, you can see the simulation results while the simulation is
running. In addition, you can change parameters and immediately see what happens, for "what
if" exploration. The simulation results can be put in the MATLAB workspace for post processing
and visualization. And because

MATLAB and Simulink are integrated, you can simulate, analyze, and revise your models in either
environment at any point.

Building a Simple Model

This example shows you how to build a model using many of the model building commands and
actions you will use to build your own models. The instructions for building this model in this
section are brief. The model integrates a sine wave and displays the result, along with the sine
wave. The block diagram of the model looks like this.

To create the model, first type simulink in the MATLAB command window. On Microsoft Windows,
the Simulink Library Browser appears.
To create a new model, select Model from the New submenu of the Simulink library window's
File menu. To create a new model on Windows, select the New Model button on the Library
Browser's toolbar.
To create this model, you will need to copy blocks into the model from the following

Simulink block libraries:

 Sources library (the Sine Wave block)


 Sinks library (the Scope block)
 Continuous library (the Integrator block)
 Signals & Systems library (the Mux block)

To copy the Sine Wave block from the Library Browser, first expand the Library Browser tree to
display the blocks in the Sources library. Do this by clicking on the Sources node to display the
Sources library blocks. Finally, click on the Sine Wave node to select the Sine Wave block.

Here is how the Library Browser should look after you have done this.

Now drag the Sine Wave block from the browser and drop it in the model window. Simulink
creates a copy of the Sine Wave block at the point where you dropped the node icon.

To copy the Sine Wave block from the Sources library window, open the Sources window by
double-clicking on the Sources icon in the Simulink library window. (On Windows, you can open
the Simulink library window by right-clicking the Simulink node in the Library Browser and then
clicking the resulting Open Library button.)
Simulink displays the Sources library window

Copy the rest of the blocks in a similar manner from their respective libraries into the model
window. You can move a block from one place in the model window to another by dragging
the block. You can move a block a short distance by selecting the block, then pressing the
arrow keys. With all the blocks copied into the model window, the model should look something
like this.

If you examine the block icons, you see an angle bracket on the right of the Sine Wave block
and two on the left of the Mux block. The > symbol pointing out of a block is an output port; if
the symbol points to a block, it is an input port. A signal travels out of an output port and into an
input port of another block through a connecting line. When the blocks are connected, the port

symbols disappear. Now it's time to connect the blocks. Connect


the Sine Wave block to the top input port of the Mux block. Position the pointer over the output
port on the right side of the Sine Wave block. Notice that the cursor shape changes to cross
hairs.

Hold down the mouse button and move


the cursor to the top input port of the Mux block. Notice that the line is dashed while the mouse
button is down and that the cursor shape changes to double-lined cross hairs as it approaches

the Mux block.

Now release the mouse button. The blocks are connected. You can also connect the line to the
block by releasing the mouse button while the pointer is inside the icon. If you do, the line is
connected to the input port closest to the cursor's position.
If you look again at the model at the beginning of this section, you'll notice that most of the lines
connect output ports of blocks to input ports of other blocks. However, one line connects a line
to the input port of another block. This line, called a branch line, connects the Sine Wave output
to the Integrator block, and carries the same signal that passes fromthe Sine Wave block to the
Mux block.

Drawing a branch line is slightly different from drawing the line you just drew. To weld a
connection to an existing line, follow these steps:

 First, position the pointer on the line between the Sine Wave and the Mux block

 Press and hold down the Ctrl key (or click the right mouse button). Press the mouse button,
then drag the pointer to the Integrator block's input port or over the Integrator block itself.

.
 Release the mouse button. Simulink draws a line between the starting point and the
Integrator block's input port.
Finish making block connections. When you're done, your model should look something like

this.
Now, open the Scope block to view the simulation output. Keeping the Scope window open, set
up Simulink to run the simulation for 10 seconds. First, set the simulation parameters by choosing
Simulation Parameters from the Simulation menu.

On the dialog box that appears, notice that the Stop time is set to 10.0 (its default value)

Close the Simulation Parameters dialog box by clicking on the OK button. Simulink applies the
parameters and closes the dialog box.

Choose Start from the Simulation menu and watch the traces of the Scope block's input. The
simulation stops when it reaches the stop time specified in the Simulation Parameters dialog box
or when you choose Stop from the Simulation menu.
To save this model, choose Save from the File menu and enter a filename and location. That file
contains the description of the model.

Engineering application:

Analog−to−Digital Conversion

One of the recently added Simulink blocks is the Idealized ADC Quantizer. Figure 19.1 shows how
this block can be used to discretize a continuous−time signal such as a clock. The Function Block
Parameters dialog box provides a detailed description for this block.

Figure 19.1. Model for Analog−to−Digital conversion

The settings specified for the Idealized ADC Quantizer are noted in Figure 19.1. The output data
types for the Clock and the Idealized ADC Quantizer blocks are specified as double. The input
and output waveforms are shown in Figure 19.2.

Figure 19.2. Input and output waveforms for the model of Figure 19.1
The Zero−Order Hold and First−Order Hold as Reconstructors

You might also like