LAB 1: Overview of DSP LAB (EEE 3218) Objectives
LAB 1: Overview of DSP LAB (EEE 3218) Objectives
LAB 1: Overview of DSP LAB (EEE 3218) Objectives
Student’s Information:
1. Name:
2. Student ID:
3. Email ID:
LAB 1:
Overview of DSP LAB (EEE 3218)
Objectives
The main objectives of this lab are:
PRELAB TASKS
• Read this laboratory tutorial carefully before coming to the laboratory class, so
that you know what is required.
• Familiarize yourself with relevant MATLAB functions and codes necessary for
this experiment.
• Do not bring any prepared MATLAB code in the lab with any portable device.
1
PART 1
Introduction
A signal is a function of a set of independent variables with time being perhaps the most
prevalent single variable. The signal itself carries some kind of information available for
observation. Most signals of interest in practice are recorded values of a physical quantities,
represented as a 1-D functions, such a time function , x(t) like speech or 2-D/3-D functions,
such as a spatial function f(x,y) like image or function of both space and time f(x,y,t) like
video . A signal carries information and contains energy.
By the term processing, we mean operating in some fashion on a signal to extract some useful
information. In many cases, this processing will be a nondestructive “transformation” of
the given data signal; however, some essential processing methods turn out to be irreversible
and thus destructive.
Finally, the word digital means that the processing is done with a digital computer or specific
purpose digital hardware. For better clarification, we can briefly define analog, discrete,
and digital signals as follows:
• Digital Signal: a discrete signal with quantized (finite) amplitude values. For
example, there are 28 = 256 gray scale levels in an 8-bit digital image (Figure 1.1).
2
• Digital signals can convey information being less susceptible to noise, distortion, and
interference.
• Digital circuits can be reproduced easily in mass quantities at comparatively low costs.
• Digital signal processing is more flexible because DSP operations can be altered using
digitally programmable systems.
• Digital signal processing is more secure because digital information can be easily en-
crypted and compressed.
• Digital systems are more accurate, and the probability of error occurrence can be
reduced by employing error detection and correction codes.
• Digital signals can be easily stored on any magnetic media or optical media using
semiconductor chips.
Applications of DSP are increasing in many areas where analog electronics are being replaced
by DSP chips, and new applications are depending on DSP techniques. With the cost of DS
processors decreasing and their performance increasing, DSP will continue to affect engineer-
ing design in our modern daily life. Some application examples using DSP are listed in Table
1.1.
3
However, the list in the table by no means covers all DSP applications. Many more areas are
increasingly being explored by engineers and scientists. Applications of DSP techniques will
continue to have profound impacts and improve our lives.
4
• Basic signal generations.
• Linear Time Invariant (LTI) system response in time domain and convolution.
Frequency Transforms
A time-to-frequency transform operates on a block of time domain samples and evaluates
the frequency content thereof. A set of frequency coefficients is derived which can be used to
quantify the amplitudes (and usually phases) of frequency components of the original signal
or the coefficients can be used to reconstruct the original time, domain samples using an
inverse transform (a frequency-to-time transform). The most well-known and widely-used of
these transforms is the Discrete Fourier Transform (DFT), usually implemented by the
FFT (for Fast Fourier Transform), the name of a class of algorithms that allow efficient
computation of the DFT. Z-transforms also convert a time domain signal to complex
frequency domain.
5
• Week 3: Convolution and Discrete LTI System response.
• Week 4: Sampling.
Domain Transformation
• Week 6: Z-transform:
Filter Design
Answer the following questions and attach them with your lab report.
Questions:
1. Can you mention name of some physical signals along with their responding
human organ? Are they Analog or Digital?
2. Are the terms data and signal synonymous? Briefly clarify with example.
3. Do you think all digital data are always represented by 1 and 0 only? Briefly
Clarify.
6
Part 2
An Overview of Using MATLAB and Octave
• Demonstration of signal-processing example using MATLAB
• Recap of MATLAB/OCTAVE & Some warm up problems.
• Functions to be used: help(),plot(), sin(), cos(),sind(),cosd()
Instructions
• Organizing files and folder properly for later use.
• For every week make a subfolder to keep the all the tasks in a specific week
separated from other tasks.
• Always try to work in the folder specifically created for the current week.
About MATLAB
MATLAB is an environment for scientific computation especially suitable for computations
that require extensive use of arrays and graphical analysis. In this lab, all works will be done
using MATLAB. So a brief overview on how to use it arises naturally.
MATLAB works with three types of windows on computer screen. These are the Command
window, the Figure window and the Editor window. Any kind of commands are typed in the
command window, such as the command to run a function, indicated by the prompt (>>).
All the output and error will appear in this command window. The Figure window only
pops up whenever you plot something. The Editor window is used for writing and editing
MATLAB programs (called M files). Current Drive or Folder allows you to access your saved
files. Workspace explore data that you create or import from files. A figure is shown in figure
1.3 to show the desktop when you start MATLAB.
7
Figure. 1.3: MATLAB Window
About OCTAVE
OCTAVE is an open-source program that you can use as an alternative to MATLAB. Its
basic numerical functions are very similar to MATLAB, in terms of appearance and usage. In
addition, because the OCTAVE language is similar to MATLAB, most MATLAB programs
should be able to run on OCTAVE.
8
Figure. 1.4: OCTAVE Window
• Variable Editor — The workspace consists of the variables you create and store in
memory during a Octave session. You can create new variables in the workspace by
running Octave code or using existing variables. You can view and edit those variables
using Variable Editor.
• Command Window — Command Window is the main window where you type com-
mands directly to the Octave interpreter.
• Editor — The Editor window is a simple text editor where you can load, edit and
save complete Octave programs. The Editor window also has a menu command (De-
bug/Run) which allows you to submit the program to the command window.
• Documentation — The Documentation window gives you access to a great deal of useful
information about the Octave language and Octave computing environment. It also has
a number of example programs and tutorials.
Figure 1.5 shows the Editor window of the Octave.
However, functions in Octave are divided into different packages. These packages do not load
in Octave by default. You need to manually load them in the environment. To do this, you
need to write the following command in the command window.
pkg load package_name
Here, package_name is the name of the package that you will use. For DSP lab, we need the
signal package. That means, the package name will be signal. Therefore, you need to write
9
Figure. 1.5: OCTAVE Editor Window
the following command in the command window each time you open/start the Octave.
MATLAB/OCTAVE Basics
Variable Declaration
While declaring a variable in MATLAB/OCTAVE, the following things should be remem-
bered:
• Variable names must begin with a letter. Following that, any number of letters, digits
and underscores can be added.
10
Listing 1: Examples of Variable Declaration
1 clc; % use to clear all the previous lines in the command window
2 clear all; % use to clear all the previous variables
3 close all; % use to close all the figure windows used previously
4
5 x = 2; % Valid
6 a_b_c = 1; % Valid
7 X = 10; %This X and the first x are not the same
8 % MATLAB is case−sensitive
9 this_is_a_variable = 3; % Valid
10 3abc = 15; %This is not a valid variable name
11 % This will give an error
12 pi = 5; %This is also not a valid variable name as pi is the
13 % built in variable for MATLAB. This will not give any error
14 % but not recommended
Data Type
The default data type in MATLAB/OCTAVE is double precision array. While we’ll use this
type of data mostly in this lab, several other data types are also important. MATLAB/OC-
TAVE variables can be used in command prompt without pre-definition. As arrays will be
used mostly in our lab, we’ll discuss how to define and access arrays. Few rules of defining
and accessing arrays:
• Column elements are separated by a comma (,) and row elements are separated by
semicolon (;)
• When defined, array elements are contained between the [ and ] symbols.
• A sequential list of elements can be generated by the colon (:) operator using the
following form: initial value: increment : final value.
Few examples corresponding the above points are given below in a MATLAB/OCTAVE Code
snippet.
Listing 2: MATLAB/OCTAVE Codes for declaring and accessing Arrays
1 clear all;
2 clc;
3
11
4 a = [1, 3, 5]; % creating a 1x3 row array
5 b = [2; 4; 6]; % creating a 3x1 column array
6
7 A = [1, 3, 5; 2, 4, 6]; %creating a 2x3 matrix
8
9 c = A(1,2); % c is the element of 1st row and 2nd column of A
10 d = A(2,2); % d is the element of 2nd row and 2nd column of A
11
12 B = 2:2:10; % B is an array containing element from 2 to 10 with an increment
2
13 C = [2,4,6,8,10]; % B and C are the same array
14
15 D = [1+2*j, 2+1*j, 4−5*j] % creating a 1x3 complex row vector
Array Operations
The arithmatic operations +,-,* and / by default correspond to true matrix computation.
Few points regarding array operations.
• A*B is the usual matrix product between A and B. Here the dimension mismatch will
give an error.
• A.*B is the element by element manipulation. Any operator followed by dot(.) will
be element by element operation.
• Any function of a matrix will result in an output with the individual element as argu-
ment of the function. For example exp(A) will result in a matrix with element being
the exponential of each element of A.
12
Extracting Sub-matrix
A portion of a matrix can be extracted and stored in a smaller matrix by specifying the rows
and columns to extract. The syntax is:
where r1(c1) and r2(c2) denote the beginning and ending rows(columns). Suppose we have
a 4 × 5 matrix A given by the following matrix and we want to extract the portion marked
with lines.
1 3 5 7 9
0 2 4 6 8
9 7 5 3 1
8 10 6 4 5
13
Listing 5: Sample code from MATLAB/OCTAVE on plotting
1 clc;
2 clear all;
3 close all;
4
5
6 x = 1:0.1:10; % creating an array of elements from 1 to 10 with 0.1 increment
7
8 y = x.^2 + 20*x + 15*x.*sin(x); %computing a function of x
9
10 plot(x,y, 'Linewidth', 2); % plotting the figure
stem is used for displaying a discrete-time signal. The command format is the same as
plot. We’ll learn more about stem in the next lab. Whenever multiple plots are needed to
be shown in a figure, the function subplot is used. The basic syntax is subplot(m,n,p).
subplot(mnp) breaks the Figure window into an m-by-n matrix of small axes, selects the
pth axes object for for the current plot, and returns the axis handle. The axes are counted
along the top row of the Figure window, then the second row, etc. For example
subplot(2,1,1), plot(time, signal1)
subplot(2,1,2), plot(time, signal2)
The whole figure window is divided into a 2-by-1 matrix. In the upper portion of the window,
signal1 is plotted and signal2 is plotted in the lower portion.
An example code is given below:
Listing 6: Use of Subplot
1 clc;
2 clear all;
3 close all;
4
5 %In this problem we'll generate two signals
6 %and plot them in a single figure using subplot
7
8 t = −10:0.01:10; %defining time variable
9
10 signal1 = t.^2; % signal1 = t^2
11 signal2 = t.^3+4*t.^2+5; % signal2 = t^2+2*t+sqrt(t)
12
13 subplot(2,1,1)
14 plot(t, signal1, 'Linewidth', 2); % it will plot the signal in the top
portion ...
15 % of the window
16 subplot(212) %note that (2,1,2) or (212) both are correct
17 plot(t, signal2, 'Linewidth', 2);
14
For Loop
Like any other programming language, MATLAB also has functions for iteration structures.
We’ll see only the for function here. The syntax for writing for loop is:
Sample Problem 1: Compile and run the codes used for plotting and subplotting
different signals and draw the output waveforms.
Sample Problem 2: Generate a sinusoidal signal with 10 kHz and show it for 10ms.
Assume amplitude values and necessary data. Use sin() and plot() function first.
Then use cos(), sind(), cosd() functions to repeat the same. [Hint: use doc sin
command in command prompt]
Sample Problem 3: Generate a sinusoidal signal with 1 kHz with a initial value =
A
√ . A is the amplitude value = 10 here show its first 4 cycles. Use sin() and plot()
2
function first. Then use cos(), sind(), cosd() functions to repeat it.
Sample Problem 4: Generate the following waveshape given in figure 1.6 with proper
labeling.
15
Figure. 1.6: Waveform for Sample Problem 4
• Submit the codes along with the plots as a MS word file strictly by
the next week. Also work out the sample problems.
• Use a cover page containing the lab name and code along with student’s
detailed particulars.
16