electrical & computer engineering technology - …fd.valenciacollege.edu/file/mejaz/dsp...
TRANSCRIPT
Electrical & Computer Engineering Technology
EET 4190C – Digital Signal Processing
Laboratory Experiments
by
Masood Ejaz
Experiment # 1
Quantization of Analog Signals and Calculation of Quantized noise
Objective: This is a MATLAB based exercise. Objective of this exercise is to take user
generated analog signal data and calculate the quantized value for each data point using Analog-
to-Digital converter specifications, also given by the user. Finally, plot both the sampled signal
and quantized signal and calculate signal-to-quantized noise ratio.
Program Requirements:
1. Give proper explanation of your program at the beginning of your script file. Also, include
your name, class, semester and date in the first line of the script file. Hence, the first few
lines will be comments
2. Clear the command window through your program (clc).
3. Ask user to enter sampled data vector (use input function)
4. Ask user to give you information about ADC, including, number of bits and maximum and
minimum reference voltages (use input function)
5. From ADC information, calculate number of steps and step size. Do not display in the
command window.
6. Analyze each value of the input sampled data vector and generate the equivalent quantized
value. Do not display in the command window (One way to do it is to use for loop to analyze
each point and generate a corresponding quantized value using proper conversion method
learned in theory).
7. Plot both original function from sampled data values (use plot function) and quantized
function from quantized data vector that you generated through your program (use stairs
function). Properly label your x-axis and y-axis, give a proper title, and add a legend on the
plot to display the information about original signal and quantized signal plotting patterns.
8. Calculate the value of signal-to-quantized noise in both numbers and decibels. Properly
display the values in the command window (use fprintf function with proper output message).
NOTE: Make sure to use comments to describe different steps of your program.
Submission Information:
Submit the following on the Blackboard by due date:
1. Your MATLAB script file. File naming format should be Your first name_last name_Ex1.
2. A paper with proper explanation of analog-to-digital conversion process with proper
expressions to go from input analog voltage to output digital code. This should be a
Microsoft Word file with same naming format as described above.
Sample Program Output:
Figure 1: Command Window Input and Output
Figure 2: Plot of Original and Quantized Signals
0 500 1000 1500 2000 2500-8
-6
-4
-2
0
2
4
6
8
Samples
Am
plitu
de
Origianl Signal and Quantized Signal
v(t)
vq(t)
Experiment # 2
Digital Convolution
Objective: This is a MATLAB based exercise. Objective of this exercise is to create a program
that takes user given input and impulse response vectors and produces output of a linear time-
invariant system. Also, program should generate a plot of all three quantities, i.e. input, impulse
response and output
Program Requirements:
1. Give proper explanation of your program at the beginning of your script file. Also, include
your name, class, semester and date in the first line of the script file. Hence, the first few
lines will be comments
2. Clear the command window through your program (clc).
3. Ask user to enter input data vector x(n) for n ≥ 0 (use input function)
4. Ask user to enter impulse response vector h(n) for n ≥ 0 (use input function)
5. Calculate output vector y(n) using convolution between x(n) and h(n) as follows,
𝑦 𝑛 = 𝑥 𝑛 ∗ ℎ 𝑛 = 𝑥 𝑘 ℎ(𝑛 − 𝑘)
∞
𝑘=0
Note that if length of x is N and length of h is M then length of the output vector y will be
N+M-1
6. Check your output values using MATLAB convolution function conv(). Make sure conv() is
just to check your values, it shouldn’t be part of the code.
7. Create three subplots with x(n), h(n), and y(n).
NOTE: Make sure to use comments to describe different steps of your program.
Submission Information:
Submit the following on the Blackboard by due date:
1. Your MATLAB script file. File naming format should be Your first name_last name_Ex2.
2. A paper with proper explanation of digital convolution process and different methods to
calculate digital convolution. This should be a Microsoft Word file with same naming format
as described above
Sample Program Output:
Figure 1: Command Window Input and Calculated Output
Figure 2: Plot of Input, Impulse Response, and Output Vectors
1 2 3 4 5 6 7 8 90
5
10
x(n
)
Digital Convolution
1 2 3 4 5 6 7 8 90
5
10
h(n
)
1 2 3 4 5 6 7 8 90
50
100
y(n
)
n
Experiment # 3
Generation of Digital Signals and Signal Spectral Analysis
Objective: This is a MATLAB based exercise. The objective of this exercise is to generate
different digital signals and to investigate about their signal spectra and bandwidth using
Discrete Fourier Transform.
Program Requirements:
1. Include your name, class, semester and date in the first line of the script file followed by a
proper explanation of your program. Hence, the first few lines of your script file should be
comments.
2. Clear the command window through your program (clc).
Signal # 1: White Noise with Gaussian Distribution
3. Generate a random White Noise with Gaussian/Normal Distribution signal vector, x(n),
comprised of 4096 points having zero mean and standard deviation of five, using randn
function. Check help of the function to further your knowledge.
4. Assume that 4096 (212
) samples of the above signal that you generated are obtained at a
sampling frequency of 8KHz. Obtain the signal spectrum X(k) (fft).
5. Plot the sampled signal x(n) vs. n (stem) and amplitude spectrum, AX, of the signal X(f) vs. f
(plot) (Make sure to convert the x-axis for X(f) from discrete k to continuous f) using two
subplots. For better visualization of the original signal, plot only first 100 samples of the
input signal. Properly label your plots. This will be figure 1.
6. Print the value of frequency resolution, Δf, in the command window through your program
and comment on the bandwidth of the signal that you observe from signal spectrum (Note: It
will be helpful to read some resources about white noise before making comment on its
bandwidth)
7. Include sound function in your program to play a sound corresponding to the scaled value of
white noise vector,
sound(x/max(abs(x)),fs)
where fs is sampling frequency.
Signal # 2: Sum of Sinusoidal Signals
8. Generate the following three sinusoidal signals sampled at 8KHz up to time length of 0.1
second.
(i) 1( ) 5cos[2 (500) ]x t t
(ii) 2 ( ) 5cos[2 (1200) 0.25 ]x t t
(iii) 3( ) 5cos[2 (1800) 0.5 ]x t t
9. Create a signal x(t) that is the sum of x1, x2, and x3. Note that x(t) is also a sampled signal
with sampling rate of 8KHz up to time length of 0.1 second.
10. Calculate the frequency spectrum of x(n), i.e. X(k)
11. Plot two figures; figure 2 with four subplots with x1, x2, x3, and x, and figure 3 with two
subplots with amplitude of the spectrum, X(k) and X(f), i.e. discrete scale k converted into
continuous scale f. Plot only first 30 samples of the time signals x1, x2, x3, and x for a better
visualization. Properly label your plots.
12. Properly print the following in the command window through your program
(i) Frequency resolution
(ii) Frequency components from spectrum analysis
(iii) Bandwidth of the signal from spectrum analysis
13. Include sound function in your program to play a sound corresponding to the scaled values of
x1, x2, x3, and x. Use pause at the end of each sound function to create a pause between the
successive signal sounds,
sound(x21/max(abs(x21)),fs),pause
Signal # 3: Digital Speech Waveform
14. A data file with speech waveform sampled at 8KHz is being provided to you (speech.dat).
Load the data in MATLAB and assign it to a sample variable.
load speech.dat
x3 = speech;
15. Obtain the frequency spectrum of the sampled data (fft).
16. Calculate the amplitude spectrum from frequency spectrum.
17. Create two subplots, one with given samples vs. time and other with amplitude of the
frequency spectrum of samples vs. frequency in Hz. This will be figure 4.
18. From the plots, observe the maximum frequency (bandwidth) of the signal.
19. Print the following in the command window through your program,
(i) Frequency resolution
(ii) Bandwidth of the signal
20. Include sound function in your program to play a sound corresponding to the scaled value of
the speech vector.
Reference
Lab 1: Generation of Digital Signals and Signal Spectral Analysis Using MATLAB by Li Tan.
http://www.elsevierdirect.com/v2/companion.jsp?ISBN=9780123740908
Experiment # 4
Z-transfer Functions, Difference Equations, and Filter Implementation
Objective: This is a MATLAB based exercise. The objective of this exercise is to create two
programs to implement a fourth-order band-pass filter in two different ways. The fourth-order
band-pass filter is defined by the following transfer function:
𝐻 𝑧 =0.0201 − 0.0402𝑧−2 + 0.0201𝑧−4
1 − 2.1192𝑧−1 + 2.6952𝑧−2 − 1.6924𝑧−3 + 0.6414𝑧−4
Description of each of the program is given in the program requirements section as follows.
Program Requirements:
1. Include your name, class, semester and date in the first line of the script file followed by a
proper explanation of your program. Hence, the first few lines of your script file should be
comments.
2. Clear the command window through your program (clc).
Program # 1 (script file naming format: First Name_Last Name_Ex4a)
3. Load the speech.dat file (we lost the golden chain). Assume that the sampling frequency (fs)
is 8KHz.
4. Plot the filter’s magnitude (in decibel) and phase response (in degrees) equations from H(z)
by taking z = ej from zero to the folding frequency (fs/2). Magnitude response axis should
be set from -40db to 0db and phase response axis should be set between -200o to 200
o. This
will be figure 1 with two subplots.
5. Create difference equation to represent system output y(n) using the transfer function H(z). In
the difference equation, input x(n) is the samples of the speech signal.
6. Implement the difference equation to calculate the output y(n). Length of y(n) should be the
same as speech signal x(n).
7. Plot the input x(n) and output y(n) in two subplots. x-axis of both the sub-plots should be in
second (time). This will be figure 2.
8. Calculate the spectral contents corresponding to the input x(n) and output y(n) using fft()
function. Calculate amplitude response for each of the spectra.
9. Plot the amplitude spectra of input and output in two sub-plots. x-axis for each of the sub-
plots should be in hertz going from zero to folding frequency. This will be figure 3.
10. Include sound function in your program to play sounds corresponding to the scaled values of
your input x(n) and output y(n).
sound(x/max(abs(x)), fs), pause sound(y/max(abs(y)), fs)
where fs is the sampling frequency.
Program # 2 (script file naming format: First Name_Last Name_Ex4b)
11. Load the speech.dat file (we lost the golden chain). Assume that the sampling frequency (fs)
is 8KHz.
12. Calculate the frequency response of the filter using freqz function (if available) or freqz_ejaz
function,
[H,W] = freqz([0.0201, 0, -0.0402, 0, 0.0201], [ 1, -2.1192, 2.6952, -
1.6924, 0.6414], K);
or
[H,W] = freqz_ejaz([0.0201, 0, -0.0402, 0, 0.0201], [ 1, -2.1192, 2.6952,
-1.6924, 0.6414], K);
where H is the frequency response evaluated at the values of W. W is normalized frequency
vector, between 0 to divided into K points. Take K = 512 points. Observe that the two
vectors in freqz or freqz_ejaz functions correspond to the coefficients of z-k
in the numerator
followed by the denominator.
13. Plot the filter’s magnitude (in decibel) and phase response (in degrees) equations from H.
Magnitude response axis should be set from -40db to 0db and phase response axis should be
set between -200o to 200
o. This will be figure 1 with two subplots.
14. Calculate the output of the filter using filter function,
y = filter([0.0201, 0, -0.0402, 0, 0.0201], [ 1, -2.1192, 2.6952, -1.6924,
0.6414], x);
Observe that the two vectors in filter function correspond to the coefficients of z-k
in the
numerator followed by the denominator.
15. Plot the input x(n) and output y(n) in two subplots. x-axis of both the sub-plots should be in
second (time). This will be figure 2.
16. Calculate the spectral contents corresponding to the input x(n) and output y(n) using fft()
function. Calculate amplitude response for each of the spectra.
17. Plot the amplitude spectra of input and output in two sub-plots. x-axis for each of the sub-
plots should be in hertz going from zero to folding frequency. This will be figure 3.
18. Include sound function in your program to play sounds corresponding to the scaled values of
your input x(n) and output y(n).
sound(x/max(abs(x)), fs), pause sound(y/max(abs(y)), fs)
where fs is the sampling frequency.
Experiment # 5
FIR Filter Design Using Window Functions
Objective: This is a MATLAB based exercise. The objective of this exercise is to create a
program that calculates the frequency response of a low-pass, high-pass, band-pass, or band-stop
filter given number of taps, sampling frequency, and cut-off frequency for low-pass and high-
pass filters and upper and lower cut-off frequencies for band-pass and band-stop filters. Also,
four window functions, triangular, Hanning, Hamming, and Blackman will be used to smooth
out the original filter’s response and to eliminate the oscillations in the pass and stop bands due
to Gibbs effect.
Description of the program is given in the program requirements section as follows.
Program Requirements:
1. Include your name, class, semester and date in the first line of the script file followed by a
proper explanation of your program. Hence, the first few lines of your script file should be
comments.
2. Clear all the variables (clear) and the command window through your program (clc).
3. Ask user to enter the following information:
a. Number of taps (2M + 1)
b. Filter type
Filter_Type = input('Enter Filter Type; lowpass, highpass, bandpass, or
bandstop:', 's');
[Note: ‘s’ stands for string; hence Filter_Type is a string or character
variable]
c. Sampling frequency in hertz
d. Window type (triangular, hanning, hamming, blackman)
4. Use switch Filter_Type to select one of the four filter types. There will be four cases for the
switch function; lowpass, highpass, bandpass, and bandstop.
5. Start with case {lowpass}. Ask user to enter the cutoff frequency fc in hertz.
6. Calculate normalized frequency c.
7. Calculate the causal filter coefficients, bo to b2M, total 2M + 1, from non-causal filter
coefficients, h(-M) to h(M) calculated from table 7.1 of your text book.
8. Calculate the frequency response using freqz_ejaz function. Use 1024 points between = 0
to .
9. Use another switch function, this time for window type. There will be four cases for this
switch; triangular, hanning, hamming, and blackman.
10. Calculate window function coefficients, w, from –M to M. Multiply h and w to get new non-
causal filter coefficients, hw.
11. From hw, calculate the causal filter coefficients bw, bwo to bw2M.
12. Calculate windowed filter frequency response using freqz_ejaz function. Use 1024 points for
normalized frequency
13. Create two figures, each with two sub-plots; figure (1) with sub-plots for original and
windowed filter magnitude responses from zero to fs/2 hertz, and figure(2) with two phase
responses, in degrees, from original and windowed filter frequency responses, from zero to
fs/2 hertz.
14. Now create the second case for switch Filter_Type; case {highpass}. Repeat everything that
you did for low-pass filter
15. Create the remaining two cases for switch Filter_Type; case {bandpass} and switch
Filter_Type; case {bandstop}. Note that you have to ask user to enter upper cut-off and lower
cut-off frequencies for these two types of filters instead of a single cut-off frequency.
Program Output Samples
Low-pass Filter
0 500 1000 1500 2000 2500 3000 3500 40000
0.2
0.4
0.6
0.8
1
in radian
Mag
nitu
de R
esp
onse
Original Filter Response
0 500 1000 1500 2000 2500 3000 3500 40000
0.1
0.2
0.3
0.4
0.5
in radian
Mag
nitu
de R
esp
onse
Windowed Filter Response
High-pass Filter
0 500 1000 1500 2000 2500 3000 3500 4000
-100
0
100
in radian
Pha
se R
esp
onse
Original Filter Response
0 500 1000 1500 2000 2500 3000 3500 4000
-100
0
100
in radian
Pha
se R
esp
onse
Windowed Filter Response
0 2000 4000 6000 8000 10000 12000 14000 16000 180000
0.2
0.4
0.6
0.8
1
in radian
Mag
nitu
de R
esp
onse
Original Filter Response
0 2000 4000 6000 8000 10000 12000 14000 16000 180000
0.2
0.4
0.6
0.8
1
in radian
Mag
nitu
de R
esp
onse
Windowed Filter Response
Band-pass Filter
0 2000 4000 6000 8000 10000 12000 14000 16000 18000
-100
0
100
in radian
Pha
se R
esp
onse
Original Filter Response
0 2000 4000 6000 8000 10000 12000 14000 16000 18000
-100
0
100
in radian
Pha
se R
esp
onse
Windowed Filter Response
0 2000 4000 6000 8000 10000 12000 14000 16000 180000
0.2
0.4
0.6
0.8
1
in radian
Mag
nitu
de R
esp
onse
Original Filter Response
0 2000 4000 6000 8000 10000 12000 14000 16000 180000
0.2
0.4
0.6
0.8
1
in radian
Mag
nitu
de R
esp
onse
Windowed Filter Response
Band-stop Filter
0 2000 4000 6000 8000 10000 12000 14000 16000 18000
-100
0
100
in radian
Pha
se R
esp
onse
Original Filter Response
0 2000 4000 6000 8000 10000 12000 14000 16000 18000
-100
0
100
in radian
Pha
se R
esp
onse
Windowed Filter Response
0 2000 4000 6000 8000 10000 12000 14000 16000 180000
0.2
0.4
0.6
0.8
1
in radian
Mag
nitu
de R
esp
onse
Original Filter Response
0 2000 4000 6000 8000 10000 12000 14000 16000 180000
0.2
0.4
0.6
0.8
in radian
Mag
nitu
de R
esp
onse
Windowed Filter Response
0 2000 4000 6000 8000 10000 12000 14000 16000 18000
-100
0
100
in radian
Pha
se R
esp
onse
Original Filter Response
0 2000 4000 6000 8000 10000 12000 14000 16000 18000
-100
0
100
in radian
Pha
se R
esp
onse
Windowed Filter Response
Experiment # 6
FIR Filter Application: Noise Reduction
Objective: This is a MATLAB based exercise. The objective of this exercise is to create a
program to design a low-pass filter according to the given specifications. This filter is then used
to curb noise from a noisy tone signal. Responses of the original signal and its frequency
spectrum, and filtered signal and its frequency spectrum will be plotted and studied
Description of the program is given in the program requirements section as follows.
Program Requirements:
1. Include your name, class, semester and date in the first line of the script file followed by a
proper explanation of your program. Hence, the first few lines of your script file should be
comments.
2. Clear all the variables (clear) and the command window through your program (clc).
3. Produce 250 samples of a noisy tone signal sampled at 8KHz, yn = x + n, from the following
clean tone signal,
𝑥 𝑡 = 2sin(1000𝜋𝑡)
and normalized Gaussian noise signal generated using MATLAB function randn,
noise = randn(1,250); n = noise/max(abs(noise));
4. Calculate the frequency spectrum of the noisy signal yn using fft function. Calculate the
amplitude spectrum from frequency spectrum.
5. Create figure(1) with two sub-plots; one with the noisy signal against number of samples,
and second with the amplitude spectrum of the noisy signal against cyclic frequency in Hz.
Keep your cyclic frequency range from 0 to folding frequency.
6. Design an FIR filter with fpass = 800Hz and fstop = 1KHz. Maximum pass-band ripple should
not exceed 0.02dB and minimum stop-band attenuation should not be more than 50dB.
(Design Hints: Calculate normalized transient bandwidth, Δf. Using Δf and ripple
information, decide which window function satisfies the design requirements and then
calculate number of taps. Calculate cut-off frequency. Using number of taps, cut-off
frequency and window type, use experiment 5 concepts to generate windowed LPF
coefficients)
7. Apply designed filter on the noisy signal yn to get filtered signal. Use filter function of
MATLAB.
8. Use fft function to calculate the frequency spectrum of the filtered signal. Calculate
amplitude spectrum from frequency spectrum.
9. Create figure(2) with two sub-plots; one with the filtered signal against number of samples,
and second with the amplitude spectrum of the filtered signal against cyclic frequency in Hz.
Keep your cyclic frequency range from 0 to folding frequency.
10. Include comments in your script file regarding the technical reason behind the delay that you
see in filtered signal graph. Also, comment on the difference between the two amplitude
spectra, noisy signal and filtered signal.
Program Output Samples
Figure 1: Noisy Signal and its Spectrum
0 50 100 150 200 250-3
-2
-1
0
1
2
3
Samples n
Nois
e S
igna
l in
tim
e
Noisy Signal
0 500 1000 1500 2000 2500 3000 3500 40000
0.5
1
1.5
Frequency in Hz
Fre
qu
en
cy
Re
spo
nse
Frequency Spectrum of the Noisy Signal
Figure 2: Filtered Signal and its Spectrum
0 50 100 150 200 250-3
-2
-1
0
1
2
3
Samples n
Filt
ere
d S
ign
al in
tim
e
Filtered Signal
0 500 1000 1500 2000 2500 3000 3500 40000
0.5
1
1.5
Frequency in Hz
Fre
qu
en
cy
Re
spo
nse
Frequency Spectrum of the Filtered output Signal