Quantize - Maple Help

SignalProcessing

 Quantize
 perform analogue-to-digital conversion on a signal

 Calling Sequence Quantize( signal, codebook, options ) Quantize( signal, coderange, numcodes, options ) Quantize( signal, quantizer, bound, numcodes, options )

Parameters

 signal - rtable or list of real-valued data codebook - 1-D rtable or list of two or more strictly increasing real values for the quantization codes coderange - range of the form $\mathrm{c1}..\mathrm{c2}$, where $\mathrm{c1}$ and $\mathrm{c2}$ are numeric values for, respectively, the smallest and largest quantization codes numcodes - positive integer larger than 1 for the number of quantization codes quantizer - keyword midriser or midtread bound - positive real value for the bound on the absolute values of quantization codes

Options

 • inplace: Keyword true or false. Specifies whether the quantization is to be performed in-place. The default is false.
 • timerange: Range of the form $\mathrm{t1}..\mathrm{t2}$, where $\mathrm{t1}$ and $\mathrm{t2}$ are numeric values. Specifies the time range for the plot and Vector of times. The default is 1..numelems(signal).
 • truncation: Keyword above, below, or nearest. Specifies how matches in a custom codebook are determined. The default is nearest.
 • threshold:  Numeric value larger than 0.0 and smaller than 1.0. Specifies, for the custom codebook with truncation=nearest, the distance ratio at which the quantized value above is considered closer than the one below. The default is 0.5.
 • uniform: Keyword true or false. When true and a custom codebook is passed, Maple assumes the codes are evenly spaced and skips the check for uniformity. The default is false.
 • emphasizecodes: Keyword true or false. Specifies whether the quantization codes should be emphasized in the plot using horizontal lines. The default is false.
 • originalsignalplotoptions: List for additional plot options to be passed when creating the plot of the original signal. The default is [].
 • quantizedsignalplotoptions: List for additional plot options to be passed when creating the plot of the quantized signal. The default is [].
 • codebookplotoptions: List for additional plot options to be passed when creating the plot of the codebook. The default is [].
 • plotoptions: List for additional plot options to be passed when creating the combined plot. The default is [].
 • output: The type of output. The supported options are:
 – codebook: Returns a Vector, of float[8] datatype, with the quantization values.
 – distortion: Returns the numeric value for the distortion between the original and quantized signals.
 – indices: Returns an rtable, of integer[4] datatype, with the indices of the codebook that give the respective values in the quantized signal.
 – originalsignal: Returns an rtable, of float[8] datatype, with the original signal.
 – plot: Returns a plot of the original and quantized signals.
 – quantizedsignal: Returns an rtable, of float[8] datatype, with the quantized signal.
 – sqnr: Returns the numeric value for the signal-to-quantized noise ratio.
 – times: Returns an rtable, of float[8] datatype, with the time values determined by timerange.
 – record: Returns a record with the previous options.
 – list of any of the above options: Returns an expression sequence with the corresponding outputs, in the same order. The default is quantizedsignal.

Description

 • The Quantize command takes a signal with real data and computes the associated quantized values for each element based on a codebook.
 • For the quantizer with a custom codebook, suppose data value $x$ satisfies ${C}_{k}\le x$ and $x<{C}_{k+1}$ for some index $k$, where $C$ is the codebook. If truncation=below, then the quantized value is $y={C}_{k}$. Similarly, if truncation=above, then $y={C}_{k+1}$. When truncation=nearest, then $y$ is ${C}_{k}$ when $y<{C}_{k}+\mathrm{\phi }\left({C}_{k+1}-{C}_{k}\right)$ and ${C}_{k+1}$ otherwise, where $\mathrm{\phi }$ is the threshold which satisfies $0<\mathrm{\phi }$ and $\mathrm{\phi }<1$. For the boundary cases, if $x\le {C}_{1}$ then $y={C}_{1}$, and if ${C}_{-1}\le x$ then $y={C}_{-1}$.
 • When the codebook is specified as a range and number of codes, a codebook with uniformly spaced codes is constructed internally.
 • For the midriser and midtread quantizers, a step size of

$\mathrm{\delta }=\frac{2b}{m}$

 is used, where $b$ is bound and $m$ is numcodes, and the codebook is of the form

$\left[\mathrm{\alpha },\mathrm{\alpha }+\mathrm{\delta },..,\mathrm{\beta }-\mathrm{\delta },\mathrm{\beta }\right]$

 where $\mathrm{\alpha }$ and $\mathrm{\beta }$, which are specified below, depend on the type of quantizer and whether the size of the codebook is even or odd. Before applying the quantizer, each data value $x$ is lowered or raised so that it satisfies the bounds, that is, $x$ is replaced with $y$, where:

$y=\mathrm{min}\left(\mathrm{max}\left(x,\mathrm{\alpha }\right),\mathrm{\beta }\right)$

 • When the quantizer is midriser, the data value $y$ is quantized as follows:

$z=\mathrm{\delta }\left(⌊\frac{y}{\mathrm{\delta }}⌋+\frac{1}{2}\right)$

 The offset term acts as a vertical translation so that there is a jump in quantized value from $-\mathrm{\delta }$ to $\mathrm{\delta }$ when a data value passes from negative to positive, hence the name. Consequently, there is no value of 0 in the codebook. For the bounds:

$\left[\mathrm{\alpha },\mathrm{\beta }\right]=\left\{\begin{array}{cc}\left[-b+\frac{\mathrm{\delta }}{2},b-\frac{\mathrm{\delta }}{2}\right]& m::\mathrm{even}\\ \left[-b,b-\mathrm{\delta }\right]& \mathrm{otherwise}\end{array}\right\$

 • When the quantizer is midtread, the data value $y$ is quantized as follows:

$z=\mathrm{\delta }⌊\frac{y}{\mathrm{\delta }}+\frac{1}{2}⌋$

 The offset term acts as a horizontal translation so that a quantized value is zero when a data value passes from negative to positive, hence the name. Consequently, the value of 0 is in the codebook. For the bounds:

$\left[\mathrm{\alpha },\mathrm{\beta }\right]=\left\{\begin{array}{cc}\left[-b,b-\mathrm{\delta }\right]& m::\mathrm{even}\\ \left[-b+\frac{\mathrm{\delta }}{2},b-\frac{\mathrm{\delta }}{2}\right]& \mathrm{otherwise}\end{array}\right\$

 • The quantized signal $Q$, indices $K$, and codebook $C$ satisfy Q=C[K].
 • The distortion is computed as the root mean square (RMS) error between the original and quantized signals.
 • The signal-to-quantization noise ratio (SQNR) is computed as

$\mathrm{sqnr}=20{{\mathrm{log}}}_{10}\left(m\right)$

 where $m$ is the size of the codebook.
 • The bound is also known as the half-scale range. Another convention is to use the full-scale range, which is twice the bound.
 • The signal and codebook rtables cannot have an indexing function and must use Fortran ordering and rectangular storage.
 • The outputs indices, quantizedsignal, and times will have the same dimensions as signal. Internally, these containers are all treated as Vectors, with Fortran ordering determining the order of elements in the respective Vector versions. The plot will display the Vector versions of originalsignal and quantizedsignal.
 • Maple attempts to coerce signal and codebook to an rtable of float[8] datatype, and an error is thrown if this is not possible. For this reason, it is most efficient for the passed containers to be rtables of this datatype.
 • The Quantize command is not thread safe.

Examples

 > $\mathrm{with}\left(\mathrm{ImageTools}\right):$
 > $\mathrm{with}\left(\mathrm{SignalProcessing}\right):$

Example 1

 • Quantization can be performed in-place:
 > $U≔\mathrm{Vector}\left['\mathrm{row}'\right]\left(\left[2.95199901137063,2.65516788149002,-1.18441542906992,-0.612556403436018,-4.65553919497091,4.50222048838355,-1.82900519939139,1.94828622975817,3.23457828327293,-4.02868218764152\right],'\mathrm{datatype}'='\mathrm{float}\left[8\right]'\right)$
 ${U}{≔}\left[\right]$ (5.1.1)
 > $\mathrm{Quantize}\left(U,'\mathrm{midtread}',5,16,'\mathrm{inplace}'\right)$
 $\left[\right]$ (5.1.2)
 > $'U'=U$
 ${U}{=}\left[\right]$ (5.1.3)

Example 2

 > $X≔\mathrm{GenerateSignal}\left(t,t=-1..1,25\right)$
 > $b≔1:$
 > $c≔5:$
 > $\mathrm{Quantize}\left(X,'\mathrm{midriser}',b,c,'\mathrm{output}'='\mathrm{plot}','\mathrm{emphasizecodes}'\right)$
 > $\mathrm{Quantize}\left(X,'\mathrm{midtread}',b,c,'\mathrm{output}'='\mathrm{plot}','\mathrm{emphasizecodes}'\right)$

Example 3

 > $X≔\mathrm{Array}\left(1..10,\left[0.01,0.04,0.09,0.16,0.25,0.36,0.49,0.64,0.81,1.00\right]\right)$
 ${X}{≔}\left[\right]$ (5.3.1)
 > $C≔\mathrm{Array}\left(1..4,\left[0.,0.50,0.75,1.00\right]\right)$
 ${C}{≔}\left[\right]$ (5.3.2)
 > $Q≔\mathrm{Quantize}\left(X,C,'\mathrm{truncation}'='\mathrm{nearest}'\right)$
 ${Q}{≔}\left[\right]$ (5.3.3)
 > $\mathrm{Quantize}\left(X,C,'\mathrm{truncation}'='\mathrm{nearest}','\mathrm{output}'='\mathrm{plot}','\mathrm{originalsignalplotoptions}'=\left['\mathrm{color}'='\mathrm{blue}'\right],'\mathrm{quantizedsignalplotoptions}'=\left['\mathrm{color}'='\mathrm{firebrick}'\right],'\mathrm{emphasizecodes}'\right)$

Example 4

 • Three different truncation methods are available for custom codebooks:
 > $\mathrm{Signal}≔\mathrm{GenerateSignal}\left(2\mathrm{cos}\left(t\right),t=-\mathrm{\pi }..\mathrm{\pi },50\right)$
 > $\mathrm{Codebook}≔\mathrm{Vector}\left(\left[\mathrm{seq}\left(-2..2,0.5\right)\right],'\mathrm{datatype}'='\mathrm{float}\left[8\right]'\right)$
 ${\mathrm{Codebook}}{≔}\left[\right]$ (5.4.1)
 > $\mathrm{Quantize}\left(\mathrm{Signal},\mathrm{Codebook},'\mathrm{uniform}','\mathrm{truncation}'='\mathrm{below}','\mathrm{timerange}'=-\mathrm{\pi }..\mathrm{\pi },'\mathrm{output}'='\mathrm{plot}'\right)$
 > $\mathrm{Quantize}\left(\mathrm{Signal},\mathrm{Codebook},'\mathrm{uniform}','\mathrm{truncation}'='\mathrm{above}','\mathrm{timerange}'=-\mathrm{\pi }..\mathrm{\pi },'\mathrm{output}'='\mathrm{plot}'\right)$
 > $\mathrm{Quantize}\left(\mathrm{Signal},\mathrm{Codebook},'\mathrm{uniform}','\mathrm{truncation}'='\mathrm{nearest}','\mathrm{timerange}'=-\mathrm{\pi }..\mathrm{\pi },'\mathrm{output}'='\mathrm{plot}'\right)$

Example 5

 • The different outputs can be collected and returned as a single record:
 > $\mathrm{expr}≔2.0\mathrm{sin}\left(t\right)+0.5\mathrm{cos}\left(3t\right):$
 > $\mathrm{\tau }\left[1\right]≔0:$
 > $\mathrm{\tau }\left[2\right]≔4\mathrm{\pi }:$
 > $\mathrm{size}≔50:$
 > $\mathrm{Data}≔\mathrm{GenerateSignal}\left(\mathrm{expr},t=\mathrm{\tau }\left[1\right]..\mathrm{\tau }\left[2\right],\mathrm{size}\right)$
 > $\mathrm{bound}≔3.0:$
 > $\mathrm{bits}≔3:$
 > $\mathrm{codes}≔{2}^{\mathrm{bits}}$
 ${\mathrm{codes}}{≔}{8}$ (5.5.1)
 > $\mathrm{R1}≔\mathrm{Quantize}\left(\mathrm{Data},'\mathrm{midriser}',\mathrm{bound},\mathrm{codes},'\mathrm{timerange}'=\mathrm{\tau }\left[1\right]..\mathrm{\tau }\left[2\right],'\mathrm{output}'='\mathrm{record}'\right):$
 > $\mathrm{R2}≔\mathrm{Quantize}\left(\mathrm{Data},'\mathrm{midtread}',\mathrm{bound},\mathrm{codes},'\mathrm{timerange}'=\mathrm{\tau }\left[1\right]..\mathrm{\tau }\left[2\right],'\mathrm{output}'='\mathrm{record}'\right):$
 > $\mathrm{R1}\left['\mathrm{plot}'\right]$
 > $\mathrm{R1}\left['\mathrm{distortion}'\right]$
 ${0.221138053629368142}$ (5.5.2)
 > $\mathrm{R1}\left['\mathrm{sqnr}'\right]$
 ${18.0617997398388717}$ (5.5.3)
 > $\mathrm{R2}\left['\mathrm{plot}'\right]$
 > $\mathrm{R2}\left['\mathrm{distortion}'\right]$
 ${0.215716733331178828}$ (5.5.4)
 > $\mathrm{R2}\left['\mathrm{sqnr}'\right]$
 ${18.0617997398388717}$ (5.5.5)

Example 6

 • Multi-dimensional rtables are also supported:
 > $X≔\mathrm{ArrayTools}:-\mathrm{Reshape}\left(\mathrm{GenerateSignal}\left(\mathrm{cos}\left(2\mathrm{\pi }{t}^{2}\right),t=0..2,300\right),\left[150,2\right]\right)$
 > $C≔⟨\mathrm{seq}\left(-1..1,0.5\right)⟩$
 ${C}{≔}\left[\right]$ (5.6.1)
 > $\mathrm{Quantize}\left(X,C,'\mathrm{uniform}','\mathrm{truncation}'='\mathrm{nearest}','\mathrm{timerange}'=0..2,'\mathrm{output}'='\mathrm{plot}'\right)$

Example 7

 • A uniform codebook can be specified by the range and number of codes:
 > $X≔\mathrm{GenerateSignal}\left(20{t}^{2}\mathrm{exp}\left(-t\right),t=0..10,25\right)$
 > $\mathrm{Quantize}\left(X,0..15,8,'\mathrm{truncation}'='\mathrm{nearest}','\mathrm{timerange}'=0..10,'\mathrm{output}'='\mathrm{plot}','\mathrm{emphasizecodes}'\right)$

Example 8

 • Consider the following grayscale image:
 > $A≔\mathrm{Array}\left(\mathrm{RGBtoGray}\left(\mathrm{SampleImage}\left(5\right)\right),'\mathrm{order}'='\mathrm{Fortran_order}'\right):$
 > $\mathrm{Embed}\left(A\right)$

 • By using a codebook with only two values, 0 and 1, we can convert the image to black and white:
 > $\mathrm{B1}≔\mathrm{Quantize}\left(A,\left[0,1\right],'\mathrm{truncation}'='\mathrm{nearest}'\right):$
 > $\mathrm{Embed}\left(\mathrm{B1}\right)$

 • The threshold can control which values are lowered to 0 and which values are raised to 1:
 > $\mathrm{B2}≔\mathrm{Quantize}\left(A,\left[0,1\right],'\mathrm{truncation}'='\mathrm{nearest}','\mathrm{threshold}'=0.4\right):$
 > $\mathrm{Embed}\left(\mathrm{B2}\right)$

 > $\mathrm{B3}≔\mathrm{Quantize}\left(A,\left[0,1\right],'\mathrm{truncation}'='\mathrm{nearest}','\mathrm{threshold}'=0.6\right):$
 > $\mathrm{Embed}\left(\mathrm{B3}\right)$

Compatibility

 • The SignalProcessing[Quantize] command was introduced in Maple 2023.