0% found this document useful (0 votes)
22 views

DSP Lab 1

Uploaded by

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

DSP Lab 1

Uploaded by

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

NATIONAL UNIVERSITY OF

TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING

Experiment No: 01
Introduction to MATLAB and generating Discrete Signals
Objective:
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
 Basic Operations in MATLAB
 Defining a scalar, vector, matrix
 Operations on vectors and matrices
 Applying Relational operators
 Discrete Signals in MATLAB

1. Introduction to MATLAB
MATLAB is a commercial "MATrix LABoratory" package, by MathWorks, which operates as an
interactive programming environment with graphical output. The MATLAB programming language is
exceptionally straightforward since almost every data object is assumed to be an array. Hence, for
some areas of engineering MATLAB is displacing popular programming languages, due to its
interactive interface, reliable algorithmic foundation, fully extensible environment, and computational
speed.

2. MATLAB Environment
a. Entering and Leaving 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.
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING

b. Leaving MATLAB
A MATLAB session may be terminated by simply typing
>> quit

or by typing

>> exit

at the MATLAB prompt.

c. Creating M-File

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 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 withtyping, 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 the Command Window, type edit myfirstprogram and press Enter
or Return key.
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING

3. Basic Operations in MATLAB


a. Defining a scalar, vector, matrix

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=
1 0 1

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
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING

Empty vector
Y=[]
Y=
[]
Defining a matrix
M = [1 2 3; 3 2 1]
M=
1 2 3
3 2 1
Zero matrix
M = zeros (2, 3) % 1st parameter is row, 2nd parameter is column.
M=
0 0 0
0 0 0
Ones matrix
m = ones (2, 3)
m=
1 1 1
1 1 1
The identity matrix
I = eye (3)
I=
1 0 0
0 1 0
0 0 1
Define a random matrix or vector
R = rand (1, 3)
R=
0.9501 0.2311 0.6068
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING

Access a vector or matrix


R (3)
ans =
0.6068
Or
R (1,2)
ans =
0.2311

Access a row or column of a matrix


I (2, :) %2nd row
ans =
0 1 0
I (: 2) %2nd col
ans =
0
1
0
I (1:2, 1:2)
ans =
1 0
0 1
Size and Length
size (I)
ans =
3 3
length (I)
ans =
3
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING

b. Operations on vectors and matrices

MATLAB utilizes the following arithmetic operators;


+ Addition
- Subtraction
* Multiplication
/ Division
^ Power Operator
‘ transpose

Arithmetic operations
x= [1 2 3 4 5]
x=
1 2 3 4 5

x= 2 * x

x= x / 2

y = [1 2 3 4 5]

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)
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING

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

c. Relational operators

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


Operator Description
< Less than
<= Less than or equal to
> Greater
>= Greater or equal to
== Equal to
~= Not equal to

3. Signals in MATLAB
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. In this lab, we will only
focus on Discrete time signals.

a. Discrete Time Signals


In MATLAB, finite-duration sequence (or discrete time signal) is represented by row vector of
appropriate values. Such representation does not have any information about sample position n.
Therefore, for correct representation, two vectors are required, one for x and other for n. Consider the
following finite duration sequence & its implementation:
x(n) = { 1 -1 0 2 1 4 6 }
>> n = [-3:1:3]

n=

-3 -2 -1 0 1 2 3

>> x = [1 -1 0 2 1 4 6]

x=

1 -1 0 2 1 4 6
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING

>> x = -3:3

x=

-3 -2 -1 0 1 2 3

>> x = 1:0.5:4

x=

1 1.5 2 2.5 3 3.5 4

b. Plotting in MATLAB:

Run the following codes

Continuous Signals

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

Put two plots on the same axis


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

Discrete Signals

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

n = -10: 10;

f = n >= 0;

stem(n,f)
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING

4. Signals Plotting:

PLOT(X,Y) plots vector Y versus vector X.


TITLE('text') adds text at the top of the current plot.
XLABEL('text') adds text beside the X-axis on the current axis.
YLABEL('text') adds text beside the Y-axis on the current axis.
GRID, by itself, toggles the major grid lines of the current axes.
GTEXT('string') displays the graph window, puts up a cross-hair, and waits for a mouse
button or keyboard key to be pressed.
SUBPLOT(m,n,p) breaks the Figure window into an m-by-n matrix of small axes.
, or
SUBPLOT(mnp),
STEM(Y) stemDiscrete sequence or "stem" plot. STEM(Y) plots the data sequence Y as
stems from the x axis terminated with circles for the data value.
SEMILOGX(...) is the same as PLOT(...), except a logarithmic (base 10) scale is used for the
X-axis.
SEMILOGY(...) is the same as PLOT(...), except a logarithmic (base 10) scale is used for the
Y-axis..

Multiple Graphs:
There are two ways to make multiple plots on a single graph.
One way is with the hold command. The command hold freezes the current graphics screen so that
subsequent plots are superimposed on it. Entering hold again releases the "hold". The commands hold
on and hold off are also available.

t = 0: 0.01: 2pi;
y1 = sin(t);
y2 = sin(2t);
y3 = sin(4t);
plot(t, y1, '-', y2, ‘*', y3, '+')

Plotting colors and line styles:


NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING

Subplot:

% Line plot of a chirp


x = 0:0.05:5;
y = sin(x2);
subplot(2,1,1)
plot(x,y);
% Bar plot of a bell shaped curve
x = -2.9 : 0.2 : 2.9;
subplot(2,1,2),
bar(x,exp(-x.*x));

Line Plots of 3-D Data:


If x, y, and z are three vectors of the same length, plot3(x,y,z) generates a line in 3-D through the
points whose coordinates are the elements of x, y, and z.
Then it produces a 2-D projection of that line on the screen. For example, these statements produce a
helix.
t = 0:pi/50:10*pi;
plot3(sin(t),cos(t),t,'LineWidth',3)
axis square; grid on

In-Lab Assignment:
1. Provide the snapshot of all the codes mention in Section 3.
2. Provide the snapshot of all the codes mention in Section 4.
3. Write a MATLAB program to plot 6 different continuous sin signals in one window. Provide
the snapshot of the code and the graph.
NATIONAL UNIVERSITY OF
TECHNOLOGY (NUTECH)
DEPARTMENT OF ELECTRICAL
ENGINEERING

Post Lab Tasks:

[ ]
10 20 30 40 90 50
1 2 3 4 90 60
8 9 −1 5 90 70
−4 −5 −2 6 90 80
−7 −6 −3 7 90 90
8 9 5 77 90 90

1. Extract the mentioned values from the given matrix (use 2 different methods to extract these
values).
2. Write a MATLAB generic code to take matrix values as user input of any dimensional matrix.
3. Write a MATLAB program to plot 4 different discrete sin signals using subplot command.
Provide the snapshot of the code and the graph.

Comments:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy