0% found this document useful (0 votes)
7 views4 pages

Signal Lab 1

The document is a lab manual for a Signals & Systems course, introducing students to basic concepts in MATLAB, including arithmetic operations, control flow, and data structures such as matrices and vectors. It provides examples of MATLAB commands and functions, along with exercises to reinforce learning. The lab aims to familiarize students with the MATLAB environment and its capabilities for signal processing and system analysis.

Uploaded by

lifeline.vpr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Signal Lab 1

The document is a lab manual for a Signals & Systems course, introducing students to basic concepts in MATLAB, including arithmetic operations, control flow, and data structures such as matrices and vectors. It provides examples of MATLAB commands and functions, along with exercises to reinforce learning. The lab aims to familiarize students with the MATLAB environment and its capabilities for signal processing and system analysis.

Uploaded by

lifeline.vpr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Signals & Systems Lab Lab 01

Name: _______________ Roll No. ______________

Basic Concepts in MATLAB


Objective of the Lab:
The students will be introduced about MATLAB basics
Basic Arithmetic Operations and control flow in MATLAB
Introduction
MATLAB is a programming language and data visualization software package which is especially effective
in signal processing and system analysis.
The MATLAB Environment

Getting Help from Within MATLAB


If you know the name of a function which you would like to learn how to use, use the help.
>> help keyword
This command displays a description of the function and generally also includes a list of related
functions.
This command will display a list of functions that include the keyword in their descriptions.
Other help commands that you may find useful are info what and which.
MATLAB Variables — Scalars, Vectors, and Matrices
MATLAB stores variables in the form of matrices which are M x N, where M is the number of rows and N
is the number of columns. All elements of a matrix can be real or complex numbers.
Real scalar >> x=5
Complex scalar >> z = 3+4j (or >> x = 3+4i)
conj(z) =________angle(z) =________ real(z) = _______ imag(z) = __________ abs(z) = ___________
cart2pol= _________________, pol2cart = _______________
Row vector >> x=[1 2 3]
Column vector >> x = [1; 2; 3]
3x3matrix >> x=[1 2 3 ;4 5 6;7 8 9]
Inf >> result of dividing with zero
NaN >> Not a number, obtained with mathematically undefined operations, such as 0/0.
Arithmetic Operations
There are four different arithmetic operators:
+ Addition
- Subtraction
* Multiplication
Signals & Systems Lab Lab 01
/ Division(for matrices it also means inversion)
There are also three other operators that operate on an element by element basis:
.* multiplication of two vectors of the identical size, element by element
./ division of two vectors, element-wise
.^ raise to the power of the array elements
Example:-
>> X= [1,3,4]
>> Y= [4,5,6]
>> X+Y = __________
>> X*Y = __________
>> X.*Y = __________
Array Indexing
In MATLAB, all arrays (vectors) are indexed starting with 1, i.e., y(1) is the first element of the array y.
Note that the arrays are indexed using parenthesis (.) and not square brackets[.] as in C/C++. To create an
array having as elements the integers 1 through 6, just enter:
>> x=[1,2,3,4,5,6]
Alternatively, you can use the : notation,
>> x=1:6
The : notation above creates a vector starting from 1 to 6, in steps of 1. If you want to create a vector from1
to 6 in steps of say 2, then type:
x=1:2:6=__________________ i=2:4:17=__________________ j=20:-2:0= __________________

Extracting or inserting numbers in a vector can be done very easily. To concatenate an array, you can use
the [] operator, as shown in the example below:
>> x= [1:3 4 6 100:110]

To access a subset of the array, try the following:


>> x(3:7) = __________________
>> length(x) = _______________ % gives the size of the array or vector
Special characters and functions
Some common special characters used in MATLAB are:
pi (3.14...)
sqrt indicates square root e.g., sqrt(4)=2
^ indicates power(e.g., 3 2=9)
abs Absolute value | .| e.g., abs(-3)=3
; Indicates the end of a row in a matrix. It is also used to suppress printing on the screen
% Denotes a comment. Anything to the right of % is ignored by the MATLAB interpreter and is
considered
as comments
‘ Denotes transpose of a vector or matrix. It is also used to define strings,
e.g.,str1=’Signals and Systems’;
Contour Plots
>> [x,y] = meshgrid(-5:.1:5, -5:.1:5);
>> z = -(x.^2 + y.^2);
>> contour(x,y,z) %Develop 2-D contour plots from 3-D information
contourf(x, y, z, 20) % Filled contour with 20 levels
colorbar % Add color scale
xlabel('X-axis')
ylabel('Y-axis')
Signals & Systems Lab Lab 01
title('2D Contour Plot of z = -(x^2 + y^2)')
Surface Plots
>> surf(x,y,z),
xlabel('x'),
ylabel('y'),
zlabel('-(x^2+y^2)'); % Develop 3-D
surface >> surfc(x,y,z),
xlabel('x'),
ylabel('y'),
zlabel('-(x^2+y^2)'); % Develop 3-D surface with their contour on xy plane
Control Flow
MATLAB has the following flow control constructs:
• if statements
• switch statements
• for loops
• while loops
• break statements
The “if”, “for”, “switch” and “while” statements need to terminate with an end statement.
Examples:
IF: FOR loop:
>> X = -3; >> X= 0;
>> if X>0 str =’positive’ for X= 1:10
elseif X== 0 str = ‘equal’ X=X+1;
elseif X <0 str = ‘negative’ End
else str=’error’ Value of X after execution of loop
end =_________________________
Answer :- str = __________________________
BREAK:
Repeat above for X = 3
The break statement lets you exit early from a for
Answer :- str = _________________________
or a while loop:
>> x=-10;
WHILE:
while x<0
>> X= -10 ;
x=x+2;
>> while X < 0
if x == -2
X= X+1;
break;
end
end
Value of X after execution of loop =
end
____________________

Also do without the statement “x==-2” and


observe the change in result.
Relational Operators
Symbol Meaning:
<= Lessthanequal, < Less than, >= Greater than equal, > Greater than, == Equal, ~=Notequal
Logical Operators
Symbol Meaning:
& AND
| OR
~ NOT
Signals & Systems Lab Lab 01

Instructor Verification Sheet


i. Create a 2×3 Matrix using MATLAB functions zeros, ones, eye, rand triu and tril.

ii. Take matrices of same order and first perform “*” operator and then perform “.*” operator. Note
the difference and write it down.

iii. Use the command size and length for matrices given by 1:0.3:10 and 1:1:1.5.

iv. Determine ∑25 (n).


𝑛=0
Hint: use for loop.

v. What do WHO and WHOS do in Matlab?

vi. What is the difference between “clc” and “clear”?

vii. Create an array that is vector of length 36, with every element zero except at 18th position, which
has value 1.

viii. Generate a complex-valued matrix a= ones(1,3) + i*ones(1,3) and calculate the absolute & square
of all elements of this matrix.

You might also like