LAB 1: Overview of DSP LAB (EEE 3218) Objectives

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Department of Electrical & Electronic Engineering

Ahsanullah University of Science and Technology (AUST)

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:

• Introduction to basic terminologies

• Motivation for the DSP Lab

• Overview of the DSP Lab

PRELAB TASKS

• Read this laboratory tutorial carefully before coming to the laboratory class, so
that you know what is required.

• Follow the lecture notes of EEE 3217.

• 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:

• Analog Signal: a function x(t), continuous in amplitude, of a continuous indepen-


dent variable t (e.g., time).

• Discrete Signal: a function x[n] = x(nt0 ), continuous in amplitude, but defined


only at a set of discrete values of the independent variable, t = 0, ±t0 , ±2t0 , ...

• 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).

Figure. 1.1: A digital image of a DSLR Camera

Advantages & Applications of Digital Signal Processing


Advantages of using digital signals, including digital signal processing (DSP) and communi-
cation systems, include the following:

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.

DSP Nomenclatures & Topics


Figure 1.2 shows a broad overview of digital signal processing. Analog signals enter an ADC
from the left, and samples exit the ADC from the right, and may be 1) processed strictly
in the discrete time domain (in which samples represent the original signal at instants in
time) or they may be 2) converted to a frequency domain representation (in which samples
represent amplitudes of particular frequency components of the original signal) by a time-to-
frequency transform, processed in the frequency domain, then converted back to the discrete
time domain by a frequency-to-time transform. Discrete time domain samples are converted
back to the continuous time domain by the DAC. Note that a particular signal processing
system might use only time domain processing, only frequency domain processing, or both
time and frequency domain processing, so either or both of the signal processing paths shown
in Fig. 1.2 may be taken in any given system.

Figure. 1.2: A broad, conceptual overview of digital signal processing.

Time Domain Processing


In time domain processing the variation of the input and output variables are observed with
respect to time. The examples of time domain processing includes

4
• Basic signal generations.

• Signal manipulation in time domain.

• Signal composition and decompositions.

• Linear Time Invariant (LTI) system response in time domain and convolution.

• Auto-correlation and Cross-correlation etc.

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.

Frequency Domain Processing


Most signal processing that can be done in the time domain can be also equivalently done
in the frequency domain. Each domain has certain advantages for a given type of problem.
Time domain filtering, for example, can be performed using frequency transforms such as
the DFT, and in certain cases, efficiency can be greatly improved using this technique. A
second use is in digital filter design, in which the desired filter frequency response is specified
in the frequency domain, i.e., as a set of DFT coefficients, for example. Yet a third and very
prevalent use is Transform Coding, in which signals are coded using a frequency transform
(usually eliminating as much redundant information as possible) and then reconstructed from
the transform coefficients. Transform Coding is a powerful tool for compression algorithms,
such as those employed with MP3 (MPEG II, Level 3) for audio signals, JPEG, a common
image compression format, etc. The use of such compression algorithms has revolutionized
the audio and video fields, making storage of audio and video data very economical and
deliverable via Internet.

Probable List of Lab Tasks


1 Week 1: introduction to DSP

Time Domain Processing

• Week 2: Discrete Time Signals and Linear System.

5
• Week 3: Convolution and Discrete LTI System response.

• Week 4: Sampling.

Domain Transformation

• Week 5: Frequency Transformation:

• Week 6: Z-transform:

Filter Design

• Week 7: IIR Filter Design

• Week 8: FIR Filter Design

• Week 9: Application of all the process learnt to a real life signal.

Assessment Procedure and Marks Distribution (Tentative)


Method Percentage
a. Attendance 10
b. Lab Reports & Performance 30
C. Lab Examination 1 10
d. Surprised Test (Lab Exam 2) 10
e. Lab Examination 3 10
f. Lab Final 30
Total 100

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.

4. When speed of processing is the only factor to be considered, whether Analog or


Digital Processing should be used? Why?

5. Which processing requires less power? Analog or Digital? Why?

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.

• Make a unique named folder for this lab like EEE3218_SP20_A1_180105001


under any drive other than C drive.

• 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.

• Follow MATLAB/OCTAVE naming convention for giving a meaningful name


before saving it in MATLAB/OCTAVE.

• While running a code be judicious in choosing add to path or change directory.


Use add to path when you are using a file from different folder.

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.

You can download the OCTAVE from the following link.


https://www.gnu.org/software/octave/download
When you start OCTAVE, the desktop appears in its default layout as the following figure
1.4.
• Current Directory — Create or access your files.
• File Browser — You can browse your files in any directory throughout your computer.
• Workspace — The workspace contains variables that you create or import into Octave
from data files or other programs. You can view and edit the contents of the workspace
in the Workspace browser or in the Command Window.
• Command History — The Command History window displays a log of statements that
you ran in the current and previous Octave sessions. The Command History lists the
time and date of each session in the short date format for your operating system,
followed by the statements from that session.

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.

pkg load signal

MATLAB/OCTAVE Basics
Variable Declaration
While declaring a variable in MATLAB/OCTAVE, the following things should be remem-
bered:

• Variable Names are case-sensitive i.e. A and a are different variables.

• A variable name in MATLAB/OCTAVE can contain upto 63 characters.

• Variable names must begin with a letter. Following that, any number of letters, digits
and underscores can be added.

Following code snippet shows different ways of declaring variables in MATLAB/OCTAVE.


The fifth variable declaration will give an error upon compiling because it started with a num-
ber instead of a letter. And also any name that resembles with built in MATLAB/OCTAVE
variable like pi is not recommended.

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:

• Array indices begin with 1.

• Column elements are separated by a comma (,) and row elements are separated by
semicolon (;)

• When defined, array elements are contained between the [ and ] symbols.

• The element in i row and j column of matrix A(Aij ) is accessed by A(i,j).

• A sequential list of elements can be generated by the colon (:) operator using the
following form: initial value: increment : final value.

• Complex entries are used using i and j.

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.

Listing 3: Sample code from MATLAB/OCTAVE on array operations


1 clc;
2 clear all;
3
4 A = [1, 2, 3]; % a row array of 3 elements
5 B = [2, 4, 6]; % a row array of 3 elements
6 C = [1; 2; 3]; % a column array of 3 elements
7
8 D = A+B ; % D is the sum of the two arrays.
9 E = A−B; % E is the subtraction of the two arrays
10 F = C*A; % F is the matrix product of A and C
11 G = A.*B; % G is the element wise product of A and B
12 H = exp(A)+sin(B); % This computes two functions of A and B. The size of any
function of any array is that of the array.

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:

sub_matrix = matrix(r1:r2, c1:c2)

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

Listing 4: Extracting Submatrix


1 clc;
2 clear all;
3 close all;
4
5 A = [1 3 5 7 9; 0 2 4 6 8; 9 7 5 3 1; 8 10 6 4 5]; % defining the matrix
6
7 % We want to extract the submatrix [9 7 5; 8 10 6] which consists of
8 % elements of 3rd and 4th row and 1st, 2nd and 3rd column of A
9 sub_mat = A(3:4, 1:3);
10
11 Output:
12
13 sub_mat =
14
15 9 7 5
16 8 10 6

Plotting with MATLAB/OCTAVE


MATLAB/OCTAVE has a lot of functions for plotting. The basic one will plot one vector
vs. another. Command plot(a,b) will plot the b vector vs. a vector treating the elements
of a vector as abscissa (or x axis) and the second as the ordinate (or y axis). The vectors
have to be the same length. If the second vector is not given, MATLAB/OCTAVE will plot
the vector vs. its own index. There are many functions that can be used to format the
figures. One can edit the title, the plotting style, color and so on. You can know about any
MATLAB/OCTAVE functions by typing help function-name in the command window.

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:

1: for <variable = expression> do


2: sentence;
3: sentence;
4: ... ...
5: ... ...
6: sentence;
7: end

An example code is given here:


Listing 7: Sample code on for loop
1 clc;
2 clear all;
3
4 % This code computes the sum of the elements of a given array
5 % using for loop
6 a = [1, 2, 3, 4, 5, 6]; % given array
7 n = length(a); % length gives the length of the vector
8 sum = 0; % initializing the sum
9 for i = 1:n %remember in matlab indices start from 1
10 sum = sum + a(i); %computes the sum
11 end % every for loop has an end!

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

Post Lab Tasks


• Go through the Overview of MATLAB/OCTAVE Part thoroughly.

• 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

You might also like