Skip to content

komm.ScalarQuantizer

General scalar quantizer. It is defined by a list of levels, $y_0, y_1, \ldots, y_{L-1}$, and a list of thresholds, $\lambda_0, \lambda_1, \ldots, \lambda_L$, satisfying $$ -\infty = \lambda_0 < y_0 < \lambda_1 < y_1 < \cdots < \lambda_{L - 1} < y_{L - 1} < \lambda_L = +\infty. $$ Given an input $x \in \mathbb{R}$, the output of the quantizer is given by $y = y_i$ if and only if $\lambda_i \leq x < \lambda_{i+1}$, where $i \in [0:L)$. For more details, see Say06, Ch. 9.

Parameters:

  • levels (ArrayLike)

    The quantizer levels $y_0, y_1, \ldots, y_{L-1}$. It should be a list floats of length $L$.

  • thresholds (ArrayLike)

    The quantizer finite thresholds $\lambda_1, \lambda_2, \ldots, \lambda_{L-1}$. It should be a list of floats of length $L - 1$.

Examples:

The $5$-level scalar quantizer whose characteristic (input × output) curve is depicted in the figure below has levels $$ y_0 = -2, ~ y_1 = -1, ~ y_2 = 0, ~ y_3 = 1, ~ y_4 = 2, $$ and thresholds $$ \lambda_0 = -\infty, ~ \lambda_1 = -1.5, ~ \lambda_2 = -0.3, ~ \lambda_3 = 0.8, ~ \lambda_4 = 1.4, ~ \lambda_5 = \infty. $$

Scalar quantizer example.

>>> quantizer = komm.ScalarQuantizer(
...     levels=[-2.0, -1.0, 0.0, 1.0, 2.0],
...     thresholds=[-1.5, -0.3, 0.8, 1.4],
... )

levels NDArray[floating] cached property

The quantizer levels $y_0, y_1, \ldots, y_{L-1}$.

Examples:

>>> quantizer = komm.ScalarQuantizer(
...     levels=[-2.0, -1.0, 0.0, 1.0, 2.0],
...     thresholds=[-1.5, -0.3, 0.8, 1.4],
... )
>>> quantizer.levels
array([-2., -1.,  0.,  1.,  2.])

thresholds NDArray[floating] cached property

The quantizer finite thresholds $\lambda_1, \lambda_2, \ldots, \lambda_{L-1}$.

Examples:

>>> quantizer = komm.ScalarQuantizer(
...     levels=[-2.0, -1.0, 0.0, 1.0, 2.0],
...     thresholds=[-1.5, -0.3, 0.8, 1.4],
... )
>>> quantizer.thresholds
array([-1.5, -0.3,  0.8,  1.4])

mean_squared_error()

Computes the mean squared (quantization) error (MSE) of the quantizer for a given input pdf. It is defined as $$ \mse = \int_{-\infty}^{\infty} (y - x)^2 f_X(x) \, dx $$ where $y$ is the quantized signal and $f_X(x)$ is the pdf of the input signal.

Parameters:

  • input_pdf (Callable[[NDArray[floating]], NDArray[floating]])

    The pdf $f_X(x)$ of the input signal.

  • input_range (tuple[float, float])

    The range $(x_\mathrm{min}, x_\mathrm{max})$ of the input signal.

  • points_per_interval (int)

    The number of points per interval for numerical integration (default: 4096).

Returns:

  • mse (float)

    The mean square quantization error.

Examples:

>>> quantizer = komm.ScalarQuantizer(
...     levels=[-2.0, -1.0, 0.0, 1.0, 2.0],
...     thresholds=[-1.5, -0.3, 0.8, 1.4],
... )
>>> gaussian_pdf = lambda x: 1/np.sqrt(2*np.pi) * np.exp(-x**2/2)
>>> quantizer.mean_squared_error(
...     input_pdf=gaussian_pdf,
...     input_range=(-5, 5),
... )
0.13598089455499335

digitize()

Returns the quantization indices for the input signal.

Parameters:

  • input (ArrayLike)

    The input signal $x$ to be digitized.

Returns:

  • output (NDArray[integer])

    The integer indices of the quantization levels for each input sample.

Examples:

>>> quantizer = komm.ScalarQuantizer(
...     levels=[-2.0, -1.0, 0.0, 1.0, 2.0],
...     thresholds=[-1.5, -0.3, 0.8, 1.4],
... )
>>> quantizer.digitize([-2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5])
array([0, 0, 1, 1, 1, 2, 2, 3, 4, 4, 4])

quantize()

Quantizes the input signal.

Parameters:

  • input (ArrayLike)

    The input signal $x$ to be quantized.

Returns:

  • output (NDArray[floating])

    The quantized signal $y$.

Examples:

>>> quantizer = komm.ScalarQuantizer(
...     levels=[-2.0, -1.0, 0.0, 1.0, 2.0],
...     thresholds=[-1.5, -0.3, 0.8, 1.4],
... )
>>> quantizer.quantize([-2.5, -2, -1.5, -1, -0.5, 0, 0.5, 1, 1.5, 2, 2.5])
array([-2., -2., -1., -1., -1.,  0.,  0.,  1.,  2.,  2.,  2.])