Skip to content

Commit 0f17780

Browse files
committed
chapter 6. checked
1 parent 235c1c3 commit 0f17780

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

quantization/quantization.ipynb

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"- lecture: https://github.com/spatialaudio/digital-signal-processing-lecture\n",
2020
"- tutorial: https://github.com/spatialaudio/digital-signal-processing-exercises\n",
2121
"\n",
22-
"Feel free to contact lecturer [email protected]"
22+
"Feel free to contact the lecturer [email protected]"
2323
]
2424
},
2525
{
@@ -50,13 +50,13 @@
5050
"outputs": [],
5151
"source": [
5252
"# most common used packages for DSP, have a look into other scipy submodules\n",
53-
"import numpy as np\n",
5453
"import matplotlib as mpl\n",
5554
"import matplotlib.pyplot as plt\n",
56-
"from scipy import signal\n",
55+
"import numpy as np\n",
5756
"\n",
5857
"# audio write and play stuff\n",
5958
"import soundfile as sf # requires 'pip install soundfile'\n",
59+
"from scipy import signal\n",
6060
"\n",
6161
"# last tested with soundfile-0.10.3"
6262
]
@@ -168,16 +168,16 @@
168168
"\n",
169169
"Quantization generates signals that have discrete values $x_q[k]$, $x_q(t)$ from signals with continuous values $x[k]$, $x(t)$. \n",
170170
"\n",
171-
"For quantization, the signals can be both, discrete and continuous in time.\n",
171+
"For quantization, the signals can be both discrete and continuous in time.\n",
172172
"However, a signal that is discrete in time **and** discrete in value is termed a **digital** signal.\n",
173173
"Only digital signals can be processed by computers.\n",
174-
"Here the quantization of discrete-time signals is treated due to practical importance.\n",
174+
"Here, the quantization of discrete-time signals is treated due to practical importance.\n",
175175
"\n",
176176
"To describe quantization analytically, the model in the figure below is used.\n",
177177
"\n",
178178
"<img src=\"QuantizationModel.png\" alt=\"QuantizationModel.png\" width=\"400\"/>\n",
179179
"\n",
180-
"The input and output signal differ by the so called quantization error (quantization noise) $e[k]$, that is defined as\n",
180+
"The input and output signal differ by the so called quantization error (quantization noise) $e[k]$, which is defined as\n",
181181
"\n",
182182
"\\begin{equation}\n",
183183
"e[k] = x_q[k] - x[k],\n",
@@ -194,7 +194,7 @@
194194
"cell_type": "markdown",
195195
"metadata": {},
196196
"source": [
197-
"To use this error model, some assumption have to be made.\n",
197+
"To use this error model, some assumptions have to be made.\n",
198198
"The quantization noise shall be uniformly distributed, which then can be modeled with the probability density function (PDF) $p_e(\\theta) = \\frac{1}{\\Delta Q} \\mathrm{rect}(\\frac{\\theta_e}{\\Delta Q})$, where $\\Delta Q$ denotes the quantization step size and $\\theta_e$ the amplitudes of the quantization error signal.\n",
199199
"This PDF is shown below."
200200
]
@@ -289,11 +289,11 @@
289289
"\n",
290290
"However, this is not observable in this example!\n",
291291
"\n",
292-
"Instead, from the above plot, we can deduce that $e[k]$ is correlated to itself, i.e. it exhibits periodicity each 100 samples in phase, and each 50 sample out of phase.\n",
293-
"The sine period is precisely 100 samples, thus the input signal and the quantization error are somewhat linked and not independent.\n",
292+
"Instead, from the above plot, we can deduce that $e[k]$ is correlated to itself, i.e., it exhibits periodicity every 100 samples in phase, and every 50 samples out of phase.\n",
293+
"The sine period is precisely 100 samples; thus, the input signal and the quantization error are somewhat linked and not independent.\n",
294294
"Thus, the error model assumption is violated. That is bad, since the sine signal allows for otherwise comparable simple analytical calculus.\n",
295295
"\n",
296-
"The links between the signals can be furthermore confirmed with the help of the cross-correlation functions.\n",
296+
"The links between the signals can be further confirmed with the help of the cross-correlation functions.\n",
297297
"Their oscillating characteristics reveal that quantization error is highly correlated. "
298298
]
299299
},
@@ -328,14 +328,14 @@
328328
"cell_type": "markdown",
329329
"metadata": {},
330330
"source": [
331-
"Therefore, the special case of sine signals is in fact not suited for the quantization model above.\n",
332-
"Because of the simplicity of the involved calculation it is common practice to conduct this analysis for sine signals nevertheless, and signal-to-noise ratios in the data sheets of A/D converters are mostly stated for excitation with sine signals.\n",
333-
"For random signals, the quantization model is only valid for high levels in the quantizer. For more information see\n",
331+
"Therefore, the special case of sine signals is, in fact, not suited for the quantization model above.\n",
332+
"Because of the simplicity of the involved calculation, it is common practice to conduct this analysis for sine signals; nevertheless, signal-to-noise ratios in the data sheets of A/D converters are mostly stated for excitation with sine signals.\n",
333+
"For random signals, the quantization model is only valid for high levels in the quantizer. For more information, see\n",
334334
"[Udo Zölzer, Digital Audio Signal Processing, Wiley](https://onlinelibrary.wiley.com/doi/book/10.1002/9780470680018)\n",
335335
"(might be available as free access in your uni network)\n",
336336
"\n",
337337
"- Task:\n",
338-
"Increase the (odd) number of quantization steps $Q$ and check what happens with the shape and amplitudes of the correlations functions. Hint: closer look to the amplitudes of the correlation signals."
338+
"Increase the (odd) number of quantization steps $Q$ and check what happens with the shape and amplitudes of the correlation functions. Hint: a closer look at the amplitudes of the correlation signals."
339339
]
340340
},
341341
{
@@ -375,10 +375,10 @@
375375
"\n",
376376
"where for $\\lfloor \\cdot \\rfloor$ a rounding operation might used and $\\backslash$ denotes integer division.\n",
377377
"So the mapping functions $g$ and $f$ are simple multiplications.\n",
378-
"At the beginning of this notebook, the function `my_quant` is implemented that realizes quantization based on this mapping.\n",
378+
"At the beginning of this notebook, the function `my_quant` is implemented, which realizes quantization based on this mapping.\n",
379379
"The approach uses `numpy`'s `round` operation.\n",
380380
"When asking for rounding, care has to be taken, which [approach](https://en.wikipedia.org/wiki/Rounding) shall be used.\n",
381-
"Numpy rounds to the nearest **even** integer in contrast to e.g. Matlab's rounding to nearest integer.\n",
381+
"Numpy rounds to the nearest **even** integer in contrast to e.g., Matlab's rounding to nearest integer.\n",
382382
"\n",
383383
"Detailed analysis for `my_quant`:\n",
384384
"\n",
@@ -396,7 +396,7 @@
396396
"\n",
397397
"The case of **even** $Q$ is practically used for virtually all analog/digital (AD) and digital/analog (DA) converters.\n",
398398
"\n",
399-
"When additionally to the above statements\n",
399+
"When addition to the above statements\n",
400400
"\n",
401401
"\\begin{equation}\n",
402402
"\\log_2(Q)\\in\\mathbb{N}\n",
@@ -468,17 +468,17 @@
468468
"source": [
469469
"### Plotting the Midtread Curve\n",
470470
"\n",
471-
"We now can visualize the characteristic curve for a simple, made up input signal, i.e. a monotonic increasing signal between $x_{max} = -x_{min}$ using an equidistant increment $\\Delta x$ over sample index $k$.\n",
471+
"We can now visualize the characteristic curve for a simple, made-up input signal, i.e., a monotonic increasing signal between $x_{max} = -x_{min}$ using an equidistant increment $\\Delta x$ over sample index $k$.\n",
472472
"\n",
473473
"Here, we use $x_{max} = 1.25$ and $\\Delta x=0.001$ and assume that we start with $x_{min} = -1.25$ at $k=0$.\n",
474-
"If $\\Delta x$ is sufficiently small, the signal's amplitude can be interpreted as continuous straight line.\n",
474+
"If $\\Delta x$ is sufficiently small, the signal's amplitude can be interpreted as a continuous straight line.\n",
475475
"This straight line is degraded in a quantization process.\n",
476-
"Plotting the quantization result over the input, results in the characteristic curve, in our example in the curve of the uniform saturated midtread quantizer.\n",
476+
"Plotting the quantization result over the input results in the characteristic curve, in our example, the curve of the uniform saturated midtread quantizer.\n",
477477
"\n",
478478
"Let us plot this.\n",
479479
"\n",
480480
"**Please note:**\n",
481-
"The quantizer `uniform_midtread_quantizer` known from lecture and `my_quant` yield the same results besides a slight detail: `uniform_midtread_quantizer` always exhibits an **even** number of quantization steps $Q$.\n",
481+
"The quantizer `uniform_midtread_quantizer` known from the lecture and `my_quant` yield the same results, besides a slight detail: `uniform_midtread_quantizer` always exhibits an **even** number of quantization steps $Q$.\n",
482482
"So, only for even $Q$ results are exactly identical.\n",
483483
"\n",
484484
"We might verify this in the next plots as well."
@@ -532,14 +532,14 @@
532532
"cell_type": "markdown",
533533
"metadata": {},
534534
"source": [
535-
"The following exercises used to be a homework assignment as exam's prerequisite.\n",
535+
"The following exercises used to be a homework assignment as an exam's prerequisite.\n",
536536
"\n",
537537
"# Exercise 1: Uniform Saturated Midtread Characteristic Curve of Quantization\n",
538538
"\n",
539539
"## Task\n",
540540
"\n",
541541
"Check this quantizer curve for $Q=7$ and $Q=8$.\n",
542-
"Make sure that you get the idea of the midtread concept (the zero is always quantized to zero) and saturation (for even $Q$) largest quantization step is saturated)."
542+
"Make sure that you get the idea of the midtread concept (the zero is always quantized to zero) and saturation (for even $Q$) the largest quantization step is saturated)."
543543
]
544544
},
545545
{
@@ -607,18 +607,18 @@
607607
"source": [
608608
"# Exercise 2: Quantization and Signal-to-Noise Ratio\n",
609609
"\n",
610-
"From theory the **6dB / Bit rule of thumb** is well known for uniform quantization. It states that the signal-to-noise ratio increases by 6 dB for every additional bit that is spent to quantize the input data.\n",
610+
"From theory, the **6dB / Bit rule of thumb** is well known for uniform quantization. It states that the signal-to-noise ratio increases by 6 dB for every additional bit that is spent to quantize the input data.\n",
611611
"Hence,\n",
612612
"\n",
613613
"\\begin{equation}\n",
614614
"\\text{SNR in dB} = 6 \\cdot B + \\gamma,\n",
615615
"\\end{equation}\n",
616616
"\n",
617-
"where $\\gamma$ is a offset value in dB that depends on the PDF of the signal to be quantized.\n",
618-
"Note that this rule of thumb assumes that the quantization error exhibits uniform PDF and is not correlated with the quantized signal.\n",
619-
"We can see that this rule of thumb is inaccurate when quantizing a sine signal with small number of bits or an amplitude in the range of the quantization step. Then, the mentioned assumptions are not fulfilled. We will observe this in exercise 3.\n",
617+
"where $\\gamma$ is an offset value in dB that depends on the PDF of the signal to be quantized.\n",
618+
"Note that this rule of thumb assumes that the quantization error exhibits a uniform PDF and is not correlated with the quantized signal.\n",
619+
"We can see that this rule of thumb is inaccurate when quantizing a sine signal with a small number of bits or an amplitude in the range of the quantization step. Then, the mentioned assumptions are not fulfilled. We will observe this in exercise 3.\n",
620620
"\n",
621-
"We plot the function SNR of bits below for uniform, normal and Laplace PDF noises and a sine signal.\n",
621+
"We plot the function SNR of bits below for uniform, normal, and Laplace PDF noises and a sine signal.\n",
622622
"We should observe the slope of always 6 dB per Bit.\n",
623623
"We should note the different absolute values of the SNR depending on the varying $\\gamma$. \n",
624624
"\n",
@@ -785,7 +785,7 @@
785785
"\n",
786786
"This dither signal with small amplitudes aims at de-correlating the quantization error $e[k]$ from the quantized signal $x_q[k]$, which is especially important for small amplitudes of $x[k]$.\n",
787787
"This technique is called **dithering**.\n",
788-
"For $d[k]=0$ no dithering is applied.\n",
788+
"For $d[k]=0$, no dithering is applied.\n",
789789
"\n",
790790
"Since the quantization error may be in the range $-\\frac{\\Delta Q}{2}\\leq e[k]\\leq \\frac{\\Delta Q}{2}$ (assuming uniform distribution), it appears reasonable to use a dither noise with a probability density function (PDF) of\n",
791791
"\n",
@@ -795,7 +795,7 @@
795795
"\n",
796796
"i.e. a **zero-mean, uniformly distributed noise** with maximum amplitude $|d[k]|=\\frac{\\Delta Q}{2}$.\n",
797797
"It can be shown that this dither noise improves the quality of the quantized signal.\n",
798-
"However, there is still a noise modulation (i.e. a too high correlation between $x_q[k]$ and $e[k]$) that depends on the amplitude of the input signal.\n",
798+
"However, there is still a noise modulation (i.e., a too high correlation between $x_q[k]$ and $e[k]$) that depends on the amplitude of the input signal.\n",
799799
"\n",
800800
"The noise modulation can be almost completely eliminated with a **zero-mean noise** signal exhibiting a **symmetric triangular PDF**\n",
801801
"\n",
@@ -805,7 +805,7 @@
805805
"\n",
806806
"with maximum amplitude $|d[k]|=Q$.\n",
807807
"By doing so, an almost ideal decorrelation between $x_q[k]$ and $e[k]$ is realized.\n",
808-
"In audio, this technique is called TPDF-Dithering (Triangular Probability Density Function Dithering) and can be applied in the mastering process of audio material that is to be distributed e.g. on a CD or via streaming."
808+
"In audio, this technique is called TPDF-Dithering (Triangular Probability Density Function Dithering) and can be applied in the mastering process of audio material that is to be distributed, e.g., on a CD or via streaming."
809809
]
810810
},
811811
{
@@ -814,31 +814,31 @@
814814
"source": [
815815
"## Task \n",
816816
"\n",
817-
"To get an impression on how dithering may be implemented and what quantized signals sound like, the following exercises shall be performed.\n",
817+
"To get an impression of how dithering may be implemented and what quantized signals sound like, the following exercises shall be performed.\n",
818818
"\n",
819819
"- Generate the sine signal $x[k]$ defined above.\n",
820820
"\n",
821821
"- Generate the dither noise $d_\\text{RECT}[k]$ according to the PDF $p_\\text{RECT}(d)$. Check the resulting amplitude and distribution carefully. The length of $d_\\text{RECT}[k]$ and $x[k]$ must be equal.\n",
822822
"\n",
823823
"- Generate the dither noise $d_\\text{TRI}[k]$ according to the PDF $p_\\text{TRI}(d)$. Check the resulting amplitude and distribution carefully. The length of $d_\\text{TRI}[k]$ and $x[k]$ must be equal.\n",
824824
"\n",
825-
"- Add each dither noise $d_\\text{RECT}[k]$ and $d_\\text{TRI}[k]$ individually to $x[k]$. Together with the case of no dithering we now have three signals to be quantized.\n",
825+
"- Add each dither noise $d_\\text{RECT}[k]$ and $d_\\text{TRI}[k]$ individually to $x[k]$. Together with the case of no dithering, we now have three signals to be quantized.\n",
826826
"\n",
827827
"- Quantize these signals individually using `my_quant(x,Q)` with $Q$ quantization steps.\n",
828828
"\n",
829829
"- Plot the midtread characteristic curve.\n",
830830
"\n",
831-
"- Plot the histogram of the dither noises as estimate of its PDF.\n",
831+
"- Plot the histogram of the dither noises as an estimate of its PDF.\n",
832832
"\n",
833-
"- Plot the histogram of the error noises as estimate of its PDF.\n",
833+
"- Plot the histogram of the error noises as an estimate of its PDF.\n",
834834
"\n",
835-
"- Plot the sine signal, the dithered signal, the quantized signal and the quantization error signal in one diagram for all three cases.\n",
835+
"- Plot the sine signal, the dithered signal, the quantized signal, and the quantization error signal in one diagram for all three cases.\n",
836836
"\n",
837837
"- Calculate and plot the CCF of the signals $x_q[k]$ and $e[k]$ for all three cases.\n",
838838
"\n",
839839
"- Interpret the obtained graphics.\n",
840840
"\n",
841-
"- For each case, render WAV files from $x[k]$, $x[k]+d[k]$, $x_q[k]$ und $e[k]$ and listen to them. **Be careful! Do not harm your ears!** Pay special attention to the sound of the quantization error, how it is correlated with the quantized signal and how loud it appears.\n",
841+
"- For each case, render WAV files from $x[k]$, $x[k]+d[k]$, $x_q[k]$ und $e[k]$ and listen to them. **Be careful! Do not harm your ears!** Pay special attention to the sound of the quantization error, how it is correlated with the quantized signal, and how loud it appears.\n",
842842
"\n",
843843
"- Consider the 5 cases\n",
844844
"\n",
@@ -848,12 +848,12 @@
848848
" 4. $B=3$ Bit, $A=\\Delta Q$\n",
849849
" 5. $B=3$ Bit, $A=\\frac{\\Delta Q}{2}$\n",
850850
"\n",
851-
"In the last case the signal has amplitude even below the quantization step size $\\Delta Q$. You might verify by listening that the sine is still perceivable if dithering is applied, but not if no dithering is applied.\n",
851+
"In the last case, the signal has amplitude even below the quantization step size $\\Delta Q$. You might verify by listening that the sine is still perceivable if dithering is applied, but not if no dithering is applied.\n",
852852
"\n",
853853
"**Again: Be careful! Do not harm your ears!**\n",
854-
"The signal amplitude $A$ and chosen $B$ is directly related to the playback level!\n",
854+
"The signal amplitude $A$ and the chosen $B$ are directly related to the playback level!\n",
855855
"\n",
856-
"**Warning again: start with very very low playback level, find the loudest signal first and then increase volume to your convenience**"
856+
"**Warning again: start with very, very low playback level, find the loudest signal first, and then increase volume to your convenience**"
857857
]
858858
},
859859
{
@@ -1174,7 +1174,7 @@
11741174
"name": "python",
11751175
"nbconvert_exporter": "python",
11761176
"pygments_lexer": "ipython3",
1177-
"version": "3.10.12"
1177+
"version": "3.12.2"
11781178
}
11791179
},
11801180
"nbformat": 4,

0 commit comments

Comments
 (0)