📄 compexer.txt
字号:
PROGRAMMING EXERCISES FOR CHAPTERS 2-12 OF:
"THE SCIENTIST AND ENGINEER'S GUIDE TO DIGITAL SIGNAL PROCESSING"
COMMENTS AND SUGGESTIONS ON THESE EXERCISES ARE WELCOME!
SEND THEM TO THE AUTHOR AT: SMITH@DSPGUIDE.COM
CHAPTER 2: STATISTICS, PROBABILITY AND NOISE
Use the following algorithm to find the value of each sample in a test
signal, where the function "rnd" returns a random number between 0 and 1:
x = rnd
if x < 0.6 then x = x-0.2
1. Calculation of the histogram.
a. Sketch the pdf of the process that generated this signal.
b. Generate and plot a signal containing 300 points from this algorithm.
c. Generate and plot the histogram of 1000 points acquired from this
algorithm. Use a bin width of 0.01.
d. Repeat (b) using 10,000 samples.
e. Repeat (b) using 100,000 samples.
f. Estimate the peak-to-peak noise on the histograms in (c) to (e). Give
your answer as the actual number, not a percentage.
g. Which of these three histograms is the best estimate of the pdf? The
worst estimate?
h. Which histogram has the lowest peak-to-peak noise? The highest peak-to-
peak noise?
i. Explain the apparent contradiction between your answers in (g) and (h).
2. Calculation of the mean and standard deviation.
a. Generate a signal containing 10,000 points from this algorithm. Calculate
the mean and standard deviation, using a method of your choice.
b. Generate three signals as in (a), and add them together. Calculate the
mean and standard deviation of the resulting signal.
c. Repeat (b) adding 10 signals.
d. Repeat (b) adding 30 signals.
e. Make a graph of your data in (a)-(d), proving that when random signals
are added, their means add and their standard deviations add in quadrature.
In your graphs, the x-axis should be "number of points," and the y-axis
should be "mean" or "standard deviation."
3. The Central Limit Theorem.
a. Calculate and plot the histogram of the signal in 2(c).
b. On this same graph, show a Gaussian curve with the mean and standard
deviation determined in 2(c).
c. Comment on the match between the two in terms of accuracy and precision.
d. Would increasing the number of samples improve the accuracy or precision
of the match?
e. Would increasing the number of signals added improve the accuracy of the
match?
CHAPTER 3: ADC AND DAC
1. Generate a 1000 point digital signal that simulates an analog signal,
consisting of a sine wave at 30.76 hertz, being sampled at 1 kHz for 1
second. Plot this digital signal with the x-axis labeled "time", running
from 0 to 1 second.
2. Simulate sampling the analog signal at 200 Hz, by discarding every 4 out
of 5 samples from the digital signal you created in step 1.
a. Plot this resampled signal, with the x-axis labeled as "time", running
from 0 to 1 second.
b. Has aliasing occurred in this signal? Explain.
c. What is the digital frequency of this signal?
d. Could the original analog signal be reconstructed from this digital
signal?
3. Repeat #2 with the sampling at 50 Hz, 33.33 Hz, and 28.57 Hz.
CHAPTER 4: DSP SOFTWARE
This exercise looks at the problem of adding numbers that are very different
is size. Write a computer program(s) to complete the following.
1. Generate a 256 samples long sine wave with an amplitude of one, and a
frequency such that it completes 3 full periods in the 256 samples.
Represent each of the samples using single precision floating point. We
will call this signal, x[ ]
2. Add a constant, k = 300,000 to each of the samples in the signal.
3. Subtract the same constant, k, from each of the samples. Call this
reconstructed signal, y[ ]
4. Find the difference (i.e., the reconstruction error) between x[ ] and
y[]. Call this signal, d[ ].
5. Plot the reconstructed signal, y[ ], and the difference signal, d[ ].
6. Repeat steps 1-5 for k = 3,000,000.
7. Repeat steps 1-5 for k = 30,000,000.
8. Answer the following questions:
a. "When floating point numbers of very different size are added, the
quantization noise on the [fill in the blank] number destroys the
information contained in the [fill in the blank] number. "
b. The results of step 7 show that the information in the reconstructed
signal is completely destroyed with k is equal to 30 million, or greater.
If this exercise were repeated with the sine wave of amplitude 0.001, how
large of value of k would be needed to destroy the information in the
reconstructed signal?
c. If this exercise were repeated using double precision, how large of value
for k would be needed to destroy the information in the reconstructed
signal?
CHAPTER 5: LINEAR SYSTEMS
These computer exercises will help you understand "The Fundamental Concept
in DSP", as illustrated in Fig. 5-11.
1. Generate and plot the following four signals, each 500 samples long:
a. x1[n] = sin(2*pi*n/100)
b. x2[n] = 4*exp(-(n-150)^2/300) - exp(-(n-150)^2/2500)
c. x3[n] = 1 for 240 < n < 300
-2 for 299 < n < 380
0 otherwise
d. x4[n] = rnd+rnd+rnd+rnd+rnd+rnd-3
(where the function rnd returns a random number uniformly
distributed between 0 and 1)
2. Generate and plot the sum of these four signals:
x[n] = x1[n] + x2[n] + x3[n] + x4[n]
3. Pass each of the five signals through the linear system defined below,
creating the signals: y[n], y1[n], y2[n], y3[n] and y4[n]. Using x[n] and
y[n] as an example, the system is defined by the difference equation:
y[n] = 0.05 x[n] + 0.95 y[n-1]
This can be carried out with the following program:
y[0] = 0
for n = 1 to 499
y[n] = 0.05*x[n] + 0.95*y[n-1]
next n
As will be discussed in Chapter 19, this is called a "single-pole recursive
low-pass filter." It operates in the same way as an RC circuit in
electronics, smoothing the waveform and removing high-frequency noise.
4. Compare y[n] with the sum of y1[n], y2[n], y3[n], and y4[n]. Are they
identical? Test this by subtracting one from the other. Plot the result
and explain any difference between the two signals.
CHAPTER 6: CONVOLUTION
1. Write a program that calculates y[n] = x[n]*h[n], where y[n] is 598
samples, x[n] is 500 samples, and h[n] is 99 samples.
2. Generate an impulse response, h[n], according to the algorithm below. As
discussed in Chapter 16, this is the filter kernel for a "low-pass
windowed-sinc" filter. When convolved with an input signal, this filter
passes sinusoids that have fewer than 25 cycles in 500 samples, and blocks
sinusoids with a higher frequency. Make a plot of this signal.
for i = 0 to 98
h[i] = 0.31752 * sin(0.314159 * (i-49.00001)) / (i-49.00001)
h[i] = h[i] * (0.54 - 0.46 * cos(0.0641114 * i))
next i
3. Test your program by convolving h[n] with the signal described below.
What should the output of your program be in response to this signal? Why?
x[n] = 1 for n = 0
x[n] = 0 otherwise
4. Generate a more complicated test signal, x[n], that consists of two
sinusoids added together. The first sinusoid will have an amplitude of 1,
and make 6 complete cycles in the 500 samples. The second sinusoid will
have an amplitude of 0.5 and make 44 complete cycles in the 500 samples.
Make of plot of this signal.
5. Test your convolution program by filtering the signal you created in 4,
with the filter kernel you created in 2. Plot this signal. Has the filter
passed the lower frequency signal, while blocking the higher frequency
signal? Comment on its effectiveness.
6. Turn the filter kernel into a high-pass filter by changing the sign of
all the samples, and then adding one to sample 49 (you can look ahead to
Fig. 14-5 if you want more information on this procedure). Test this
high-pass filter in the same way as step 4.
CHAPTER 7: PROPERTIES OF CONVOLUTION
The exercise looks at two different ways of detect a known waveform in a
noisy signal. The waveform to be detected is an exponentially decaying
pulse. The first detection method is to threshold the first difference of
the signal. The second method is to threshold the correlation of the signal
with the known waveform.
1. Generate a 600 sample test signal, containing three target pulses:
x[n] = exp(-(n-100)/15), if 99 < n < 160
= exp(-(n-300)/15), if 299 < n < 360
= exp(-(n-500)/15), if 499 < n < 560
= 0, otherwise
2. Generate a 600 sample signal of normally distributed random noise with
mean = 0, and SD = 1.
3. Generate four test signals with signal-to-noise ratios (SNRs) of 50, 10,
5, and 2.5. Do this by adding the signal from step 1, with an appropriately
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -