Quantization of a Sine Wave
3-Bit Sine Sampling¶
The following example quantizes a sine wave with 3 bit. This bit-depth is too low to result in acceptable results, but it is well suited for demonstrating the effects that occur during quantization. The sine wave has an amplitude of $1$, a frequency of $f=200 \mathrm{Hz}$ at a sampling rate of $f=48 \mathrm{kHz}$:
Quantized sine wave at 16 kHz:
At 3 bit, the quantized sine wave is heavily distorted. The distortions are apparantly harmonic.
Quantization Error¶
The quantization error $e$ is the difference between the input signal and the quantized signal:
$$ e = x-x* $$
Spectrum¶
The magintude spectrum clearly shows the harmonic nature of the distortion, with peaks at integer multiples of the input signal's frequency $f_0=200$.
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[8], line 8 5 f = np.linspace(0,fs/2,int(N/2)) 7 plt.plot(f,abs(E),'gray') ----> 8 plt.plot(f,abs(X),'k') 10 ax.set_yscale('log') 11 ax.set_ylim([0.01,10e5]) File /media/anwaldt/ANWALDT_DATA/WORK/TEACHING/Online/RingBuffer/py-env/lib/python3.12/site-packages/matplotlib/pyplot.py:3794, in plot(scalex, scaley, data, *args, **kwargs) 3786 @_copy_docstring_and_deprecators(Axes.plot) 3787 def plot( 3788 *args: float | ArrayLike | str, (...) 3792 **kwargs, 3793 ) -> list[Line2D]: -> 3794 return gca().plot( 3795 *args, 3796 scalex=scalex, 3797 scaley=scaley, 3798 **({"data": data} if data is not None else {}), 3799 **kwargs, 3800 ) File /media/anwaldt/ANWALDT_DATA/WORK/TEACHING/Online/RingBuffer/py-env/lib/python3.12/site-packages/matplotlib/axes/_axes.py:1779, in Axes.plot(self, scalex, scaley, data, *args, **kwargs) 1536 """ 1537 Plot y versus x as lines and/or markers. 1538 (...) 1776 (``'green'``) or hex strings (``'#008000'``). 1777 """ 1778 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D) -> 1779 lines = [*self._get_lines(self, *args, data=data, **kwargs)] 1780 for line in lines: 1781 self.add_line(line) File /media/anwaldt/ANWALDT_DATA/WORK/TEACHING/Online/RingBuffer/py-env/lib/python3.12/site-packages/matplotlib/axes/_base.py:296, in _process_plot_var_args.__call__(self, axes, data, *args, **kwargs) 294 this += args[0], 295 args = args[1:] --> 296 yield from self._plot_args( 297 axes, this, kwargs, ambiguous_fmt_datakey=ambiguous_fmt_datakey) File /media/anwaldt/ANWALDT_DATA/WORK/TEACHING/Online/RingBuffer/py-env/lib/python3.12/site-packages/matplotlib/axes/_base.py:486, in _process_plot_var_args._plot_args(self, axes, tup, kwargs, return_kwargs, ambiguous_fmt_datakey) 483 axes.yaxis.update_units(y) 485 if x.shape[0] != y.shape[0]: --> 486 raise ValueError(f"x and y must have same first dimension, but " 487 f"have shapes {x.shape} and {y.shape}") 488 if x.ndim > 2 or y.ndim > 2: 489 raise ValueError(f"x and y can be no greater than 2D, but have " 490 f"shapes {x.shape} and {y.shape}") ValueError: x and y must have same first dimension, but have shapes (48000,) and (101,)