SignalProcessing
Quantize
perform analogue-to-digital conversion on a signal
Calling Sequence
Parameters
Options
Description
Examples
Compatibility
Quantize( signal, codebook, options )
Quantize( signal, coderange, numcodes, options )
Quantize( signal, quantizer, bound, numcodes, options )
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 c1..c2, where c1 and 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
inplace: Keyword true or false. Specifies whether the quantization is to be performed in-place. The default is false.
timerange: Range of the form t1..t2, where t1 and 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.
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 Ck≤x and x<Ck+1 for some index k, where C is the codebook. If truncation=below, then the quantized value is y=Ck. Similarly, if truncation=above, then y=Ck+1. When truncation=nearest, then y is Ck when y<Ck+φ⁢Ck+1−Ck and Ck+1 otherwise, where φ is the threshold which satisfies 0<φ and φ<1. For the boundary cases, if x≤C1 then y=C1, and if C−1≤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
δ=2⁢bm
is used, where b is bound and m is numcodes, and the codebook is of the form
α,α+δ,..,β−δ,β
where α and β, 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=min⁡max⁡x,α,β
When the quantizer is midriser, the data value y is quantized as follows:
z=δ⁢yδ+12
The offset term acts as a vertical translation so that there is a jump in quantized value from −δ to δ 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:
α,β=−b+δ2,b−δ2m::even−b,b−δotherwise
When the quantizer is midtread, the data value y is quantized as follows:
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:
α,β=−b,b−δm::even−b+δ2,b−δ2otherwise
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
sqnr=20⁢log10⁡m
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.
with⁡ImageTools:
with⁡SignalProcessing:
Example 1
Quantization can be performed in-place:
U≔Vectorrow⁡2.95199901137063,2.65516788149002,−1.18441542906992,−0.612556403436018,−4.65553919497091,4.50222048838355,−1.82900519939139,1.94828622975817,3.23457828327293,−4.02868218764152,datatype=float8
U≔
Quantize⁡U,midtread,5,16,inplace
U=U
U=
Example 2
X≔GenerateSignal⁡t,t=−1..1,25
b≔1:
c≔5:
Quantize⁡X,midriser,b,c,output=plot,emphasizecodes
Quantize⁡X,midtread,b,c,output=plot,emphasizecodes
Example 3
X≔Array⁡1..10,0.01,0.04,0.09,0.16,0.25,0.36,0.49,0.64,0.81,1.00
X≔
C≔Array⁡1..4,0.,0.50,0.75,1.00
C≔
Q≔Quantize⁡X,C,truncation=nearest
Q≔
Quantize⁡X,C,truncation=nearest,output=plot,originalsignalplotoptions=color=blue,quantizedsignalplotoptions=color=firebrick,emphasizecodes
Example 4
Three different truncation methods are available for custom codebooks:
Signal≔GenerateSignal⁡2⁢cos⁡t,t=−π..π,50
Codebook≔Vector⁡seq⁡−2..2,0.5,datatype=float8
Codebook≔
Quantize⁡Signal,Codebook,uniform,truncation=below,timerange=−π..π,output=plot
Quantize⁡Signal,Codebook,uniform,truncation=above,timerange=−π..π,output=plot
Quantize⁡Signal,Codebook,uniform,truncation=nearest,timerange=−π..π,output=plot
Example 5
The different outputs can be collected and returned as a single record:
expr≔2.0⁢sin⁡t+0.5⁢cos⁡3⁢t:
τ1≔0:
τ2≔4⁢π:
size≔50:
Data≔GenerateSignal⁡expr,t=τ1..τ2,size
bound≔3.0:
bits≔3:
codes≔2bits
codes≔8
R1≔Quantize⁡Data,midriser,bound,codes,timerange=τ1..τ2,output=record:
R2≔Quantize⁡Data,midtread,bound,codes,timerange=τ1..τ2,output=record:
R1plot
R1distortion
0.221138053629368142
R1sqnr
18.0617997398388717
R2plot
R2distortion
0.215716733331178828
R2sqnr
Example 6
Multi-dimensional rtables are also supported:
X≔ArrayTools:-Reshape⁡GenerateSignal⁡cos⁡2⁢π⁢t2,t=0..2,300,150,2
C≔seq⁡−1..1,0.5
Quantize⁡X,C,uniform,truncation=nearest,timerange=0..2,output=plot
Example 7
A uniform codebook can be specified by the range and number of codes:
X≔GenerateSignal⁡20⁢t2⁢exp⁡−t,t=0..10,25
Quantize⁡X,0..15,8,truncation=nearest,timerange=0..10,output=plot,emphasizecodes
Example 8
Consider the following grayscale image:
A≔Array⁡RGBtoGray⁡SampleImage⁡5,order=Fortran_order:
Embed⁡A
By using a codebook with only two values, 0 and 1, we can convert the image to black and white:
B1≔Quantize⁡A,0,1,truncation=nearest:
Embed⁡B1
The threshold can control which values are lowered to 0 and which values are raised to 1:
B2≔Quantize⁡A,0,1,truncation=nearest,threshold=0.4:
Embed⁡B2
B3≔Quantize⁡A,0,1,truncation=nearest,threshold=0.6:
Embed⁡B3
The SignalProcessing[Quantize] command was introduced in Maple 2023.
For more information on Maple 2023 changes, see Updates in Maple 2023.
See Also
ImageTools
SignalProcessing[GenerateSignal]
Download Help Document