Accelerating the pace of engineering and science

quantize

Quantize fixed-point numbers

Syntax

• y = quantize(x)
• y = quantize(x,nt)
• y = quantize(x,nt,rm)
• y = quantize(x,nt,rm,oa) example
• 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) example

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

expand all

Quantize Binary-Point Scaled to Binary-Point Scaled Data

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

Quantize Binary-Point Scaled to Slope-Bias Data

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

```ntSB = numerictype('Scaling','SlopeBias', ...
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.1410

DataTypeMode: Fixed-point: slope and bias scaling
Signedness: Signed
WordLength: 16
Slope: 0.000439453125
Bias: 1```

Quantize Slope-Bias Scaled to Binary-Point Scaled Data

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 =

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 =

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

Quantize Slope-Bias Scaled to Slope-Bias Scaled Data

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

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

Define the input.

```x_SB = fi(rand(5,3),numerictype('Scaling','SlopeBias','Bias',-0.125))
```
```x_SB =

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 =

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

Quantize Built-in Integer to Binary-Point Scaled Data

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 =

-16  -12   -8   -4    0    4    8   12   16```

Use the defined numerictype, ntBP, to quantize the inputxInt to a binary point scaled data type.

```yBP3 = quantize(xInt,ntBP,'Zero')
```
```yBP3 =

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 =

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

Quantize Built-in Integer to Slope-Bias Data

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

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

Define the input.

`xInt = int8(-16:4:16)`
```xInt =

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

Columns 1 through 6
-13.4000  -11.9999   -8.0000   -4.0001   -0.0002    4.0001
Columns 7 through 9
8.0000   12.0000   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 =

-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

expand all

x — Input datafi objects or built-in integers

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.

nt — Numerictype (true,16,15) (default)

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

rm — Rounding methodFloor (default) | Ceiling | Convergent | Nearest | Round | Zero

Rounding method to use

oa — Overflow actionWrap (default) | Saturate

Action to take when a data overflow occurs

s — Signednesstrue (default) | false

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

wl — Word length16 (default)

Word length of the fixed-point number

fl — Fraction length15 (default)

Fraction length of the fixed-point number

Output Arguments

expand all

y — Quantized outputfi object

Quantized value of the input

yBP — Quantized outputfi object

Input quantized to binary-point scaled value