# fixed.Quantizer

Quantize fixed-point numbers

## Syntax

`q = fixed.Quantizerq = fixed.Quantizer(nt,rm,oa)q = fixed.Quantizer(s,wl,fl,rm,oa)q = fixed.Quantizer(Name,Value)`

## Description

`q = fixed.Quantizer` creates a quantizer `q` that quantizes fixed-point (`fi`) numbers using default fixed-point settings.

`q = fixed.Quantizer(nt,rm,oa)` uses the `numerictype` (`nt`) object information and the `RoundingMethod` (`rm`) and `OverflowAction` (`oa`) properties.

The `numerictype`, rounding method, and overflow action apply only during the quantization. The resulting, quantized `q` does not have any fimath attached to it.

`q = fixed.Quantizer(s,wl,fl,rm,oa)` uses the `Signed` (`s`), `WordLength` (`wl`), `FractionLength` (`fl`), `RoundingMethod` (`rm`), and `OverflowAction` (`oa`) properties.

`q = fixed.Quantizer(Name,Value)` creates a quantizer with the property options specified by one or more `Name,Value` pair arguments. You separate pairs of `Name,Value` arguments with commas. `Name` is the argument name, and `Value` is the corresponding value. `Name` must appear inside single quotes (`''`). You can specify several name-value pair arguments in any order as `Name1,Value1,…,NameN,ValueN`.

## Input Arguments

 `nt` Binary-point, scaled numerictype object or slope-bias scaled, fixed-point numerictype object. If your `fixed.Quantizer` uses a numerictype object that has either a `Signedness` of `Auto` or unspecified `Scaling`, an error occurs. `rm` Rounding method to apply to the output data. Valid rounding methods are: `Ceiling`, `Convergent`, `Floor`, `Nearest`, `Round`, and `Zero`. The associated property name is `RoundingMethod`. Default: `Floor` `oa` Overflow action to take in case of data overflow. Valid overflow actions are `Saturate` and `Wrap`. The associated property name is `OverflowAction`. Default: `Wrap` `s` Logical value, `true` or `false`, indicating whether the output is signed or unsigned, respectively. The associated property name is `Signed`. Default: `true` `wl` Word length (number of bits) of the output data. The associated property name is `WordLength`. Default: `16` `fl` Fraction length of the output data. The associated property name is `FractionLength`. Default: `15`

## Name-Value Pair Arguments

Specify optional comma-separated pairs of `Name,Value` arguments. `Name` is the argument name and `Value` is the corresponding value. `Name` must appear inside single quotes (`' '`). You can specify several name and value pair arguments in any order as `Name1,Value1,...,NameN,ValueN`.

`'Bias'`

The bias is part of the numerical representation used to interpret a fixed-point number. Along with the slope, the bias forms the scaling of the number.

Default: 0

`'FixedExponent'`

Fixed-point exponent associated with the object. The exponent is part of the numerical representation used to express a fixed-point number.

The exponent of a fixed-point number is equal to the negative of the fraction length. `FixedExponent` must be an integer.

Default: -15

`'FractionLength'`

Fraction length of the stored integer value of the object, in bits. The fraction length can be any integer value.

This property automatically defaults to the best precision possible based on the value of the word length and the real-world value of the `fi` object.

Default: 15

`'OverflowAction'`

Action to take in case of data overflow. Valid overflow actions are `Saturate` and `Wrap`. .

Default: `Wrap`

`'RoundingMethod'`

Rounding method to apply to the output data. Valid rounding methods are: `Ceiling`, `Convergent`, `Floor`, `Nearest`, `Round`, and `Zero`.

Default: `Floor`

`'Signed'`

Whether the object is signed. The possible values of this property are:

• `1` — signed

• `0` — unsigned

• `true` — signed

• `false` — unsigned

 Note:   Although the `Signed` property is still supported, the `Signedness` property always appears in the `numerictype` object display. If you choose to change or set the signedness of your `numerictype` object using the `Signed` property, MATLAB® updates the corresponding value of the `Signedness` property.

Default: `true`

`'Signedness'`

Whether the object is signed, unsigned, or has an unspecified sign. The possible values of this property are:

• `Signed` — signed

• `Unsigned` — unsigned

Default: `Signed`

`'Slope'`

Slope associated with the object. The slope is part of the numerical representation used to express a fixed-point number. Along with the bias, the slope forms the scaling of a fixed-point number.

Default: 2^-15

`'SlopeAdjustmentFactor'`

Slope adjustment associated with the object. The slope adjustment is equivalent to the fractional slope of a fixed-point number. The fractional slope is part of the numerical representation used to express a fixed-point number.

`SlopeAdjustmentFactor` must be greater than or equal to 1 and less than 2.

Default: 1

`'WordLength'`

Word length of the stored integer value of the object, in bits. The word length can be any positive integer value.

Default: 16

## Output Arguments

 `q` Quantizer that quantizes fi input numbers

## Examples

Use `fixed.Quantizer` to reduce the word length that results from adding two fixed-point numbers.

```q = fixed.Quantizer; x1 = fi(0.1,1,16,15); x2 = fi(0.8,1,16,15); y = quantize(q,x1+x2); ```

Use `fixed.Quantizer` object to change a binary point scaled fixed-point fi to a slope-bias scaled fixed-point fi

```qsb = fixed.Quantizer(numerictype(1,7,1.6,0.2),... 'Round','Saturate'); ysb = quantize(qsb,fi(pi,1,16,13));```

collapse all

### Fixed-point numbers

Fixed-point numbers can be represented as

`$real\text{-}worldvalue=\left(slope×storedinteger\right)+bias$`

where the slope can be expressed as

`$slope=fractionalslope×{2}^{fixedexponent}$`

### Tips

• Use `y = quantize(q,x)` to quantize input array `x` using the fixed-point settings of quantizer `q`. `x` can be any fixed-point number fi, except a Boolean value. If `x` is a scaled double, the `x` and `y` data will be the same, but `y` will have fixed-point settings. If `x` is a double or single then `y` = `x`. This functionality lets you share the same code for both floating-point data types and fi objects when quantizers are present.

• Use `n = numerictype(q)` to get a numerictype for the current settings of quantizer `q`.

• Use `clone(q)` to create a quantizer object with the same property values as `q`.

• If you use a `fixed.quantizer` in code generation, note that it is a handle object and must be declared as persistent.