You are on page 1of 21

Depart ment of El ect ri cal Power Engi neeri ng

L a b m a n u a l o n
D i g i t a l S i g n a l P r o c e s s i n g





Submitted to:
En g r . M . A b b a s A b b a s i
Su b m i t t e d b y :
0 8 EL - 6 0
Sh a f q a t H u s s a i n Sa b a a q
Se m e st e r :
6
t h
, 2 0 0 8 - 2 0 1 2


Uni versi ty College Of Engi neeri ng And Technology
Isl ami a Uni ver si t y Of Bahawal pur , Paki st an.
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
2

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

S.No Lab Experiments Page No
1
To become familiar with the different types of discrete signal
and plot them in the matlab.

3
2
Generating Tunes with Music Notes in MATLAB 11
3
Working with Audio Signals in MATLAB

13
4 Introduction to SimPowerSystem 16
5
To See the Frequency Response of a Signal in MATLAB

18
6
To Implement a Discrete System in Simulink.

20


C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
3

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

Lab No 1
Objective:
To become familiar with the different types of discrete signal and plot them in the matlab.
Apparatus:
MATLAB software.
Theoretical background:
Discrete signals are those signals which have discrete values at particular
interval of time. We study some of the basic discrete time signals.
(1) Unit sample signal:
The unit step signal can be expressed as
x(n) = (n)
And(n) = 1 for n = 0
(n) = 0 for all n 0



-3 -2 -1 0 1 2 3
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
(n)
(
x
n
)
(FIG: UNIT IMPULSE SIGNAL)
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
4

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

(2) Unit step signal:
The unit step signal can be expressed as
x(n) = u(n)
And u(n) = 1 for n 0
u(n) =0 for n < 0



(3) Ramp signal:
Ramp signal can be expressed as
r(n) = n for n 0
r(n) = 0 for n < 0
-10 -8 -6 -4 -2 0 2 4 6 8 10
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
(n)
(
x
n
)
(FIG: UNIT STEP SIGNAL)
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
5

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing


(4) Sinusoidal signal:
The sinusoidal signal can be expressed as
x(n) = A cos (2fn)


-10 -8 -6 -4 -2 0 2 4 6 8 10
0
1
2
3
4
5
6
7
8
9
10
(n)
(
x
n
)
(FIG: RAMP SIGNAL)
-10 -8 -6 -4 -2 0 2 4 6 8 10
-10
-8
-6
-4
-2
0
2
4
6
8
10
(n)
(
x
n
)
(FIG: SINUSOIDAL SIGNAL)
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
6

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

(5) Exponential signal:
The exponential signal can be expressed as
x(n) = A e
an

(6) Complex exponential signal:
The complex exponential signal can be expressed as
x(n) = A e
j2fn

-10 -8 -6 -4 -2 0 2 4 6 8 10
0
10
20
30
40
50
60
70
80
(n)
(
x
n
)
(FIG: EXPONENTIAL SIGNAL)
-10 -8 -6 -4 -2 0 2 4 6 8 10
-10
-5
0
5
10
(n)
x
r
FIG: COMPLEXEXPONENTIAL SIGNAL(REAL PART)
-10 -8 -6 -4 -2 0 2 4 6 8 10
-10
-5
0
5
10
(n)
(
x
i
)
FIG: COMPLEXEXPONENTIAL SIGNAL(IMAGINARY PART)
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
7

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

(7) Square wave signal:
This signal obtained on the graph which shows that it has the wave like a square
It can be expressed as
x(n) = square (2fn)


(8) Sawtooth wave signal:
On the graph its behavior like a sawtooth. It is very common type of wave signal.
It can be expressed as

x(n) = sawtooth (2fn)
-10 -8 -6 -4 -2 0 2 4 6 8 10
-8
-6
-4
-2
0
2
4
6
8
(n)
(
x
n
)
(FIG: SQUARE WAVE SIGNAL)
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
8

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing


(9) Sinc signal:
It is the signal which can be expressed mathematically as
x(n) = sinc (2fn)

-20 -15 -10 -5 0 5 10 15 20
-20
-15
-10
-5
0
5
10
15
20
(n)
(
x
n
)
(FIG: SAWTOOTH WAVE SIGNAL)
-10 -8 -6 -4 -2 0 2 4 6 8 10
-2
-1
0
1
2
3
4
5
6
7
8
(n)
(
x
n
)
(FIG: SINC WAVE SIGNAL)
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
9

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

Cell structure matlab code for all signals listed above:
%%
%(1)unit impulse signal
n=-3:3;
x_n=[0 0 0 1 0 0 0];
stem(n,x_n);xlabel('(n)');ylabel('(x_n)');grid on
title('(FIG: UNIT IMPULSE SIGNAL)');
%%
%(2)unit step signal
n=-10:10;
x_n=zeros(1,length(n));
x_n(find(n>=0))=1;
stem(n,x_n);xlabel('(n)');ylabel('(x_n)');
title('(FIG: UNIT STEP SIGNAL)');grid on
%%
%(3)ramp signal
n=-10:10;
x_n=n; x_n(find(n<=0))=0;
stem(n,x_n);xlabel('(n)');ylabel('(x_n)');
title('(FIG: RAMP SIGNAL)');grid on
%%
%(4)sinusoidal signal
A=10;
f=0.1;
n=-10:10;
x_n=A*cos(2*pi*f.*n);
stem(n,x_n);xlabel('(n)');ylabel('(x_n)');
title('(FIG: SINUSOIDAL SIGNAL)');grid on
%%
%(5)exponential signal
A=10;
a=-0.2;
n=-10:10;
x_n=A*exp(a.*n);
stem(n,x_n);xlabel('(n)');ylabel('(x_n)');
title('(FIG: SINUSOIDAL SIGNAL)');grid on
%%
%(6)complex explonetial signal
n=-10:10;
A=8;
f=0.1;
x_n=A*(cos(2*pi*f.*n)+j*sin(2*pi*f.*n));
x_r=real(x_n);
x_i=imag(x_n);
subplot(211);stem(n,x_r);xlabel('(n)');ylabel('x_r');
title('FIG: COMPLEX EXPONENTIAL SIGNAL(REAL PART)');grid on
subplot(212);stem(n,x_i);xlabel('(n)');ylabel('(x_i)');
title('FIG: COMPLEX EXPONENTIAL SIGNAL(IMAGINARY PART)');grid on
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
10

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

%%
%(7) square wave signal
n=-10:10;
A=8; f=0.1;
x_n=A*square(2*pi*f*n);
stem(n,x_n);xlabel('(n)');ylabel('(x_n)');
title('(FIG: SQUARE WAVE SIGNAL)');grid on
%%
%(8) sawtooth wave signal
n=-20:20;
A=20; f=0.1;
x_n=A*sawtooth(2*pi*f*n);
stem(n,x_n);xlabel('(n)');ylabel('(x_n)');
title('(FIG: SAWTOOTH WAVE SIGNAL)');grid on
%%
%(9) sinc wave signal
n=-10:10;
A=8; f=0.1;
x_n=A*sinc(2*pi*f*n);
stem(n,x_n);xlabel('(n)');ylabel('(x_n)');
title('(FIG: SINC WAVE SIGNAL)');grid on















C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
11

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

Lab No 2
Objective:
Generating Tunes with Music Notes in MATLAB
Apparatus:
MATLAB software.
Theoretical background:
Introduction:
The music tunes are composed of several sinusoidal and exponential signals. Each music
note is defined as a function of time. So by the combination of different music notes, a tune can be
generated. This lab session is to generate the music tune and to work with several discrete signals by
getting familiarity with several parameters.
Upsampling and Downsampling:
The process of increasing the sampling frequency of the sampler is called upsampling while the process
of decreasing the sampling frequency is called downsampling.
Downsampling is done in MATLAB by the following code:
tdown=downsample(t,N)
here t is the sampling time that is reciprocal of sampling frequency and N is the number by which the
downsampling is required. This command reduces the sampling frequency by a factor of N. tdown is the
new sampling time.
Similarly the command for upsampling is:
tup=upsampling(t,N)
Matlab code for sampling:
%sampling
A=5;
F=300;
F_s=8000;
T_s=1/F_s;
t=0:T_s:0.01;
x_t=A*sin(2*pi*F.*t);
tup=upsample(t,4);
x_tup=upsample(x_t,4);
stem(tup,x_tup)




C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
12

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

Output results:

Fi g: Sa m p l i n g











0 0.001 0.002 0.003 0.004 0.005 0.006 0.007 0.008 0.009 0.01
-5
-4
-3
-2
-1
0
1
2
3
4
5
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
13

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

Lab No 3
Objective:
Working with Audio Signals in MATLAB

Apparatus:
MATLAB software.
Theoretical background:
Section A
There are four ways to transmit or receive data.
1) Text Keypad
2) Audio Microphone
3) Image Camera
4) Video Web Cam/VID Cam
Sound command is used to listen a defined function in MATLAB. This function will be played at the
frequency of 8 kHz, which is the default frequency of sound card. In addition, the transmission system is
also transmitting signals on this frequency.
The Maximum frequency that can be achieved from sound card is 48 kHz. To hear the maximum sound
of sound card, the command soundsc is used.
Lab Work:

Consider the following program

t=0:0.01:1;
y1=5*sin(40*pi.*t);
y2=10*square(40*pi.*t);
y3=15*sawtooth(40*pi.*t);

Q1. Listen the following signals at specified frequency
i) y1
ii) y2
iii) y3
iv) y1+y2+y3
v) y1y2y3
vi) y1y2+y3
vii) y1-y2-y3
viii) y1-y2y3
sound(signal name) command is used to listen these signals.



C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
14

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

Q2. Listen y2 at 8 kHz, 4 kHz, 2 kHz and 5 Hz
sound(y2, 8000) is used to listen y2 at 8 kHz. Similarly by changing the frequency, y2 can be heard at any
frequency.
Q3. Listen y3 at 9 kHz
sound(y3, 9000) is used here.

All signals given in above questions have been successfully listened during the lab session.
Matlab code:
t=0:0.01:1;
y1=5*sin(40*pi.*t);
y2=10*square(40*pi.*t);
y3=15*sawtooth(40*pi.*t);
play(y1,8000)
play(y2,8000)
play(y2,4000)
play(y2,3000)
play(y2,2000)
play(y3,9000)
play(y1+y2+y3)
play(y1y2y3)
play(y1y2+y3)
play(y1+y2y3)
play(y1-y2-y3)
Section B
Recording:
Recording in MATLAB can be static or dynamic.
The difference in two types is that the static recording cant be plotted and also it is difficult to save it in
memory. To view the recording graphically and to save it, we have to convert the recording into Dynamic
Type.
How to record a static file?
Static recording can be accomplished using audiorecorder and record command. In order to record a
signal y for 10 seconds, following MATLAB code is written:
y=audiorecorder;
record(y,10)

To play this signal, use play command as shown below:
play(y)
To pause the playing signal, use pause command as shown below:
pause(y)
To stop the signal, use stop command as shown below:
stop(y)
How to covert Static Recording into Dynamic Recording?
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
15

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

Static recording can be converted into dynamic recording by using the getaudiodata command as shown
below:
x= getaudiodata(y);
Now it is a dynamic file and cant be played with a play command. To listen this, use wavplay
command as shown below:
wavplay(x)
How to save a wav file?
wav file x can be saved by using the command wavwrite as shown below:
wavwrite(x, desired frequency, Name of the file)
This file will be saved in work folder of MATLAB.
How to play a wav file from local disk?
If a wav file is located in the local disk of computer, then it can be listened in MATLAB using the
command wavread. Suppose a wav file format of 3idiots ringtone is located in tones folder in local disk
D, then following code will be used to play this file in MATLAB:
wavread(d:\tones\3idiots.wav)

Matlab code for recording:
y=audiorecorder;
record(y)
play(y,70)
pause(y)
stop(y)
yd=getaudiodata(y);
wavplay(yd)
wavplay(yd,8000)
wavwrite(yd,8000,'Sabaaq.wav')












C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
16

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

Lab No 4
Objective:
Introduction to SimPowerSystem

Apparatus:
MATLAB software.
Theoretical background:
Introduction:
SimPowerSystem is a library in simulink and is the core tool for Power Engineers for
implementing and simulating the Power Devices and Projects. This library covers almost all of the
practical modeling in simulation. This lab session is to learn techniques of simulink modeling of physical
and power devices.
Lab Work:
Implement the following model in simpower and observe the output

Fig: Simpower model



powergui
Continuous
VoltageMeasurement
v
+
-
Universal Bridge
A
B
C
+
-
Scope
Sadasource
N
A
B
C
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
17

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

Output Results:

Fig: Output Results for simpower model











C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
18

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

Lab No 5
Objective:
To See the Frequency Response of a Signal in MATLAB

Apparatus:
MATLAB software.
Theoretical background:
Introduction:
Discrete-Time Fourier Transform (DTFT) is used to see the behavior of a signal with
respect to Frequency that is often called as Frequency Response of a Signal.
Discrete-Time Fourier Transform is denoted as X(j).
Mathematically it is written as
X(]) = x(n)c
-]on
N-1
n=0

Lab Work:
x(n) = o
n
u(n)
Then
|X(])|^2 = 1/ (1 - 2ocos + o^2 )

Sketch X(j) for
i) a=1/4
ii) a=-1/4
iii) a=1/2
iv) a=-1/2

a1=1/4;
a2=-1/4;
a3=1/2;
a4=-1/2;
w=-pi:0.001:pi;
E1=1./(1-2*a1*cos(w)+a1^2);
E2=1./(1-2*a2*cos(w)+a2^2);
E3=1./(1-2*a3*cos(w)+a3^2);
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
19

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

E4=1./(1-2*a4*cos(w)+a4^2);
plot(w,E1,w,E2,w,E3,w,E4);
grid on
legend('a=1/4','a=-1/4','a=1/2','a=-1/2')
Output Results:





Fig: Frequency Response of a signal









-4 -3 -2 -1 0 1 2 3 4
0
0.5
1
1.5
2
2.5
3
3.5
4


a=1/4
a=-1/4
a=1/2
a=-1/2
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
20

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

Lab No 6
Objective:
To Implement a Discrete System in Simulink.

Apparatus:
MATLAB software.
Theoretical background:
Introduction:
This lab session is to implement a discrete system in Simulink and to be familiar with
different simulink tools.
The general equation of a system is given by
y(n) = oy(n - 1) + bx(n)
Lab Work:
Let o = 1 - RI/ I
b = I/ I

And let
R = 10u
I = 1 mE
Is = 0.5s

Now we will implement this model in simulink as shown below




Fig: Model A
Unit Delay
z
1
SineWave
Scope
Gain 1
a
Gain
b
First-Order
Hold1
First-Order
Hold
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
21

08EL-60 Shafqat Hussain Sabaaq Digital Signal Processing

Output Results:


Fig: Results for model A




C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m
C
lic
k
t
o
b
u
y
N
O
W
!
P
D
F-XChan
g
e
w
w
w
.
t
r
a
c
k
er-soft w
a
r
e
.
c
o
m

You might also like