Main Content

# quantize

Quantize fixed-point numbers

## Syntax

``y = quantize(x)``
``y = quantize(x,nt)``
``y = quantize(x,nt,rm)``
``y = quantize(x,nt,rm,oa)``
``yBP = quantize(x,s)``
``yBP = quantize(x,s,wl)``
``yBP = quantize(x,s,wl,fl)``
``yBP = quantize(x,s,wl,fl,rm)``
``yBP = quantize(x,s,wl,fl,rm,oa)``

## Description

````y = quantize(x)` quantizes `x` using these default values: `numerictype` `(true,16,15)``Floor` rounding method`Wrap` overflow action The `numerictype`, rounding method, and overflow action apply only during the quantization. The resulting value, quantized `y`, does not have any fimath attached to it.```
````y = quantize(x,nt)` quantizes `x` to the specified `numerictype` `nt`. The rounding method and overflow action use default values. ```
````y = quantize(x,nt,rm)` quantizes `x` to the specified `numerictype`, `nt` and rounding method, `rm`. The overflow action uses the default value.```

example

````y = quantize(x,nt,rm,oa)` quantizes `x` to the specified `numerictype`, `nt`, rounding method, `rm`, and overflow action, `oa`. ```
````yBP = quantize(x,s)` quantizes `x` to a binary-point, scaled fixed-point number. The `s` input specifies the sign to be used in `numerictype` `(s,16,15)`. Unspecified properties use these default values: `WordLength` 16`FractionLength` 15`RoundingMethod` `Floor``OverflowAction` `Wrap` ```
````yBP = quantize(x,s,wl)` uses the specified word length, `wl`. The fraction length defaults to `wl`–1. Unspecified properties use default values.```
````yBP = quantize(x,s,wl,fl)` uses the specified fraction length, `fl`. Unspecified properties use default values.```
````yBP = quantize(x,s,wl,fl,rm)` uses the specified rounding method, `rm`. Unspecified properties use default values.```

example

````yBP = quantize(x,s,wl,fl,rm,oa)` uses the specified overflow action, `oa`.```

## Examples

collapse all

Create `numerictype` object, `ntBP`, which specifies a signed, 8-bit word length, 4-bit fraction length data type.

`ntBP = numerictype(1,8,4);`

Define the input.

`x_BP = fi(pi)`
```x_BP = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13 ```

Use the defined `numerictype`, `ntBP`, to quantize the input, `x_BP`, to a binary-point scaled data type.

`yBP1 = quantize(x_BP,ntBP)`
```yBP1 = 3.1250 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 4 ```

Create a `numerictype` object, `ntSB`, which specifies a slope-bias data type.

```ntSB = numerictype('Scaling','SlopeBias', ... 'SlopeAdjustmentFactor',1.8,'Bias',... 1,'FixedExponent',-12);```

Define the input.

`x_BP = fi(pi)`
```x_BP = 3.1416 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 16 FractionLength: 13 ```

Use the defined `numerictype`, `ntSB`, to quantize the input, `x_BP`, to a slope-bias data type.

`ySB1 = quantize(x_BP, ntSB)`
```ySB1 = 3.1415 DataTypeMode: Fixed-point: slope and bias scaling Signedness: Signed WordLength: 16 Slope: 0.000439453125 Bias: 1 ```

Create a `numerictype` object, `ntBP`, which specifies a signed, 8-bit word length, 4-bit fraction length data type.

`ntBP = numerictype(1,8,4);`

Define the input.

`x_SB = fi(rand(5,3),numerictype('Scaling','SlopeBias','Bias',-0.125))`
```x_SB=5×3 object 0.8147 0.0975 0.1576 0.8750 0.2785 0.8750 0.1270 0.5469 0.8750 0.8750 0.8750 0.4854 0.6324 0.8750 0.8003 DataTypeMode: Fixed-point: slope and bias scaling Signedness: Signed WordLength: 16 Slope: 3.0517578125e-5 Bias: -0.125 ```

Use the defined `numerictype`, `ntBP`, to quantize the input, `x_SB`, to a binary point scaled data type.

`yBP2 = quantize(x_SB,ntBP,'Nearest','Saturate')`
```yBP2=5×3 object 0.8125 0.1250 0.1875 0.8750 0.2500 0.8750 0.1250 0.5625 0.8750 0.8750 0.8750 0.5000 0.6250 0.8750 0.8125 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 4 ```

Create a `numerictype` object, `ntSB`, which specifies a slope-bias data type.

```ntSB = numerictype('Scaling','SlopeBias', ... 'SlopeAdjustmentFactor',1.8,'Bias',... 1,'FixedExponent',-12);```

Define the input.

`x_SB = fi(rand(5,3),numerictype('Scaling','SlopeBias','Bias',-0.125))`
```x_SB=5×3 object 0.8147 0.0975 0.1576 0.8750 0.2785 0.8750 0.1270 0.5469 0.8750 0.8750 0.8750 0.4854 0.6324 0.8750 0.8003 DataTypeMode: Fixed-point: slope and bias scaling Signedness: Signed WordLength: 16 Slope: 3.0517578125e-5 Bias: -0.125 ```

Use the defined `numerictype`, `ntSB`, to quantize the input, `x_SB`, to a slope-bias data type.

`ySB2 = quantize(x_SB,ntSB,'Ceiling','Wrap')`
```ySB2=5×3 object 0.8150 0.0978 0.1580 0.8752 0.2789 0.8752 0.1272 0.5469 0.8752 0.8752 0.8752 0.4854 0.6326 0.8752 0.8005 DataTypeMode: Fixed-point: slope and bias scaling Signedness: Signed WordLength: 16 Slope: 0.000439453125 Bias: 1 ```

Create a `numerictype` object, `ntBP`, which specifies a signed, 8-bit word length, 4-bit fraction length data type.

`ntBP = numerictype(1,8,4);`

Define the input.

`xInt = int8(-16:4:16)`
```xInt = 1x9 int8 row vector -16 -12 -8 -4 0 4 8 12 16 ```

Use the defined `numerictype`, `ntBP`, to quantize the input|xInt| to a binary point scaled data type.

`yBP3 = quantize(xInt,ntBP,'Zero')`
```yBP3=1×9 object 0 4 -8 -4 0 4 -8 -4 0 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 4 ```

Show the range of the quantized output.

`range(yBP3)`
```ans=1×2 object -8.0000 7.9375 DataTypeMode: Fixed-point: binary point scaling Signedness: Signed WordLength: 8 FractionLength: 4 ```

The first two and last three values are wrapped because they are outside the representable range of the output type.

Create a `numerictype` object `ntSB`, which specifies a slope-bias data type.

```ntSB = numerictype('Scaling','SlopeBias', ... 'SlopeAdjustmentFactor',1.8,'Bias',... 1,'FixedExponent',-12);```

Define the input.

`xInt = int8(-16:4:16)`
```xInt = 1x9 int8 row vector -16 -12 -8 -4 0 4 8 12 16 ```

Use the defined `numerictype`, `ntSB`, to quantize the input, `xInt`, to a slope-bias data type.

`ySB3 = quantize(xInt,ntSB,'Round','Saturate')`
```ySB3=1×9 object Columns 1 through 7 -13.4000 -11.9814 -7.9877 -3.9939 -0.0002 3.9936 7.9873 Columns 8 through 9 11.9811 15.3996 DataTypeMode: Fixed-point: slope and bias scaling Signedness: Signed WordLength: 16 Slope: 0.000439453125 Bias: 1 ```

Show the range of the quantized output.

`range(ySB3)`
```ans=1×2 object -13.4000 15.3996 DataTypeMode: Fixed-point: slope and bias scaling Signedness: Signed WordLength: 16 Slope: 0.000439453125 Bias: 1 ```

The first and last values saturate because they are at the limits of he representable range of the output type.

## Input Arguments

collapse all

Input data to quantize. Valid inputs are:

• Built-in signed or unsigned integers (`int8`, `int16`, `int32`, `int64`, `uint8`, `uint16`, `uint32`, `uint64`)

• Binary point scaled fixed-point fi

• Slope-bias scaled fixed-point fi

Although fi doubles and fi singles are allowed as inputs, they pass through the `quantize` function without being quantized.

Numerictype object that defines the sign, word length, and fraction length of a fixed-point number.

Rounding method to use

Action to take when a data overflow occurs

Whether the fixed-point number is signed (`true`) or unsigned (`false`)

Word length of the fixed-point number

Fraction length of the fixed-point number

## Output Arguments

collapse all

Quantized value of the input

Input quantized to binary-point scaled value

## See Also

### Topics

Introduced before R2006a

## Support Get trial now