Lab 1 DSP. Introduction Matlab For DSP
Lab 1 DSP. Introduction Matlab For DSP
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
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)
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)
Matlab Octave
-- sin (X)
Compute the sine for each element of X in radians.
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
Imaginary number − 1 1i or 1j
>> x= 2
>> whos x
b. Variables
The basic variable of the Matlab is a matrix or an array. The variables can be listed by the table
below.
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.
3. Plotting in Matlab
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.
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].
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
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.
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
In the Malab we create the function impseq(n0, n1, n2) in the impseq.m file.
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
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
3. Vinay K. Ingle and John G. Proakis. 2011. Digital Signal Processing Using MATLAB (3rd. Ed)
CL-Engineering.
12