Computer Exercise Chap5-已解锁
Computer Exercise Chap5-已解锁
(a) Using an ideal LPF of bandwidth B = 2000 Hz for reconstruction, illustrate the difference in time
domain between the reconstructed signal and the original signal.
(b) If we use a simple rectangular pulse Π(t/Ts ) of width Ts to reconstruct the original signal from the
ht quantized samples, design and implement the equalizer filter to recover the message as discussed
in Section 5.1.2. Compare the difference between the original signal and the recovered signal.
(c) If we use a simple triangular pulse ∆(t/Ts ) of width Ts to reconstruct the original signal from the
quantized samples, design and implement the equalizer filter to recover the message as discussed
in Section 5.1.2. Compare the difference signal recovery using the two different pulses Π(t/Ts )
and ∆(t/Ts ).
ma Da
COMPUTER EXERCISE Examples
U
ter vis
Computer exercises of this section provide examples of signal sampling, quantization, signal reconstruction
from samples, and image compression and coding.
As an initial step, the next few lines of Python code serve to load the necessary Python packages to be
C
To load your own data or dataset, different Jupyter platforms offer different choices. On Google Colab,
one could first upload a data folder, e.g., “chapter5images” to “MyDrive” under “ColabDataFolders”. Once
the data folder is uploaded, run the following lines to enable access:
py 0@ import os
# Load the cameraman image on Google Colab
data_dirbook=’/content/drive/MyDrive/ColabDataFolders/chapter5images’
The data files within the data folder can be accessed by Jupyter notebook via Python code. For example,
C
rig
to read the image file “cameraman.tiff”, we can use the following line:
G = plt.imread(os.path.join(data_dirbook,’cameraman.tiff’))
Readers are encouraged to explore how to access user data files using their chosen Jupyter nodebook
platforms.
ht
5.9.1 Sampling and Reconstruction of Low-Pass Signals
In the sampling example, we first construct a signal g(t) with two sinusoidal components of 1-second duration;
their frequencies are 1 and 3 Hz. Note, however, that when the signal duration is infinite, the bandwidth of
g(t) would be 3 Hz. However, the finite duration of the signal implies that the actual signal is not strictly
band-limited, although most of the signal content stays within a bandwidth of 5 Hz. For this reason, we
select a sampling frequency of 50 Hz, much higher than the minimum Nyquist frequency of 6 Hz. The
Python program, Exsample.py, implements sampling and signal reconstruction. Figure 5.40 illustrates the
ma Da
original signal, its uniform samples at the 50 Hz sampling rate, and the frequency response of the sampled
signal. In accordance with our analysis of Section 5.1, the spectrum of the sampled signal gT (t) consists of
the original signal spectrum periodically repeated every 50 Hz.
SAMPLING AND RECONSTRUCTION EXAMPLE
# Save this codeblock as ExSample.py
U
# receive 3 signals:
# 1. sampled signal s_out
# 2. sampled and quantized signal sq_out
# 3. sampled, quantized, and zero-order hold signal sqh_out
# calculate the Fourier transforms
Lfft = 2**int(np.ceil(np.log2(Lsig)+1))
Fmax = 1/(2*td)
Faxis = np.linspace(-Fmax, Fmax, Lfft)
Xsig = np.fft.fftshift(np.fft.fft(xsig, Lfft))
S_out = np.fft.fftshift(np.fft.fft(s_out, Lfft))
py 0@ sfig1a = plt.plot(t,xsig,’m’,linewidth=1.5)
sfig1b = plt.plot(t,s_out[:Lsig],’b’,linewidth=2)
plt.xlabel(’time (sec)’)
plt.title(r’Signal ${\it g}({\it t})$ and its uniform samples’)
# non-ideal reconstruction
ZOH = np.ones(Nfactor)
s_ni = np.kron(s_out[::Nfactor], ZOH)
U
–2
0.0 0.2 0.4 0.6 0.8 1.0
time (sec)
Spectrum of g(t)
30
20
10
ht 0
–100 –75 –50 –25 0 25 50 75 100
frequency (Hz)
Spectrum of gT (t)
300
200
100
0
–100 –75 –50 –25 0 25 50 75 100
ma Da
frequency (Hz)
Figure 5.40: The relationship between the original signal and the ideal uniformly sampled signal in the
time (a) and frequency (b, c) domains.
To reconstruct the original signal g(t) from the impulse sampling train gT (t), we applied an ideal LPF
with bandwidth 10 Hz in the frequency domain. This corresponds to the interpolation using the ideal sinc
U
function as shown in Section 5.1.1. The resulting spectrum, as shown in Fig. 5.41, is nearly identical to
ter vis
the original message spectrum of g(t). Moreover, the time domain signal waveforms are also compared in
Fig. 5.41 and show near-perfect match.
Spectrum of ideal filtering (reconstruction)
300
C
200
100
0
ial
–100 –75 –50 –25 0 25 50 75 100
frequency (Hz)
–2
0.0 0.2 0.4 0.6 0.8 1.0
time (sec)
Figure 5.41: Reconstructed signal spectrum and waveform from applying the ideal impulse sampling and
ideal LPF reconstruction.
In our last exercise in sampling and reconstruction, given in the same program, we use a simple rectangular
pulse of width Ts (sampling period) to reconstruct the original signal from the samples (Fig. 5.42). An LPF
is applied on the rectangular reconstruction and also shown in Fig. 5.42. It is clear from comparison to the
This content is protected and may not be shared, uploaded, or distributed.
Co 16Section 5.9. Computer Exercises
original source signal that the recovered signal is still very close to the original signal g(t). This is because
we have chosen a high sampling rate such that Tp = Ts is so small that the approximation of Eq. (5.16)
holds. Certainly, based on our analysis, by applying the low-pass equalization filter of Eq. (5.15a), the
288
EE
reconstruction error can be greatly reduced.
Original signal versus flat-top reconstruction
py 0@ 2
1
0
–1
–2
0.0 0.2 0.4
time (sec)
0.6
0.8 1.0
time (sec)
Figure 5.42: Reconstructed signal spectrum and waveform from applying the simple rectangular recon-
struction pulse (Fig. 5.6) followed by LPF without equalization.
ht
5.9.2 PCM Illustration
The uniform quantization of an analog signal using L quantization levels can be implemented by the Python
function uniquan.py.
DEFINING UNIFORM QUANTIZER
ma Da
def uniquan(sig_in, L):
"""
Usage:
q_out, Delta, SQNR = uniquan(sig_in, L)
L - number of uniform quantization levels
sig_in - input signal vector
U
Function outputs:
ter vis
q_out - quantized output
Delta - quantization interval
SQNR - actual signal to quantization noise ratio
"""
C
The function sampandquant.py executes both sampling and uniform quantization simultaneously. The
sampling period ts is needed, along with the number L of quantization levels, to generate the sampled
This content
output is protected
s out, the and may
sampled and quantized not sq
output beout,
shared,
and the uploaded, or distributed.
signal after sampling, quantizing, and
Co 16
Section 5.9. Computer Exercises
py 0@ Usage:
s_out, sq_out, sqh_out, Delta, SQNR = sampandquant(sig_in, L, td, ts)
L - number of uniform quantization levels
sig_in - input signal vector
td - original signal sampling period of sig_in
ts - new sampling period
C
rig NOTE: td*fs must be a positive integer;
Function outputs:
s_out - sampled output
sq_out - sample-and-quantized output
sqh_out - sample,quantize, and hold output
Delta - quantization interval
SQNR - actual signal to quantization noise ratio
"""
ht if (ts/td).is_integer():
nfac = int(np.round(ts/td))
p_zoh = np.ones(nfac)
s_out = sig_in[::nfac]
sq_out, Delta, SQNR = uniquan(s_out, L)
s_out = np.stack([s_out] +[s_out*0] * (nfac-1),axis=-1).reshape(-1)
ma Da
sqh_out = np.kron(sq_out, p_zoh)
else:
print("Errors! ts/td is not an integer!")
s_out = sq_out = sqh_out = Delta = SQNR = []
return s_out, sq_out, sqh_out, Delta, SQNR
The Python program ExPCM.py provides a numerical example that uses these two Python functions to
U
# plot the original signal and the PCM signal in time domain
fig1 = plt.figure()
This content is protected and may not be shared, uploaded, or distributed.
Co 16
Section 5.9. Computer Exercises
fig1.subplots_adjust(hspace=0.5,wspace=0.4)
plt.subplot(211)
290
EE
sfig1 = plt.plot(t,xsig,’k’,t,sqh_out1[:Lsig],’b’,linewidth=2)
plt.title(r’Signal ${\it g}({\it t})$ and its 16 level PCM signal’)
fig2 = plt.figure()
fig2.subplots_adjust(hspace=0.5,wspace=0.4)
ial
plt.subplot(211)
sfig3 = plt.plot(t,xsig,’b-’,t,s_recv1,’b-.’,linewidth=2)
plt.legend([’original’,’recovered’])
plt.title(r’Signal ${\it g}({\it t})$ and filtered 16-level PCM signal’)
plt.xlabel(’time (sec.)’); plt.grid()
plt.subplot(212)
sfig4 = plt.plot(t,xsig,’b-’,t,s_recv2[:Lsig],’b-.’,linewidth=2)
plt.legend([’original’,’recovered’])
plt.title(r’Signal ${\it g}({\it t})$ and filtered 4-level PCM signal’)
plt.xlabel(’time (sec.)’); plt.grid()
# Set the spacing between subfigures
plt.tight_layout(pad=1.0); plt.show()
In the first example, we maintain the 50 Hz sampling frequency and utilize L = 16 uniform quantization
levels. The resulting PCM signal is shown in Fig. 5.43. This PCM signal can be low-pass-filtered at the
EE
receiver and compared against the original message signal, as shown in Fig. 5.44. The recovered signal is
seen to be very close to the original signal g(t).
py 0@ To illustrate the effect of quantization, we next apply L = 4 PCM quantization levels. The resulting
PCM signal is again shown in Fig. 5.43. The corresponding signal recovery is given in Fig. 5.44. It is very
clear that a smaller number of quantization levels (L = 4) leads to much larger approximation error.
C
rig
ht
ma Da
Figure 5.43: Original signal and the PCM signals with different numbers of quantization levels.
U
ter vis
C
ial
Figure 5.44: Comparison between the original signal and the PCM signals after LPF to recover the original
message.
This content is protected and may not be shared, uploaded, or distributed.