MATLAB ESSENTIALS signals and system lab manual - Computer Programming


C C++ Java Python Perl Programs Examples with Output -useful for Schools & College Students

Sunday, October 30, 2011

MATLAB ESSENTIALS signals and system lab manual


In this course we will use software MATLAB to implement and simulate signals & systems algorithms.
The name MATLAB stands for MATrix LABoratory. Originally MATLAB was developed to deal with only one
single but universal data type: the matrix.
A matrix is defined by [1]
• Its associated name
• The number of rows
• The number of column
• The value of all matrix elements
The matrix data type includes vectors, for the case when either of the number of rows or the number of column
equals to 1; and it includes scalars when both the number of rows and columns equal 1. Furthermore, matrices
can be either real-valued or complex valued, which is the most general case.
There is a small tutorial in this exercise for newcomer to become familiar with the MATLAB fundamentals. It is
recommended to try out introduced methods while solving the described tasks.
1.1 How to get help?
There are several methods in MATLAB to access texts, which explain the usage and behavior of given
• The HELP command is the basic help feature to get help of a function. For example, to get help on the
SUM function, type at the command prompt:
help sum
• HELPWIN is used in the same manner as help, but it opens a separate window with advance search
facilities and links to related topics.
• HELPDESK uses a window of an installed web browser to display the help texts.
• If the name of a command is not exactly known, the LOOKFOR function helps by seeking through the
first comment line of all available functions and listing all functions where a desired expression is
1.2 Statement, expressions and variables:
MATLAB is an expression language. The expressions you type are interpreted and evaluated. MATLAB
statements are usually of the form
variable = expression, or simply expression
Expressions are usually composed from operators, functions and variable names. Evaluation of the expression
produces a matrix, which is then displayed on the screen and assigned to the variable for future use.
A statement is normally terminated with the carriage return. However, a statement can be continued to the next
line with three or more periods followed by a carriage return. On the other hand, several statements can be
placed on a single line if separated by commas or semicolon.
1.2.1 The variable ANS:
If the variable name and = sign are omitted, a variable ANS (for answer) is automatically created to which the
result is assigned.
1.2.2 Suppressing screen output:
If the last character of a statement is a semicolon, the printing is suppressed, but the assignment is carried out.
This is essential in suppressing unwanted printing of intermediate results.
1.2.3 Colon notation:
Colon notation is used to generate vectors, and reference submatrices, known as subscripting. Creative use of

this feature makes MATLAB programming code simple, readable and minimizes the use of loop, which slows
• An index vector is created by using the colon notation
first : last
For example, [1:5] creates the vector [1 2 3 4 5].
• An spacing can be introduced between two elements of a vector by using the colon notation
first : spacing : last
For example, [1:2:5] creates the vector [1 3 5].
Say you want to evaluate the expression a3 +√bd −4c , where a=1.2, b=2.3, c=4.5 and d=4. Then in the command
window, type:
a = 1.2;
ans =
Note the semicolon after each variable assignment. If you omit the semicolon, then MATLAB echoes back on
the screen the variable value.
1.3 Workspace:
The contents of all the variables are stored in the MATLAB workspace. This is the memory region allocated for
variable. • The command WHO lists all variable which currently exist in the workspace. • WHOS additionally
lists the size and amount of allocated memory. • The entire workspace or single variable can be cleared by using
the CLEAR command.
1.4 Elementary operations:
Algebraic expressions can be formed by the following basic operations:
• Transpose of a vector/matrix can be produced by .*
e.g. B.*
• + is used to add two vectors/matrices of identical size, or a vector/matrix and a scalar, e.g.
[3 2; 4 5] + [7 4; 9 6]
• - subtracts two vectors/matrices of identical size, or a vector/matrix
• .*performs element-wise multiplication of two vectors/matrices of identical size, or a vector/matrix and a
scalar. For example, to square all elements of , we may write
• ./performs element-wise division of two vectors/matrices of identical size, or a vector/matrix and a scalar.
For example, the reciprocal of all elements in is computed through
1. /B
• *performs vector/matrix multiplication. The number of columns in the first vector/matrix must
equal to the number of rows in the second. Example:
1.5 Real and complex matrices:
In general, any matrix within the MATLAB can be complex-valued. However, for efficient storage, MATLAB
distinguishes between real-valued and complex-valued matrices. Real valued matrices are matrices where the

imaginary parts of all matrix elements are zero. The following essentials must be known to deal with complexvalued
• The variables i and j are assigned, by default, the value i = √-1. This is used to define the
complex values. For example, 5 + j*10
generates a complex-valued variable.
The real part of a complex-valued matrix can be extracted by using the function REAL and imaginary part can
be extracted by using the function IMAG. Both functions valued matrices as outputs. The function CONJ is used
to produce the complex conjugate of a matrix.
The special character ’ generates the complex conjugate transpose of a matrix. A’
z=3 + 4i % note that you do not need the ‘*’ after 4
conj(z) % computes the conjugate of z
angle(z) % computes the phase of z
real(z) % computes the real part of z
imag(z) % computes the imaginary part of z
abs(z) % computes the magnitude of z
You can also define the imaginary number with any other variables you like. Try the following:
1.6 M-files:
MATLAB can execute a sequence of statements stored in a file. Such files are called M-files because they have
“.m” extension as the last part of their file name.
There are two types of M-types:
• Script files
• Function files
1.6.1 Script file:
In a script file, script is a sequence of commands as they could be entered at the prompt. The script allows to
execute the entire sequence multiple times in a comfortable way, or to test modified versions.
1.6.2 Function file:
In a function file, function has the additional feature of passing parameters. On calling a function, it may read
input arguments and after execution it may return output values. The “FUNCTION” command specifies the
input and output parameters of a function. It must be very first command in a function file. Only comment may
be written before the FUNCTION command. Any text after a % sign inside an m-file is comment.
1.7 Flow control:
In MATLAB flow control means that a program may not only be executed straight down from top to bottom but
with branches and repetitions.
• The FOR function allows to create a loop which is controlled by a counter.
• More general exit conditions can be implemented in a WHILE loop.
• The IF command allows for conditional programming, together with ELSE.
• All these commands come along with an END command which acts as a closing brace, together with the
opening brace, i.e. the introducing FOR, WHILE or IF command.

No comments:

Post a Comment