Convert number to nearest value representable by specified fixed-point data type
outValue = num2fixpt(OrigValue, FixPtDataType, FixPtScaling, RndMeth, DoSatur)
num2fixpt(OrigValue, FixPtDataType, FixPtScaling,
RndMeth, DoSatur)
returns the result of converting OrigValue
to
the nearest value representable by the fixed-point data type FixPtDataType
.
Both OrigValue
and outValue
are
of data type double
. As illustrated in the example
that follows, you can use num2fixpt
to investigate
quantization error that might result from converting a number to a
fixed-point data type. The arguments of num2fixpt
include:
| Value to be converted to a fixed-point representation.
Must be specified using a |
| The fixed-point data type used to convert |
| Scaling of the output in either Slope or [Slope Bias]
format. If |
| Rounding technique used if the fixed-point data type
lacks the precision to represent |
| Indicates whether the output should be saturated to the
minimum or maximum representable value upon underflow or overflow.
If |
Suppose you wish to investigate the quantization effect associated with representing the real-world value 9.875 as a signed, 8-bit fixed-point number. The command
num2fixpt(9.875, sfix(8), 2^-1) ans = 9.50000000000000
reveals that a slope of 2^-1
results in a
quantization error of 0.375. The command
num2fixpt(9.875, sfix(8), 2^-2) ans = 9.75000000000000
demonstrates that a slope of 2^-2
reduces
the quantization error to 0.125. But a slope of 2^-3
,
as used in the command
num2fixpt(9.875, sfix(8), 2^-3) ans = 9.87500000000000
eliminates the quantization error entirely.