Vous êtes sur la page 1sur 48

1

04\08\2010
DSP-Lab
DIGITAL SIGNAL PROCESSING LABORATORY
(Sub., Code : 06ECL57)

2
04\08\2010
DSP-Lab
LIST OF EXPERIMENTS USING MATLAB / SCILAB /
OCTAVE / WAB

1. Verification of Sampling theorem.
2. Impulse response of a given system
3. Linear convolution of two given sequences.
4. Circular convolution of two given sequences
5. Autocorrelation of a given sequence and verification of its
properties.
6. Cross correlation of given sequences and verification of its
properties.

3
04\08\2010
DSP-Lab
1. Solving a given difference equation.
2. Computation of N point DFT of a given sequence and to
plot magnitude and phase spectrum.
3. Linear convolution of two sequences using DFT and IDFT.
4. Circular convolution of two given sequences using DFT
and IDFT
5. Design and implementation of FIR filter to meet given
specifications.
6. Design and implementation of IIR filter to meet given
specifications. d order system

4
04\08\2010
DSP-Lab

Overview B. LIST OF EXPERIMENTS USING DSP PROCESSOR
1. Linear convolution of two given sequences.
2. Circular convolution of two given sequences.
3. Computation of N- Point DFT of a given sequence
4. Realization of an FIR filter (any type) to meet given specifications
The input can be a signal from function generator /speech signal.
5. Audio applications such as to plot time and frequency (Spectrum)
display of Microphone output plus a cosine using DSP. Read a
wav file and match with their respective spectrograms
6. Noise: Add noise above 3kHz and then remove; Interference
suppression using 400 Hz tone.
7. Impulse response of first order and second order system
REFERENCE BOOKS:
1. Digital signal processing using MATLAB - Sanjeet Mitra, TMH, 2001
2. Digital signal processing using MATLAB - J. G. Proakis & Ingale, MGH, 2000
3. Digital Signal Processors, B. Venkataramani and Bhaskar, TMH,2002

5
04\08\2010
DSP-Lab
MATLAB MATrix LABoratory
Initially developed by a lecturer in 1970s to help students
learn linear algebra.
It was later marketed and further developed under
MathWorks Inc. (founded in 1984) www.mathworks.com
Matlab is a software package which can be used to perform
analysis and solve mathematical and engineering problems.
It has excellent programming features and graphics capability
easy to learn and flexible.
Available in many operating systems Windows, Macintosh,
Unix, DOS
It has several tooboxes to solve specific problems.
Introduction
6
04\08\2010
DSP-Lab
Run MATLAB from Start Programs MATLAB
Depending on version used, several windows appear
For example in Release 13 (Ver 6), there are several
windows command history, command, workspace, etc
For Matlab Student only command window

Command window
Main window where commands are entered
Getting Started
7
04\08\2010
DSP-Lab
Example of MATLAB Release 13 desktop
8
04\08\2010
DSP-Lab
MATLAB Environment

9
04\08\2010
DSP-Lab
MATLAB Environment

10
04\08\2010
DSP-Lab
MATLAB Environment

11
04\08\2010
DSP-Lab
Display Windows
12
04\08\2010
DSP-Lab




Command Window

Workspace
Command
History
Change the current
directory to the
location of your
Matlab file at
startup
13
04\08\2010
DSP-Lab

Command Window
type commands

Workspace
view program variables
clear to clear
double click on a variable to see it in the Array Editor

Command History
view past commands
save a whole session using diary

Launch Pad
access tools, demos and documentation
14
04\08\2010
DSP-Lab
Introduction
Matlab prompts >> when it is ready to accept a command.
To end a matlab session type quit or exit at the matlab prompt.
Type help at the matlab prompt, to get help topics.
Variables
Variables in matlab are named objects that are assigned using the
equals sign = .
They are limited to 31 characters and can contain upper and
lowercase letters, any number of _ characters, and numerals.
They may not start with a numeral.
matlab is case sensitive: A and a are different variables.
who lists in alphabetical order all variables in the currently active
workspace.
clear removes all variables from the workspace. This frees up system
memory.
15
04\08\2010
DSP-Lab
Introduction
Colon Operator:
Colon (:)Create vectors, array subscripting, and for-loop
iterators
The colon is one of the most useful operators in MATLAB.
It can create vectors, subscript arrays, and specify for
iterations.
The colon operator uses the following rules to create regularly
spaced vectors:
j:k is the same as [j, j+1,...,k]
j:i:k is the same as [j, j+i, j+2i, ...,k]
5:10 -> 5, 6, 7,8,9,10
5:2:10 -> 5,7,9
16
04\08\2010
DSP-Lab
Operators (arithmetic)
+ addition
- subtraction
* multiplication
/ division
^ power
complex conjugate
transpose


.* element-by-element mult
./ element-by-element div
.^ element-by-element power
. transpose

17
04\08\2010
DSP-Lab
Operators (relational, logical)
== equal
~= not equal
< less than
<= less than or equal
> greater than
>= greater than or equal

& AND
| OR
~ NOT
1
pi 3.14159265
j imaginary unit,
i same as j

18
04\08\2010
DSP-Lab
Logical Operations
Mass = [-2 10 NaN 30 -11 Inf 31];
each_pos = Mass>=0
each_pos =
0 1 0 1 0 1 1
all_pos = all(Mass>=0)
all_pos =
0
all_pos = any(Mass>=0)
all_pos =
1
pos_fin = (Mass>=0)&(isfinite(Mass))
pos_fin =
0 1 0 1 0 0 1
= = equal to
> greater than
< less than
>= Greater or equal
<= less or equal
~ not
& and
| or
isfinite(), etc. . . .
all(), any()
find
Note:
1 = TRUE
0 = FALSE
19
04\08\2010
DSP-Lab
Variables
Vectors and Matrices
ALL variables are matrices
Variables
They are casesensitive i.e x = X
Their names can contain up to 31 characters
Must start with a letter

Variables are stored in workspace
e.g. 1 x 1 4 x 1 1 x 4 2 x 4
(

4 2 3 9
6 5 1 2
| | 7 1 2 3
(
(
(
(

3
9
2
3
| | 4
20
04\08\2010
DSP-Lab
How do we assign a value to a variable?
EDU v1=3
v1 =
3
EDU i1=4
i1 =
4
EDU R=v1/i1
R =
0.7500
EDU
EDU whos
Name Size Bytes Class
R 1x1 8 double array
i1 1x1 8 double array
v1 1x1 8 double array
Grand total is 3 elements using 24 bytes
EDU who
Your variables are:
R i1 v1
EDU
Vectors and Matrices
21
04\08\2010
DSP-Lab
(
(
(
(
(
(

=
18
16
14
12
10
B
How do we assign values to vectors?
EDU A = [1 2 3 4 5]
A =
1 2 3 4 5
EDU
EDU B = [10;12;14;16;18]
B =
10
12
14
16
18
EDU
A row vector
values are
separated by
spaces
A column
vector
values are
separated
by semi
colon (;)
| | 5 4 3 2 1 A =
Vectors and Matrices
22
04\08\2010
DSP-Lab
If we want to construct a vector of, say, 100
elements between 0 and 2t linspace
EDU c1 = linspace(0,(2*pi),100);
EDU whos
Name Size Bytes Class
c1 1x100 800 double array
Grand total is 100 elements using 800 bytes
EDU
How do we assign values to vectors?
Vectors and Matrices
23
04\08\2010
DSP-Lab
How do we assign values to vectors?
If we want to construct an array of, say, 100
elements between 0 and 2t colon notation
EDU c2 = (0:0.0201:2)*pi;
EDU whos
Name Size Bytes Class
c1 1x100 800 double array
c2 1x100 800 double array
Grand total is 200 elements using 1600 bytes
EDU
Vectors and Matrices
24
04\08\2010
DSP-Lab
How do we assign values to matrices ?
Columns separated by
space or a comma
Rows separated by
semi-colon
EDU A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
EDU
(
(
(

9 8 7
6 5 4
3 2 1
Vectors and Matrices
25
04\08\2010
DSP-Lab
How do we access elements in a matrix or a vector?
Try the followings:
EDU A(2,3)
ans =
6
EDU A(:,3)
ans =
3
6
9
EDU A(1,:)
ans =
1 2 3
EDU A(2,:)
ans =
4 5 6
Vectors and Matrices
26
04\08\2010
DSP-Lab
Some special variables

beep
pi (t)
inf (e.g. 1/0)
i, j ( )
1
EDU 1/0
Warning: Divide by zero.
ans =
Inf
EDU pi
ans =
3.1416
EDU i
ans =
0+ 1.0000i
Vectors and Matrices
27
04\08\2010
DSP-Lab
Arithmatic operations Matrices
Performing operations to every entry in a matrix
Add and subtract
EDU A=[1 2 3;4 5 6;7 8
9]
A =
1 2 3
4 5 6
7 8 9
EDU
EDU A+3
ans =
4 5 6
7 8 9
10 11 12
EDU A-2
ans =
-1 0 1
2 3 4
5 6 7
Vectors and Matrices
28
04\08\2010
DSP-Lab
Arithmatic operations Matrices
Performing operations to every entry in a matrix
Multiply and divide
EDU A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
EDU
EDU A*2
ans =
2 4 6
8 10 12
14 16 18
EDU A/3
ans =
0.3333 0.6667 1.0000
1.3333 1.6667 2.0000
2.3333 2.6667 3.0000
Vectors and Matrices
29
04\08\2010
DSP-Lab
Arithmatic operations Matrices
Performing operations to every entry in a matrix
Power
EDU A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
EDU
A^2 = A * A
To square every element in A, use
the elementwise operator .^
EDU A.^2
ans =
1 4 9
16 25 36
49 64 81
EDU A^2
ans =
30 36 42
66 81 96
102 126 150
Vectors and Matrices
30
04\08\2010
DSP-Lab
Arithmatic operations Matrices
Performing operations between matrices
EDU A=[1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9

EDU B=[1 1 1;2 2 2;3 3 3]
B =
1 1 1
2 2 2
3 3 3
A*B
(
(
(

(
(
(

3 3 3
2 2 2
1 1 1
9 8 7
6 5 4
3 2 1
A.*B
(
(
(

3 x 9 3 x 8 3 x 7
2 x 6 2 x 5 2 x 4
1 x 3 1 x 2 1 x 1
(
(
(

27 24 21
12 10 8
3 2 1
=
=
(
(
(

50 50 50
32 32 32
14 14 14
Vectors and Matrices
31
04\08\2010
DSP-Lab
Arithmatic operations Matrices
Performing operations between matrices
A/B
A./B
(
(
(

0000 . 3 6667 . 2 3333 . 2


0000 . 3 5000 . 2 0000 . 2
0000 . 3 0000 . 2 0000 . 1
=
?
(
(
(

3 / 9 3 / 8 3 / 7
2 / 6 2 / 5 2 / 4
1 / 3 1 / 2 1 / 1
Vectors and Matrices
32
04\08\2010
DSP-Lab
Flow Control Constructs
Logic Control:
IF / ELSEIF / ELSE
SWITCH / CASE / OTHERWISE

Iterative Loops:
FOR
WHILE
33
04\08\2010
DSP-Lab
The if, elseif and else statements
if I == J
A(I,J) = 2;
elseif abs(I-J) == 1
A(I,J) = -1;
else
A(I,J) = 0;
end
Works on Conditional
statements

Short-circuited in
MATLAB - once a
condition is true, the
sequence terminates.
34
04\08\2010
DSP-Lab
Switch, Case, and Otherwise
switch input_num
case -1
input_str = 'minus one';
case 0
input_str = 'zero';
case 1
input_str = 'plus one';
case {-10,10}
input_str = '+/- ten';
otherwise
input_str = 'other value';
end
More efficient than
elseif statements
Only the first
matching case is
executed
35
04\08\2010
DSP-Lab
The for loop
Similar to other
programming
languages

Repeats loop a set
number of times
(based on index)

Can be nested
N=10;
for I = 1:N
for J = 1:N
A(I,J) = 1/(I+J-1);
end
end
36
04\08\2010
DSP-Lab
The while loop
I=1; N=10;
while I<=N
J=1;
while J<=N
A(I,J)=1/(I+J-1);
J=J+1;
end
I=I+1;
end
Similar to other
programming
languages

Repeats loop until
logical condition
returns FALSE.

Can be nested.
37
04\08\2010
DSP-Lab
Matlab Graphics

x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
xlabel('x = 0:2\pi')
ylabel('Sine of x')
title('Plot of the
Sine Function')


38
04\08\2010
DSP-Lab

Sample Plot
Title
Ylabel
Xlabel
Grid
Legend
39
04\08\2010
DSP-Lab
t = 0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2);
plot(t,y1,t,y2)
grid on
Matlab Graphics
40
04\08\2010
DSP-Lab
Multiple Plots
t = 0:pi/100:2*pi;
y1=sin(t);
y2=sin(t+pi/2);
subplot(2,2,1)
plot(t,y1)
subplot(2,2,2)
plot(t,y2)
41
04\08\2010
DSP-Lab
Graph Functions (summary)
plot linear plot
stem discrete plot
grid add grid lines
xlabel add X-axis label
ylabel add Y-axis label
title add graph title
subplot divide figure window
figure create new figure window
pause wait for user response
42
04\08\2010
DSP-Lab
2-D Plotting
Syntax:


Example:
plot(x1, y1, 'clm1', x2, y2, 'clm2', ...)
x=[0:0.1:2*pi];
y=sin(x);
z=cos(x);
plot(x,y,x,z,'linewidth',2)
title('Sample Plot','fontsize',14);
xlabel('X values','fontsize',14);
ylabel('Y values','fontsize',14);
legend('Y data','Z data')
grid on
43
04\08\2010
DSP-Lab
2-D Plotting
x=[0:0.1:2*pi];
y=sin(x);
z=cos(x);
plot(x,y,x,z,'linewidth',2)
title('Sample Plot','fontsize',14);
xlabel('X values','fontsize',14);
ylabel('Y values','fontsize',14);
legend('Y data','Z data')
grid on
44
04\08\2010
DSP-Lab
Subplots
Syntax:
subplot(2,2,1);


subplot(2,2,2)
...

subplot(2,2,3)
...

subplot(2,2,4)
...
subplot(rows,cols,index)
45
04\08\2010
DSP-Lab
Subplots
Syntax:
subplot(2,2,1);


subplot(2,2,2)
...

subplot(2,2,3)
...

subplot(2,2,4)
...
subplot(rows,cols,index)
46
04\08\2010
DSP-Lab
M-Files
The M-file is a text file that consists a group of
MATLAB commands.
MATLAB can open and execute the commands
exactly as if they were entered at the MATLAB
command window.
To run the M-files, just type the file name in the
command window. (make sure the current working
directory is set correctly)
All MATLAB commands are M-files.
So far, we have executed the commands in the command window.
But a more practical way is to create a M-file.
47
04\08\2010
DSP-Lab
To begin a new M-file
48
04\08\2010
DSP-Lab
How the source-code looks like

Vous aimerez peut-être aussi