Introduction To Matlab& Signals: Defining A Scalar

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

LAB # 1

INTRODUCTION TO MATLAB& SIGNALS


INTRODUCTION
This lab is to familiarize the students with MATLAB environment through it some preliminary
MATLAB functions will be also covered.
PROCEDURE:
Students are required to go through the steps explained below and then complete the exercises
given at the end of the lab.
1. INTRODUCTION TOMATLAB
i. Too add comment the following symbol is used "%".
ii. Help is provided by typing “help” in the command window and press ENTER
• Explore MATLAB’s help capability by trying the following:
>> help
>> help plot
>> help ops
>> help arith
iii. If you don't know the exact name of the topic or command you are looking
for,type "lookfor keyword" (e.g., "lookforconvolution")
iv. If after a statement “;” is entered then MATLAB will not display the result of
the statement entered otherwise result would be displayed.
v. Use the up-arrow to recall commands without retyping them (and down
arrow to go forward in commands).
vi. MATLAB is case sensitive so “ciit” is not same as “CIIT”

2. BASIC FUNCTIONALITIES OF MATLAB


Defining a scalar:
x=1
x=
1
Defining a column vector
v = [1;2;3]
v=
1
2
3
Defining a row vector
w = [1 0 1]
w=
101
Transpose a vector
W = w’
W=
1
0
1
Defining a range for a vector
X = 1:.5:5
X=
Columns 1 through 9
1.0000 1.5000 2.0000 2.5000 3.0000 3.50004.0000 4.5000 5.0000
Empty vector
Y = []
Y=
[]
Defining a matrix
M = [1 2 3; 3 2 1]
M=
123
321
Zero matrix
M = zeros(2,3) % 1st parameter is row, 2nd parameter is column.
M=
000
000
ones matrix
m = ones(2,3)
m=
111
111
The identity matrix
I = eye(3)
I=
100
010
001
Define a random matrix or vector
R = rand(1,3)
R=
0.9501 0.2311 0.6068
Access a vector or matrix
R(3)
ans =
0.6068
or
R(1,2)
ans =
0.2311
Access a row or column of matrix
I(2,:) %2nd row
ans =
010
I(:,2) %2nd col
ans =
0
1
0
I(1:2,1:2)
ans =
10
01
Size and Length
size(I)
ans =
33
length(I)
ans =
3
3. OPERATIONS ON VECTOR AND MATRICES IN MATLAB
MATLAB utilizes the following arithmetic operators;
+ Addition
- Subtraction
* Multiplication
/ Division
^ Power Operator
‘ transpose

Arithmetic operations
x=[ 1 2 3 4 5]
x=
12345
x= 2 * x
x=
2 4 6 8 10
x= x / 2
x=
12345
y=[12345]
y=
12345
z=x+y
z=
2 4 6 8 10
point by point mult/div use “.“
W = x.*y
W=
1 4 9 16 25
Matlab has a large number of built in functions, some operate on each point of a
vector/matrix:
log([1 2 3 4])
ans =
0 0.6931 1.0986 1.3863
round([1.5 2; 2.2 3.1])
ans =
22
23
a=[1 4 6 3]
a=
1463
sum(a)
ans =
14
mean(a)
ans =
3.5000
max(a)
ans =
6
a =[1 2 3; 4 5 6]
mean(a) %mean of each column
max(a) %max of each column
max( max([1 2 3; 4 5 6]) ) %to obtain max of matrix

4. RELATIONAL OPERATORS IN MATLAB


Relational operators: =(equal), ~=3 (not equal), etc.

Let
a = [1 1 3 4 1]
a=
11341
ind = (a == 1)
ind =
11001
ind = (a < 1)
ind =
00000
ind = (a > 1)
ind =
00110
ind = (a <= 1)
ind =
11001
ind = (a >= 1)
ind =
11111
ind = (a ~= 1)
ind =
00110

5. CONTROL FLOW IN MATLAB


To control the flow of commands, the makers of MATLAB supplied four devices a
programmer can use while writing his/her computer code
• the for loops
• the while loops
• the if-else-end constructions

• the switch-case constructions

6. CREATING FUNCTIONS USING M-FILES


Files that contain a computer code are called the m-files. There are two kinds of m-files:
the script files and the function files. Script files do not take the input arguments or
return the output arguments. The function files may take input arguments or return
output arguments. To make the m-file click on File next select New and click on M-File
from the pull-down menu. You will be presented with the MATLAB Editor/Debugger
screen. Here you will type your code, can make %changes, etc. Once you are done with
typing, click on File, in the MATLAB Editor/Debugger screen and select Save As… .
Chose a name for your file, e.g., myfirstprogram.m and click on Save. Make sure that your
file is saved in the directory that is in MATLAB's search path. If you %have at least two
files with duplicated names, then the one that occurs first in MATLAB's search path will
be executed.
To open the m-file from within the Command Window type edit myfirstprogram %and then
press Enter or Return key.
7. INTRODUCTION TO SIGNALS
Signals are broadly classified into continuous and discrete signals. A continuous signal will be denoted by x(t),
in which the variable t can represent any physical quantity. A discrete signal will be denoted x[n], in which
the variable n is integer value. In this lab we will learn to represent and operate on signals in MATLAB. The
variables t and n are assumed to represent time.

8. CONTINOUS TIME SIGNALS


Run the following
a) t = 0:2*pi;
plot(t,sin(t))

b) t = 0:0.2:2*pi;
plot(t,sin(t))

c) t= 0:0.02:2*pi;
plot(t,sin(t))
title('CT signal plot')
xlabel('t (Seconds)')
ylabel('y(t)')

d) Put two plots on the same axis


t = 0:0.2:2*pi;
plot(t,sin(t),t,sin(2*t))

9. Discrete Time Signals


Use stem to plot the discrete-time step-function.

a) n = -10: 10;
f = n >= 0;
stem(n,f)

ACTIVITY:
I. Provide the snapshot of all the codes mention in STEP 3.
II. Provide the snapshot of all the codes mention in STEP 4.
III. Provide the snapshot of all the codes mention in STEP 8.
IV. Provide the snapshot of all the codes mention in STEP 9.
V. Write a MATLAB program to plot 6 different continuous sin signals in one window.
Provide the snapshot of the code and the graph.
VI. Write a MATLAB program to plot 4 different continuous sin signals using subplot
command. Provide the snapshot of the code and the graph.
VII. Repeat above 2 steps for discrete signals.
Provide the snapshot of all the codes mention in STEP 3.

Provide the snapshot of all the codes mention in STEP 4.


Provide the snapshot of all the codes mention in STEP 8.

Write a MATLAB program to plot 4 different continuous sin signals using subplot
command. Provide the snapshot of the code and the graph.

Write a MATLAB program to plot 6 different continuous sin signals in one window.
Provide the snapshot of the code and the graph.
Provide the snapshot of all the codes mention in STEP 9.

Repeat above 2 steps for discrete signals.

You might also like