Signals & Systems Laboratory CSE-301L Lab # 01
Signals & Systems Laboratory CSE-301L Lab # 01
CSE- 301L
Lab # 01
OBJECTIVES OF THE LAB
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
Matlab will be used extensively in all the succeeding labs. The goal of this first lab is to gain familiarity
with Matlab and build some basic skills in the Matlab language. Some specific topics covered in this lab
are:
• Introduction to Matlab
• Matlab Environment
• Matlab Help
• Variable arithmetic
• Built in Mathematical Functions
• Input and display
• Timing functions
• Introduction to M‐files
‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐‐
2
1.1 WHAT IS MATLAB?
Double click on the MATLAB icon to launch and a command window will appear with the prompt:
>>
You are now in MATLAB. From this point on, individual MATLAB commands may be given at the program
prompt. They will be processed when you hit the <ENTER> key. The following figure shows the
screenshot of matlab.
3
1.3 LEAVING MATLAB
Online help is available from the MATLAB prompt, both generally (listing all available
commands). >> help
[a long list of help topics follows]
and for specific commands:
>> help command_name
If you want to search for all the commands related to some particular functionality, use the keyword
lookfor followed by a keyword that explains the functionality.
>> lookfor convolution
will return a number of commands that perform convolution related tasks.
1.5 VARIABLES
MATLAB has built‐in variables like pi, eps, and ans. You can learn their values from
the MATLAB interpreter.
>> eps
eps =
2.2204e‐16
>> pi
ans =
3.1416
4
1.5.1 Variable Assignment
The equality sign is used to assign values to variables:
>> x = 3
x=
3
>> y = x^2
y=
9
Variables in MATLAB are case sensitive. Hence, the variables "x" and "y" are distinct from "X"
and "Y" (at this point, the latter are in fact, undefined).
Output can be suppressed by appending a semicolon to the command lines.
>> x = 3;
>> y = x^2;
>> y
y=
9
At any time you want to know the active variables you can use who:
>> who
Your variables
are: ans x y
5
1.5.4 Saving and Restoring Variables
To save the value of the variable "x" to a plain text file named "x.value"
use >> save x.value x ‐ascii
To save all variables in a file named mysession.mat, in reloadable format, use
>> save mysession
To restore the session, use
>> load mysession
MATLAB uses some fairly standard notation. More than one command may be entered on a single line,
if they are separated by commas.
>> 2+3;
>> 3*4, 4^2;
Powers are performed before division and multiplication, which are done before subtraction and
addition. For example
>> 2+3*4^2;
generates ans = 50. That is:
2+3*4^2 ==> 2 + 3*4^2 <== exponent has the highest
precedence ==> 2 + 3*16 <== then multiplication operator
==> 2 + 48 <== then addition operator
==> 50
All arithmetic is done to double precision, which for 32‐bit machines means to about 16 decimal
digits of accuracy. Normally the results will be displayed in a shorter form.
>> a = sqrt(2)
a=
1.4142
>> format long, b=sqrt(2)
b=
6
1.41421356237310
>> format short
The arrow keys allow "command‐line editing," which cuts down on the amount of typing
required, and allows easy error correction. Press the "up" arrow, and add "/2." What will this
produce?
>> 2+3*4^2/2
Parentheses may be used to group terms, or to make them more readable. For
example: >> (2 + 3*4^2)/2
generates ans = 25.
MATLAB has a platter of built‐in functions for mathematical and scientific computations. Here is a
summary of relevant functions.
7
1.7 TIMING COMMANDS
Timing functions may be required to determine the time taken by a command to execute or an
operation to complete. Several commands are available to accomplish it:
1.7.1 Clock
CLOCK returns Current date and time as date vector. CLOCK returns a six element date vector
containing the current time and date in decimal form:
CLOCK = [year month day hour minute seconds]
The first five elements are integers. The second’s element is accurate to several digits beyond the
decimal point. FIX(CLOCK) rounds to integer display format.
1.7.2 Etime
8
1.8 INPUT & DISPLAY
1.8.1 INPUT
INPUT prompts for user input.
R = INPUT('How many apples')
gives the user the prompt in the text string and then waits for input from the keyboard. The input
can be any MATLAB expression, which is evaluated, using the variables in the current workspace,
and the result returned in R. If the user presses the return key without entering anything, INPUT
returns an empty matrix.
Enter a variable: 4
x=
A vector is entered by specifying [] and elements are inserted inside these brackets, separated by
space.
Enter a vector: [3 4 1]
x=
3 4 1
Enter a value
x=
9
1.8.2 DISP
DISP(X) displays the value of variable X, without printing the variable name. In all other ways it's
the same as leaving the semicolon off an expression.
DISP(‘string’) is another variation of the same function that is used to display a string on
the command prompt.
Example:
I am using MATLAB
1.9 M-Files
Typing errors are time‐consuming to fix if you are working in the command window because you need
to retype all or part of the program. Even if you do not make any mistakes, all of your work may be lost
if you inadvertently quit MATLAB. To preserve large sets of commands, you can store them in a special
type of file called an M‐file. MATLAB supports two types of M‐files: script and function M‐files. To hold a
large collection of commands, we use a script M‐file. The function M‐file is discussed in coming lab. The
script file has a '.m' extension and is referred to as an M‐file (for example, myfile.m
myfuncion.m, etc.). The commands in the script file can then be executed by typing the file name
without its extension in the command window. Commands in a script utilize and modify the contents of
the current workspace. It is possible to embed comments in a script file.
To make a script M‐file, you need to open a file using the built‐in MATLAB editor. There are two ways
to accomplish it:
1. From file menu, click NEW
2. Type edit on command line
A new window appears like one shown in the figure below.
10
When you are finished with typing in this new window, click File‐>Save to save this file. The extension
of this file be .m. In order to execute this program,
b. Take your name in command window e.g. name = ‘Ali’. Convert it into 8‐bit integer
format using int8 function.
-------------------------TASK 02--------------------------
Create an M-File to prove any five expressions from the following:
Use etime or tic toc functions to evaluate time taken for solving each of the five chosen
expressions.
12
grade point:
==============================================
Grade Grade Point
==============================================
A 4.00
A- 3.67
B+ 3.33
B 3.00
B- 2.67
C+ 2.33
C 2.00
C- 1.67
D+ 1.33
D 1.00
F 0
==============================================
------------------------------------------------------------------------------------------------------------------------------------------
13