Вы находитесь на странице: 1из 7

LAB # 1

INTRODUCTION TO MATLAB & SIGNALS


OBJECTIVE:
The objective of this lab is to familiarize students with the matlab environment. How data is
stored in matlab. How scalars, vectors and matrices are generated in matlab. How indexing is
done is matlab. How to access the values from vectors and matrices. What are the possible ways
for searching the help about any command in matlab. Brief introduction to basic commands for
generating vectors, trigonometric functions, arithmetic operators, logical operators. At the end
comprehensive detail about plotting the data in matlab and the complete customization of plots.

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 TO MATLAB
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., "lookfor convolution")
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. CREATING 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.

To open the m-file from within the Command Window type edit myfirstprogram %and then
press Enter or Return key.

3. 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.5000 4.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
4. 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= x / 2

y=[12345]
z=x+y
point by point mult/div use “.“

W = x.*y

Matlab has a large number of built in functions, some operate on each point of a
vector/matrix:

log([1 2 3 4])

round([1.5 2; 2.2 3.1])

a=[1 4 6 3]

sum(a)

mean(a)

max(a)

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
5. RELATIONAL OPERATORS IN MATLAB
Relational operators: =(equal), ~=3 (not equal), etc.

Let
a = [1 1 3 4 1]

b= (a == 1)

b = (a < 1)

b = (a > 1)

b = (a <= 1)

b = (a >= 1)

b = (a ~= 1)

6. PLOTTING:
You can plot arrays using MATLAB’s function plot. The function plot (.) is used to generate line
plots. The function stem(.) is used to generate “picket-fence” type of plots. More generally,
plot(X,Y) plots vector Y versus vector X. Various line types, plot symbols and colors may be
obtained.Using plot(X,Y,S) where S is a character string indicating the color of the line, and the
type of line (e.g., dashed, solid, dotted, etc.). Examples for the string S include:

r Red + plus -- dashed


g Green * star -o circle
b Blue s square

You can insert x-labels, y-labels and title to the plots, using the functions xlabel(.), ylabel (.) and
title (.) respectively. To plot two or more graphs on the same figure, use the command subplot.
The subplot (m, n, p) argument in the subplot command indicates that the figure will be split in
‘m’ rows and ‘ n’ columns. The ‘p’ argument takes the values 1, 2, . . . , m × n.
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.

7.1. CONTINOUS TIME SIGNALS

Run the following codes

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

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

c) Put two plots on the same axis


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

7.2. DISCRETE TIME SIGNALS


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

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

Multiple plots in a figure: subplot:


You can show a number of plots in the same figure window with the subplot function. It looks a little
curious at first, but getting the hang of it is quite easy. The statement
subplot(m,n,p)
divides the figure window into m × n small sets of axes and selects the pth set for the current plot
(numbered by row from the left of the top row).
In-Lab Tasks:
1. Provide the snapshot of all the codes mention in STEP 3.
2. Provide the snapshot of all the codes mention in STEP 4.
3. Provide the snapshot of all the codes mention in STEP 5.
4. Provide the snapshot of all the codes mention in STEP 7.1 & 7.2.
5. Write a MATLAB program to plot 6 different continuous sin signals in one window.
Provide the snapshot of the code and the graph.

Post Lab Tasks:


1. Extract the mentioned values from the given matrix (use 2 different 1 2 30 4 90 5
methods to extract these values). 0 0 0 0
2. Write a MATLAB generic code to take matrix values as user input 1 2 3 4 90 60
8 9 -1 5 90 7
of any dimensional matrix.
0
3. Write a MATLAB program to plot 4 different continuous & -4 -5 -2 6 90 80
discrete sin signals using subplot command. Provide the snapshot -7 -6 -3 7 90 90
of the code and the graph. 8 9 5 7 90 9
7 0
I/P Q1

Вам также может понравиться