Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
95 views

Lab 1 DSP. Introduction Matlab For DSP

This document provides an introduction to using MATLAB and Octave for digital signal processing (DSP) labs. It outlines the requirements to pass the DSP lab and provides deadlines. It then covers topics like getting started with MATLAB, plotting functions in MATLAB, discrete-time signals, and more. Examples are provided with MATLAB and Octave code to illustrate key concepts like plotting sine waves, using titles and labels, and plotting discrete numbers.

Uploaded by

Trí Từ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

Lab 1 DSP. Introduction Matlab For DSP

This document provides an introduction to using MATLAB and Octave for digital signal processing (DSP) labs. It outlines the requirements to pass the DSP lab and provides deadlines. It then covers topics like getting started with MATLAB, plotting functions in MATLAB, discrete-time signals, and more. Examples are provided with MATLAB and Octave code to illustrate key concepts like plotting sine waves, using titles and labels, and plotting discrete numbers.

Uploaded by

Trí Từ
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lab1.

Introduction Matlab for DSP


WINTER SEMESTER 2021
INSTRUCTOR: MSc. Tri Bien
tri.bm@vgu.edu.vn

Requirement to pass the DSP lab:


- Attend all of the DSP lab tutorials.
- Submit all the DSP lab reports before the deadline.
- Do NOT cheat, copy another codes & reports

Deadline for Lab1: 6p.m: 4th-Apr-2022


1. Introduction: Matlab and Octave 2
Matlab Introduction 2
Octave Introduction 2
Comparison Matlab and Octave 2
Example 1.1 : Plotting the sine function in Matlab and Octave 3
Example 1.2 : If you need any help! 3

2. Getting Started: Matlab 4


Numbers 4
Variables 4
Operator 5

3. Plotting in Matlab 6
Three main approaches for plotting functions 6
Title and axis labels 8
Plotting a set of discrete numbers 9

4. Discrete-time Signals 10
Unit sample sequence: 10
Unit step sequence: 11

1
1. Introduction Matlab and Octave

a. Matlab Introduction1

MATLAB (an abbreviation of "matrix laboratory") is a proprietary multi-paradigm programming


language and numeric computing environment developed by MathWorks. MATLAB allows matrix
manipulations, plotting of functions and data, implementation of algorithms, creation of user
interfaces, and interfacing with programs written in other languages. Although MATLAB is intended
primarily for numeric computing, an optional toolbox uses the MuPAD symbolic engine allowing
access to symbolic computing abilities. An additional package, Simulink, adds graphical
multi-domain simulation and model-based design for dynamic and embedded systems. (Wikipedia)

b. Octave Introduction 2

GNU Octave is a high-level interpreted language, primarily intended for numerical computations. It
provides capabilities for the numerical solution of linear and nonlinear problems, and for
performing other numerical experiments. It also provides extensive graphics capabilities for data
visualization and manipulation. The program is named after Octave Levenspiel, a former professor
of the principal author. GNU Octave is normally used through its interactive interface (CLI and GUI),
but it can also be used to write non-interactive programs. The project was conceived around 1988
and at first it was intended to be a companion to a chemical reactor design course. The GNU Octave
language is largely compatible with Matlab so that most programs are easily portable. In addition,
Octave is free software licensed. (Wikipedia)

c. Comparison Matlab and Octave

MATLAB vs Octave are primarily used for the same purpose. Some differences are features and
syntaxes. Matlab consists of specially designed toolboxes which are not part of Octave. In some
cases, they are not fully compatible ; code written in Matlab can be crushed in Octave and vice versa.
Matlab's key advantage is the large number of ready-to-use tools. The main goal of Octave is to give
freedom to users to choose which software to use to run their code. It has drop-in compatibility with
Matlab. When octave is performed interactively, it saves the commands input in an internal buffer so

1
https://www.mathworks.com/products/matlab.html
2
https://www.gnu.org/software/octave/index

2
they can be retrieved and altered later, it can easily be integrated with another IDE like Google
Colab or Jupyter Notebook.
Example 1.1 : Plotting the sine function in Matlab and Octave

Matlab Octave

x = 0:pi/100:2*pi; x = 0:pi/100:2*pi;
y = sin(x); y = sin(x);
plot(x,y) plot(x,y)

The result will be the same in both Matlab and Octave.

Example 1.2 : If you need any help!

Matlab Octave

>> help <your_command> help <your_command>

>> help sin


'sin' is a built-in function from the file libinterp/corefcn/mappers.cc

-- sin (X)
Compute the sine for each element of X in radians.

See also: asin, sind, sinh.

2. Getting Started Matlab

3
a. Numbers

MATLAB is a high-precision numerical engine that can handle a wide range of numbers, including
integers, real numbers, and complex numbers. 3

Number types Example Matlab

Real number 1.23 1.23

Real number 4.23 x 107 4.23e7

Imaginary number − 1 1i or 1j

Complex number 3 + 2j 3 + 1j*2

Some constants π, ∞ pi, inf

Example 2.1 : Check your numbers type

>> x= 2
>> whos x

Name Size Bytes Class Attributes


x 1x1 8 double

>> check = isinteger(2)


check =0 % MATLAB(r) stores a real number as a double type by default.
>> check = isinteger(int8(2))
check =1 % It is True

b. Variables

The basic variable of the Matlab is a matrix or an array. The variables can be listed by the table
below.

Variable types Example Matlab

Scalar is a 1 X = 1 or X=[1] X = 1
× 1 matrix or a X = [1]
single number.

3
https://www.mathworks.com/help/matlab/numeric-types.html

4
Column vector x = [ 1 ; 2; 3; 4]
Or using transpose
x = [1 2 3 4]’

Row vector y = [ 5 6 7 8]

General matrix
A= [1 2 ; 3 4]

Matrix 2x2

c. Operator

Matlab contributes certain arithmetic and logical operators. For the complete list, you can go to the
link in the footnote 4.

4
https://www.mathworks.com/help/matlab/matlab_prog/matlab-operators-and-special-characters.html

5
Exercise 2.1 : We have 3x3 matrices A and B, scalar C and Vector D.

Using Matlab or Octave calculate the:

1. Multiplication by a Scalar: A*C


2. Matrix Addition A+B and Subtraction A-B and A-D
3. Matrix -Vector multiplication: A*D and B*D*C
4. Matrix -Matrix multiplication: A*B and Transpose A’*B
5. Dot operations: A.*B and A.*D and A./B

3. Plotting in Matlab

a. Three main approaches for plotting functions.

Example 3.1 Consider the following sum of sinusoidal functions.

Using MATLAB, we want to generate samples of x(t) at time instances 0:0.01:1. We will discuss three
approaches.

Approach 1:

The first approach using C or Fortran languages style, we use two for... end loops, one each on t and
k. This approach can be implemented in Matlab but it is the most inefficient.

t = 0:0.01:1; N =length(t); xt= zeros(1,N);


for n = 1:N
temp =0;
for k =1:3
temp = temp +(1/k)*sin(2*pi*k*t(n));
end
xt(n) = temp;
end
plot(t,xt)

6
Approach 2:

Using the time vector t = 0:0.01:1 and only one for...end loop. This approach has fewer lines of code
than the first one, so this approach is better than the first one.

t= 0:0.01:1 ; xt = zeros(1,length(t));
for k = 1:3
xt= xt + (1/k)*sin(2*pi*k*t);
end
plot(t,xt)

Approach 3:

Using matrix-vector multiplication. This is the most compact code and the most efficient execution in
MATLAB, especially when the number of sinusoidal terms is very large. For demonstration, we
consider only four values for t = [t1, t2, t3, t4].

which can be written in matrix form as

t=0:0.01:1; k=1:3;
xt= (1./k)*sin(2*pi*k'*t);
plot(t,xt)
Three approach has the same result :

7
b. Title and axis labels

Plot function plot(X,Y)5 creates a 2-D line plot of the data in Y versus the corresponding values in X.

Example 3.2 Plotting sinusoidal functions with title and axis labels

t=0:0.01:1; k=1:3; % sample points from 0 to 1 in steps of 0.01 and k from 1 to 3


xt= (1./k)*sin(2*pi*k'*t); % Evaluate sin function
plot(t,xt,'r'); % Create plot with red line
xlabel('t in sec'); ylabel('x(t)'); % Label axis
title('Plot of sin(2\pi k*t)'); % Title plot

5
https://www.mathworks.com/help/matlab/ref/plot.html

8
c. Plotting a set of discrete numbers

For plotting a set of discrete numbers (or discrete-time signals), we will use the stem6 command
which displays data values as a stem, that is, a small circle at the end of a line connecting it to the
horizontal axis.

t=0:0.01:1; k=1:3; % sample points from 0 to 1 in steps of 0.01 and k from 1 to 3


xt= (1./k)*sin(2*pi*k'*t); % Evaluate sin function
Hs = stem(t,xt,'b','filled'); %Plot discrete data (x,y), blue color, filled circle
set(Hs,'markersize',4); % Change the circle size
xlabel('t in sec'); ylabel('x(n)'); %Label axis
title('Stem Plot is sin(2\pi k*t)'); % Title plot

Exercise 3.1 Plotting discrete sinusoidal with t=0:1:40 includes title, axis labels

6
https://www.mathworks.com/help/matlab/ref/stem.html

9
4. Discrete-time Signals

a. Unit sample sequence:

In the Malab we create the function impseq(n0, n1, n2) in the impseq.m file.

function [x,n] = impseq(n0,n1,n2)


% Generates x(n) = delta(n-n0); n1 <= n <= n2
% ----------------------------------------------
% [x,n] = impseq(n0,n1,n2)
%
n = [n1:n2]; x = [(n-n0) == 0];

Example 4.1 Create impulse function at position 0 and range [-5:5]

n = [-5:5]; % Create range of function [-5:5]


x = impseq(0,-5,5); % Create impulse function at position 0 in range [-5:5]
stem(n,x); title('Unit Impulse sequences'); %plot discrete impulse sequences
xlabel('n'); ylabel('x(n)'); % Print the x and y labels

Exercise 4.1 : Generate and plot each of the following sequences over the
indicated interval

10
b. Unit step sequence:

In the Malab we create the function stepseq(n0, n1, n2) in stepseq.m file

function [x,n] = stepseq(n0,n1,n2)


% Generates x(n) = u(n-n0); n1 <= n <= n2
% ------------------------------------------
% [x,n] = stepseq(n0,n1,n2)
%
n = [n1:n2]; x = [(n-n0) >= 0];

Example 4.2 Create step function begin at position 0 and range [-5:5]
n = [-5:5]; % Create range of function [-5:5]
x = stepseq(0,-5,5); % Create step function begin at position 0 in range [-5:5]
stem(n,x); title('Unit Step sequences'); %plot discrete impulse sequences
xlabel('n'); ylabel('x(n)'); % Print the x and y labels

Exercise 4.2: Generate and plot each of the following sequences over the
indicated interval

11
Exercise 4.3 Sketch by hand the following signals and write a MATLAB script to
plot signal samples

Bibliography

1. Wikipedia, MATLAB. https://en.wikipedia.org/wiki/MATLAB.

2. Octave, Wikipedia. https://www.gnu.org/software/octave/index.

3. Vinay K. Ingle and John G. Proakis. 2011. Digital Signal Processing Using MATLAB (3rd. Ed)

CL-Engineering.

12

You might also like