This jupyter notebook is part of a collection of notebooks on various topics of Digital Signal Processing. Please direct questions and suggestions to Sascha.Spors@uni-rostock.de.
The following example illustrates the requantization of a speech signal. The signal was originally recorded with a wordlength of $w=16$ bits. It is requantized by a uniform mid-tread quantizer to various wordlengths. The signal-to-noise ratio (SNR) after quantization is computed and a portion of the (quantized) signal is plotted. It is further possible to listen to the requantized signal and the quantization error. Note, the level of the quantization error has been normalized for better audability of the effects.
import numpy as np
import matplotlib.pyplot as plt
import soundfile as sf
idx = 130000 # index to start plotting
def uniform_midtread_quantizer(x, w):
"""Uniform mid-tread quantizer with limiter."""
# quantization step
Q = 1 / (2 ** (w - 1))
# limiter
x = np.copy(x)
idx = np.where(x <= -1)
x[idx] = -1
idx = np.where(x > 1 - Q)
x[idx] = 1 - Q
# linear uniform quantization
xQ = Q * np.floor(x / Q + 1 / 2)
return xQ
def evaluate_requantization(x, xQ):
"""Evaluate rquantization by plotting signals and computing SNR."""
e = xQ - x
# SNR
SNR = 10 * np.log10(np.var(x) / np.var(e))
print("SNR: {:2.1f} dB".format(SNR))
# plot signals
plt.figure(figsize=(10, 4))
plt.plot(x[idx : idx + 100], label=r"signal $x[k]$")
plt.plot(xQ[idx : idx + 100], label=r"requantized signal $x_Q[k]$")
plt.plot(e[idx : idx + 100], label=r"quantization error $e[k]$")
plt.xlabel(r"sample index $k$")
plt.grid()
plt.legend()
# normalize error
e = 0.2 * e / np.max(np.abs(e))
return e
# load speech sample
x, fs = sf.read("../data/speech.wav")
# normalize sample
x = x / np.max(np.abs(x))
Original Signal ../data/speech.wav
xQ = uniform_midtread_quantizer(x, 8)
e = evaluate_requantization(x, xQ)
sf.write("speech_8bit.wav", xQ, fs)
sf.write("speech_8bit_error.wav", e, fs)
SNR: 34.0 dB
Requantized Signal speech_8bit.wav
Quantization Error speech_8bit_error.wav
xQ = uniform_midtread_quantizer(x, 6)
e = evaluate_requantization(x, xQ)
sf.write("speech_6bit.wav", xQ, fs)
sf.write("speech_6bit_error.wav", e, fs)
SNR: 22.9 dB
Requantized Signal speech_6bit.wav
Quantization Error speech_6bit_error.wav
xQ = uniform_midtread_quantizer(x, 4)
e = evaluate_requantization(x, xQ)
sf.write("speech_4bit.wav", xQ, fs)
sf.write("speech_4bit_error.wav", e, fs)
SNR: 11.7 dB
Requantized Signal speech_4bit.wav
Quantization Error speech_4bit_error.wav
xQ = uniform_midtread_quantizer(x, 2)
e = evaluate_requantization(x, xQ)
sf.write("speech_2bit.wav", xQ, fs)
sf.write("speech_2bit_error.wav", e, fs)
SNR: 2.4 dB
Requantized Signal speech_2bit.wav
Quantization Error speech_2bit_error.wav
Copyright
This notebook is provided as Open Educational Resource. Feel free to use the notebook for your own purposes. The text is licensed under Creative Commons Attribution 4.0, the code of the IPython examples under the MIT license. Please attribute the work as follows: Sascha Spors, Digital Signal Processing - Lecture notes featuring computational examples.