B PDF

You might also like

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

PRACTICALWORKBOOK

For Academic Session Fall 2018

Digital Signal Processing (EE-394)


For
TE Electrical

Name: DANIYAL
Roll Number: EE-16045 Year: 2018
Section: B

Batch: 2016 – 17

Department of Electrical Engineering


N.E.D. University of Engineering & Technology, Karachi

1
Digital Signal Processing Contents
NED University of Engineering and Technology Department of Electrical Engineering

CONTENTS

Lab. Page
List of Experiments R e mar ks
No. No.

1 Introduction to Digital Signal Processing 4

2 Analog to Digital Conversion 5

3 9
Analog to Digital Conversion

4 11
Open Ended Lab 1

5 Discrete time modeling of continuous time system 15

6 Convolution for Discrete Time Signals 20

7 CTFS Calculation of a CTCV Signal 25

Analysis and Synthesis of Signals through Discrete Fourier


8 28
Transform (DFT)

To become familiar with practical constraints in calculating


9 32
Fourier Transform of any real world signal

10 Open Ended Lab 2 36

11 To understand the concept of window overlapping 40

2
Digital Signal Processing Laboratory Rubrics
Department of Electrical Engineering NED University of Engineering and Technology

Lab Rubrics:
1. Properly formatted lab document (report writing skill)
2. Understanding of the concepts delivered.

S. No. Rubric No. Grading Score

1 1 Exemplary (100%): Adequate (70%): Poor (40%):


Lab report was well written and Lab report was written with Poorly written lab content.
properly formatted with good delivery some needed improvements
of the required concepts.

2 2 Exemplary (100%): Adequate (70%): Poor (40%):


Clearly describes the objectives of the Describes the objective and Inaccurate understanding of the
lab as well as the concepts learned. concepts related to lab with concepts related to lab session.
some deficiency.

3
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Laboratory Session No. 01

Objective:
To get introduced with fundamentals of Digital Signal Processing
Post Lab Exercises:
Question 1:
What do you mean by the term digital. Explain it briefly.
Answer:
Digital describes technology that generates, stores, and processes data in terms of two states:
positive and non-positive. Positive is expressed or represented by the number 1 and non-positive
by the number 0.
Question 2:
Write some (at least three) applications of DSP related to electrical (power) engineering.
Answer:
1. A DSP based electric power meter (Research paper showed on 13 th Nov 1992).
2. DSP-based three phase power quality measurement system.
3. DSP Based Digital Relay of Power Transformer.

4
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Laboratory Session No. 02

Objective:
An Introduction to Analog to Digital Conversion (Sampling and Aliasing).
Post Lab Exercises:
Question 1:
What do you mean by the term “Sampling”? Discuss it briefly with the help of figure.
Answer:
Sampling is a process used in statistical analysis in which a predetermined number of
observations are taken from a larger population.
In signals sampling is conversion of continuous time signal to discrete time signal.

Question 2:
What is Sampling theorem? What do you mean by the term Aliasing?
Answer: It’s the(Nyquist sampling) theorem used to go from continuous to discrete time signal
While avoiding aliasing.
Aliasing is an effect that causes different signals to become indistinguishable when
. Sampled.
Question 3:
Human audible frequency ranges from ____20____ to ____20k_____Hz. Human voice
frequency ranges from____100___ to __8000______ Hz.
5
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Question 4:
Record audio for 10sec and complete the following table. Show and verify the output file size
through mathematical calculations.
(Hint: Check the Microphone ADC bits and use sampling frequency and the audio record time to
evaluate the file size)

Formula used:

Size 1 = 10*1000*4 = 40,000 Bytes.


Size 2 = 10*2000*4 = 80,000 Bytes.
Size 3 = 10*4000*4 = 160,000 Bytes.
Size 4 = 10*6000*4 = 240,000 Bytes.
Size 5 = 10*10000*4 = 400,000 Bytes.
Size 6 = 10*22050*4 = 882,000 Bytes.
Size 7 = 10*44100*4 = 1,764,000 Bytes.

6
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

S No Sampling File Size Quality


Frequency(Hz) (MB) Comment
1 44100 1,725 Best quality sound.
2 22050 863 No noticeable difference
3 10000 392 A few sounds seem to be dulled
4 6000 235 High pitch(frequency) instruments can’t be heard
5 4000 157 Sound quality is degrading
6 2000 79 Further degradation.
7 1000 40 Worst quality sound.

Answer:
The sound quality decreases as the sampling frequency decreases and higher pitch
sounds start vanishing.

Question 5:
If an ADC has sampling frequency =1000 Hz and receive analog signals of the following
frequencies what will be the frequency of a signal which is converted back to analog by a DAC
converter?
S No Frequency
(Hz)
1 100
2 750
3 1250
4 1900
5 2000
6 2500

7
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Answer:
S No Frequency
(Hz)
Reconverted frequencies calculated using: 1 100
2 250
3 250
4 100
The fundamental range is B/W -0.5 to 0.5 5 0
6 500
Any frequency lying beyond this range can be calculated
. by adding or subtracting an appropriate integer,.

8
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Laboratory Session No. 03

Objective:
An Introduction to Analog to Digital Conversion (Quantization and Coding).
Post Lab Exercises:
Question 1:
Why Quantization is needed in Digital Signal Processing?
Answer: As we have limited memory and cannot store every possible value the signal has.
Question 2:
What is Anti-Aliasing Filter? Discuss it with some example.
Answer: A low pass filter is an anti-aliasing filter.Consider a ADC which receives a signal
having max frequency of 500hz but the max sample frequency that can be set is 400hz meaning
max of 200hz signal can be sampled according to theorem, so a low pass filter that allows
frequencies till 400hz is used blocking 500hz signal and avoiding aliasing.
Question 3:
Discuss the specifications of Arduino Uno ADC like number of bits, number of quantization
levels, etc. Also, calculate the default Arduino Uno ADC resolution.
Link: https://store.arduino.cc/usa/arduino-uno-rev3
Answer: Bits = 10, Levels of quantization = 1024, Bit rate = 16M * 1024 = 16384M, Clock
Frequency = 16 Mhz. Xmax = 5, X min = 0.
Resolution = (5 - 0) / ((2 ^ 10) - 1) = 0.00488.
Question 4:
A 12-bit ADC has input values in the range of 0 – 1 V. Calculate the resolution of ADC.
Answer:

9
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Laboratory Session No. 04


(Open Ended Lab 01)

Objective:
To convert an analog (voltage) signal into digital signal using ADC (audio card) and display it on
MATLAB Simulink environment.
Required Components:
1. Audio Card
2. Transformer (220V/12V)
3. Resistors (for VDR)
4. Veroboard
5. Audio jack
6. PC with MATLAB environment
Procedure:
 Using Transformer convert 220VAC from mains into 12VAC.
 Using VDR convert 12VAC to a voltage compatible to audio card (show all the
calculations of resistances with their power ratings).
 Set the sampling frequency of the audio card ADC in MATLAB Simulink environment
with proper justification
 Plot the acquired voltage waveform to Simulink scope.
 Mention the safe operating range of your equipment.
Calculations:
A transformer 220V/ (15 + 15)V center tapped transformer was used. Through use of a function
generator, clipping of sound card occurred at 0.120Vpp. The cards gain was determined to be
approximately 16 .

10
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

S.No Gain
0 15.58
1 16.26
2 16.13
3 15.63

The process of gain determination was repeated with a VDR circuit where 100kΩ potentiometers
along with a 100Ω resistor and at different values for the potentiometer, the following readings
were taken:
Average Gain Value from VDR Circuit = 16.50
So the cards gain can approx. be taken as 16.

S.No Gain Potentiometer Value(kΩ) Vin(V)


0 16.2946 40.9 15.2
1 16.2893 50.5 15.3
2 16.7089 61.0 15.6
3 17.5156 71.9 15.5
4 16.4886 80.9 15.5
5 15.9122 95.8 15.3

The potentiometer was at 41.0 kΩ.


To balance the effect of gain and voltage drops we need to add a gain block to get back our 220
volts for which we used 80 gain block.

11
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Output:

12
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Laboratory Session No. 05

Objective:
To model a source free and sourced RL circuit in a discrete time domain and to verify it using
MATLAB code and Simulink environment.

Theory:
Consider a series RL circuit excited by a voltage source as shown below,

Applying KVL we get,

(1)

For a source a source free RL circuit the above expression will become,

(2)

By using first principle, the derivate of inductor current in a discrete time domain can be
evaluated as,

(3)
or,

(4)

13
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

From eq. (2), the derivative at the instant can be evaluated as,

(5)

Equating eq. (4) and (5) we get,

(6)

By choosing sampling T close to zero we can evaluate the difference equation as,

( ) (7)

Where, and are replaced by and respectively.

Post Lab Exercise:


Question 1:
For a series RL circuit excited by a voltage source , derive an expression for inductor current
, in discrete time domain. Using the derived expression, develop a MATLAB code to plot
the inductor current under following system parameters:

Answer: The new Equation will be:

( )

14
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

( )

Code

15
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Simulink Circuit:

16
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Laboratory Session No. 06

Objective:
Convolution for Discrete Time Signals
Post Lab Exercises:
Question 1:
We have an impulse response of a system h(n)= {3,2,1, 2,1,0, 4,0,3}, provided with an input
x(n)={1 2 3 5 4 6}, find and plot y(n). Also check and comment on the causality property of the
system.
Answer: Causal system as h(t) at t<0 = 0.
Code:
>> h = [3 2 1 2 1 0 4 0 3];
x = [1 2 3 5 4 6 0 0 0];
y=conv(x,h);
n=0:1:((length(x)+length(h))-2);
stem(n,y);

17
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Question 2:
We have an impulse response of a system h(n)= {-3,1,2, 4,1,0, 4,0,3}, provided with an input
x(n)={1 2 3 5 4 6}, find and plot y(n). Also check and comment on the causality property of the
system.
Answer Non - Causal system as h(t) at t<0 has some value.
Code:
h = [-3 1 2 4 1 0 4 0 3];
x = [1 2 3 5 4 6 0 0 0];
y=conv(x,h);
n=0:1:((length(x)+length(h))-2);
stem(n,y);

18
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Question 3:
If impulse response of the system h1(n)= {1 1 1}, h2(n)= {0,0, 1} and h3(n)= {1,2,0, 1} , Input
x1(n)={1 2 3 5 4 6} and x2(n)={1 2 3 5 4 6}, find and plot y(n). Also check and comment on the
causality property of the system.

19
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Answer: Causal system as h(t) at t<0 = 0.

20
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Laboratory Session No. 07

Objective:
To generate a square wave in time domain of specific time period and pulse width and to apply
CTFS equation to compute the spectral coefficients using MATLAB.
Post Lab Exercises:
Question 1:
Generate a square wave of magnitude 2 units with duty cycle of 50 % and have a frequency of 5
Hz using MATLAB square() function and then plot the frequency spectrum (magnitude vs
frequency plot) of the resulting wave using MATLAB script. Also, verify your MATLAB results
using hand calculations.
Answer:
MATLAB Script for Square Wave generation:
t = 0:0.001:1;
y = square(2*pi*5*t,50) + 1;
plot(t,y);
axis([0 1 -0.5 2.5]);
grid on;

21
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

y(t) = 2, 0 < t < 0.1


0, 0.1 < t <0.2
Time period = 0.2s

∫ ∫

MATLAB Script
t = 0:0.001:0.2-0.001;
y = square(2*pi*5*t,50) + 1;
n = -10:10;
F0 = 5;
C = zeros(1,21);
for nn = n
I = exp(-1i*2*pi*F0*nn.*t);
C(nn+ceil(length(n)/2)) = (sum(y.*I)/length(y));
end
n = F0.*n;
stem(n,abs(C));
grid on;

22
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Manual calculations by hand:

∫ ∫

∫ ∫

∫ ∫

∫ ∫

23
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Laboratory Session No. 08

Objective:
Analysis and Synthesis of Signals through Discrete Fourier Transform (DFT)
Post Lab Exercises:
Question 1:
Discuss significance of “N” in N point DFT.
Answer: The parameter N dictates how many samples from the discrete time domain signal are
processed to calculate the discrete Fourier Transform. The parameter also dictates the resolution
of the frequency domain results an amount of time necessary to calculate an N-point DFT.

Question 2:
DFT can deal both ______Periodic_____ and ____Aperiodic________ signals.

Question 3:
Consider the following figure, if this system receives input
then, plot outputs at point “b” and “c”.
(Note: Both magnitude and phase plot is required at point “c”)

Answer:
Code: clear all; close all; clc;
t=0:.00001:10e-3;
xt=sin(2*pi*1000*t)+sin(2*pi*3000*t)+cos(2*pi*0*t)+cos(2*pi*2000*t);
N=10;

24
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

n=0:N-1;
Fs=10000; Ts=1/Fs;
xn=sin(2*pi*1000*n*Ts)+sin(2*pi*3000*n*Ts)+cos(2*pi*0*n*Ts)+cos(2*pi*0*n*Ts);
t1 = n.*Ts;
subplot(411);
plot(t,xt,'r');
hold on
stem(t1,xn,'filled');
xlabel ('Time (sec)');
ylabel ('Amplitude');
title('Input signal')
grid
Xk=zeros(1,N);
iXk=zeros(1,N);
j=sqrt(-1);
for k=0:N-1
for n=0:N-1
Xk(k+1)=Xk(k+1)+(xn(n+1)*exp((-j)*2*pi*k*n/N));
end
end
ft=fft(xn);
magnitude=abs(ft);
k=0:N-1;
Df = Fs/N;
Fk = k.*Df;
subplot(412);
stem(Fk,magnitude,'filled','r');
xlabel ('Frequency (Hz)');
ylabel ('Magnitude');
xlim([0 Fs])
title('Magnitude Spectrum')
grid
phase=angle(round(Xk));
phase=(phase.*180)/pi;
subplot(413);
stem(Fk,phase,'filled','r');
xlabel ('Frequency (Hz)');
ylabel ('Phase');
xlim([0 Fs])
title('Phase Spectrum')
grid

25
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Question 4:
Discuss at least three applications of FFT in Electrical power system Engineering.
Answer:
- It’s used for power system harmonic analysis.
- It’s used in power quality assessment to check various abnormalities.
- It’s used in power (voltage and Current) measurement.

26
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Laboratory Session No. 09

Objective:
To become familiar with practical constraints in calculating Fourier Transform
of any real world signal.
Post Lab Exercises:
Question 1:
Briefly discuss the concept of spectral leakage.
Answer:
If input signal contains frequency which is not integral multiple of analysis frequency(kFs/N)
then it will show up at all dft bins i.e leak into other dft bins which means spectrum is not
correctly estimated.
Question 2:
Discuss the solution of spectral leakage.
Answer:
Solution of DFT leakage is set a window size , normally window used by matlab is in such a way
that it occurs in dft leakage and therefore noise and distortion on sides , the set window
minimizes this leakage and noise on the sides.
Question 3:
Discuss the importance of window size (value of N) on frequency spectrum.

Answer:
The purpose of windowing your data before processing it with a discrete Fourier transform (DFT
or FFT), is to minimize spectral leakage, So your window length should match the length of your
sample sequences. For instance, with 1024 samples, your window length should be 1024 else
there will be less than required information taken by small window affecting the frequency as
well.

27
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Question 4:
Run the following MATLAB script, observe and discuss the resulting plots.

clear; close;
fs=4000;
N=400;
n=0:N-1;
y=10*cos(2*pi*1500*n/fs);
f=abs(fft(y));
k=0:N-1;
df=fs/N;
kf=k.*df;
subplot(411);
stem(kf,f);
xlabel('Frequency Hz');
title('Magnitude Spectrum Without Spectral Leakage');
% If frequency bins are not available (Spectral Leakage) %
N=50;
n=0:N-1;
nfft=1024;
y=100*cos(2*pi*1500*n/fs);
f=abs(fft(y,nfft));
k=0:nfft-1;
df=fs/nfft;
kf=k.*df;
subplot(412);
plot(kf,f);
xlabel('Frequency Hz');
title('Magnitude Spectrum With spectral leakage Leakage');

% Solution of Spectral leakage (Windowing)%


win=window(@hamming,N);
subplot(413);
stem(n,win);
title('Window');

28
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

y_win=y.*win';
f_win=abs(fft(y_win,nfft));
k=0:nfft-1;
df=fs/nfft;
kf=k.*df;
subplot(414);
plot(kf,f_win);
xlabel('Frequency Hz');
title('Magnitude Spectrum With Windowing');

Answer:
The first plots shows plot which should be present but in the second plot its seen that that not the
case and plot is distorted a bit due to leakage but by setting a proper window which minimizes
the distortion on the sides its seen that the plot is corrected.

29
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Laboratory Session No. 10


(Open Ended Lab 02)

Objective:
To convert an analog (current) signal into digital signal using ADC (audio card).
Display it on MATLAB Simulink environment and perform FFT of the resulting
current signal.
Required Components:
7. Audio Card
8. Current Sensor (current sensing resistor / hall effect sensor / CT)
9. Vero board
10. Audio jack
11. Harmonic producing Load (Electronic devices)
12. PC with MATLAB environment
Procedure:
 Using current sensor, convert the current flowing through load into an equivalent voltage.
 If required, using VDR to convert the voltage as obtained from current sensor to a voltage
compatible to audio card (show all the calculations of resistances with their power
ratings).
 Set the sampling frequency of the audio card ADC in MATLAB Simulink environment
with proper justification
 Plot the acquired current waveform to Simulink scope.
 Mention the safe operating range of your equipment.
 Plot the frequency spectrum of the obtained current waveform. Use windowing function
to reduce DFT leakage if required.
 Also, plot the frequency spectrum of the line voltage as obtained in open ended lab 01.

Calculations:
When we began with a CT, it was found there was a DC offset present in the current plot on the time
scope. The reason for this was determined to be the core of the CT saturating. So a current sensing
resistor was fashioned from a copper wire of standard gauge 29. This wire has a resistance of 0.3 Ω and a
high power dissipation capability.

30
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Results:

Current waveform

current fft

voltage fft
CODE:
clear all;clc;
fs = 44100;
win_size = 80e-3*fs;
win = window(@hamming,win_size);

31
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

zero_pad = zeros(win_size/2,1);
sound_card = audioDeviceReader('SamplesPerFrame',win_size);
current_data_raw = sound_card();
current_data_raw = current_data_raw.*(1/(16*0.3));
current_data = [zero_pad ;current_data_raw(1:end/2)];

figure;
while (true)%~isDone(sound_card)
current_data_windowed = current_data.*win;
current_spectrum = abs(fft(current_data_windowed,fs))/fs;
clf;
plot(current_spectrum); axis([0 1000 0 0.0003]);grid minor;grid on;
drawnow;
overlap_points = current_data_raw(1+end/2 : end);
current_data_raw = sound_card();
current_data_raw = current_data_raw.*(1/(16*0.3));
current_data = [overlap_points ;current_data_raw(1:end/2)];
end

release(sound_card);

32
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

fs = 44100;
win_size = 80e-3*fs;
win = window(@hamming,win_size);
zero_pad = zeros(win_size/2,1);

sound_card = audioDeviceReader('SamplesPerFrame',win_size);
voltage_data_raw = sound_card();

voltage_data = [zero_pad ;voltage_data_raw(1:end/2)];

figure;
while (true)%~isDone(sound_card)

voltage_data_windowed = voltage_data.*win;
voltage_spectrum = abs(fft(voltage_data_windowed,fs));
clf;
plot(voltage_spectrum/fs*2); axis([0 300 0 0.05]); grid on; grid minor;
drawnow;
overlap_points = voltage_data_raw(1+end/2 : end);
voltage_data_raw = sound_card();

voltage_data = [overlap_points ;voltage_data_raw(1:end/2)];


end

release(sound_card);

33
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

Laboratory Session No. 11

Objective:
To understand the concept of window overlapping.
Post Lab Exercises:
Question 1:
Run the following MATLAB script, attach and discuss the resulting plot.
clear all;
clc;
[y fs] = audioread('wavTones.com.unregistred.warble_2000-4000Hz_-6dBFS_3s.wav');
n=0:5000;
win_size = 40e-3*fs;
frame_rate = 20e-3*fs;
Y = buffer(y,win_size,frame_rate);
[m n] = size(Y);
win = window(@hamming,win_size);
YYF = [];
for i = 1:n
YY = Y(:,i).*win;
YF = abs(fft(YY,fs));
YYF = [YYF YF];
end
figure;
for i = 1:n
clf;
plot(YYF(:,i));
drawnow;
end

Answer:

The Above code shows us use of buffer command which forms our next window in such a way
that it overlaps the previous window and overlapping all other windows as a normal hanning
window may introduce abnormalities as well as it’s minimizing the sides which may be
important but by overlapping this is resolved.

34
Laboratory Session Digital Signal Processing
Department of Electrical Engineering NED University of Engineering and Technology

35

You might also like