Quantize - Maple Help
For the best experience, we recommend viewing online help using Google Chrome or Microsoft Edge.

Online Help

All Products    Maple    MapleSim


SignalProcessing

  

Quantize

  

perform analogue-to-digital conversion on a signal

 

Calling Sequence

Parameters

Options

Description

Examples

Compatibility

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 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

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 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.

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 Ckx 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+1Ck and Ck+1 otherwise, where φ is the threshold which satisfies 0<φ and φ<1. For the boundary cases, if xC1 then y=C1, and if C−1x 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

δ=2bm

  

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

α&comma;α+δ&comma;..&comma;βδ&comma;β

  

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=minmaxx&comma;α&comma;β

• 

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:

α&comma;β=b+δ2&comma;bδ2m::evenb&comma;bδotherwise

• 

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

z=δyδ+12

  

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:

α&comma;β=b&comma;bδm::evenb+δ2&comma;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=20log10m

  

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

withImageTools&colon;

withSignalProcessing&colon;

Example 1

• 

Quantization can be performed in-place:

UVectorrow2.95199901137063&comma;2.65516788149002&comma;1.18441542906992&comma;0.612556403436018&comma;4.65553919497091&comma;4.50222048838355&comma;1.82900519939139&comma;1.94828622975817&comma;3.23457828327293&comma;4.02868218764152&comma;datatype=float8

U

(5.1.1)

QuantizeU&comma;midtread&comma;5&comma;16&comma;inplace

(5.1.2)

U=U

U=

(5.1.3)

Example 2

XGenerateSignalt&comma;t=1..1&comma;25

b1&colon;

c5&colon;

QuantizeX&comma;midriser&comma;b&comma;c&comma;output=plot&comma;emphasizecodes

QuantizeX&comma;midtread&comma;b&comma;c&comma;output=plot&comma;emphasizecodes

Example 3

XArray1..10&comma;0.01&comma;0.04&comma;0.09&comma;0.16&comma;0.25&comma;0.36&comma;0.49&comma;0.64&comma;0.81&comma;1.00

X

(5.3.1)

CArray1..4&comma;0.&comma;0.50&comma;0.75&comma;1.00

C

(5.3.2)

QQuantizeX&comma;C&comma;truncation=nearest

Q

(5.3.3)

QuantizeX&comma;C&comma;truncation=nearest&comma;output=plot&comma;originalsignalplotoptions=color=blue&comma;quantizedsignalplotoptions=color=firebrick&comma;emphasizecodes

Example 4

• 

Three different truncation methods are available for custom codebooks:

SignalGenerateSignal2cost&comma;t=π..π&comma;50

CodebookVectorseq2..2&comma;0.5&comma;datatype=float8

Codebook

(5.4.1)

QuantizeSignal&comma;Codebook&comma;uniform&comma;truncation=below&comma;timerange=π..π&comma;output=plot

QuantizeSignal&comma;Codebook&comma;uniform&comma;truncation=above&comma;timerange=π..π&comma;output=plot

QuantizeSignal&comma;Codebook&comma;uniform&comma;truncation=nearest&comma;timerange=π..π&comma;output=plot

Example 5

• 

The different outputs can be collected and returned as a single record:

expr2.0sint+0.5cos3t&colon;

τ10&colon;

τ24π&colon;

size50&colon;

DataGenerateSignalexpr&comma;t=τ1..τ2&comma;size

bound3.0&colon;

bits3&colon;

codes2bits

codes8

(5.5.1)

R1QuantizeData&comma;midriser&comma;bound&comma;codes&comma;timerange=τ1..τ2&comma;output=record&colon;

R2QuantizeData&comma;midtread&comma;bound&comma;codes&comma;timerange=τ1..τ2&comma;output=record&colon;

R1plot

R1distortion

0.221138053629368142

(5.5.2)

R1sqnr

18.0617997398388717

(5.5.3)

R2plot

R2distortion

0.215716733331178828

(5.5.4)

R2sqnr

18.0617997398388717

(5.5.5)

Example 6

• 

Multi-dimensional rtables are also supported:

XArrayTools:-ReshapeGenerateSignalcos2πt2&comma;t=0..2&comma;300&comma;150&comma;2

Cseq1..1&comma;0.5

C

(5.6.1)

QuantizeX&comma;C&comma;uniform&comma;truncation=nearest&comma;timerange=0..2&comma;output=plot

Example 7

• 

A uniform codebook can be specified by the range and number of codes:

XGenerateSignal20t2expt&comma;t=0..10&comma;25

QuantizeX&comma;0..15&comma;8&comma;truncation=nearest&comma;timerange=0..10&comma;output=plot&comma;emphasizecodes

Example 8

• 

Consider the following grayscale image:

AArrayRGBtoGraySampleImage5&comma;order=Fortran_order&colon;

EmbedA

• 

By using a codebook with only two values, 0 and 1, we can convert the image to black and white:

B1QuantizeA&comma;0&comma;1&comma;truncation=nearest&colon;

EmbedB1

• 

The threshold can control which values are lowered to 0 and which values are raised to 1:

B2QuantizeA&comma;0&comma;1&comma;truncation=nearest&comma;threshold=0.4&colon;

EmbedB2

B3QuantizeA&comma;0&comma;1&comma;truncation=nearest&comma;threshold=0.6&colon;

EmbedB3

Compatibility

• 

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

SignalProcessing[GenerateSignal]