specify fixed-point attributes for blocks
fixed-point block parameters
toolbox blocks that have fixed-point support usually allow you to specify fixed-point characteristics through block parameters. by specifying data type and scaling information for these fixed-point parameters, you can simulate your target hardware more closely.
note
floating-point inheritance takes precedence over the settings discussed in this section. when the block has floating-point input, all block data types match the input.
you can find most fixed-point parameters on the data types pane of toolbox blocks. the following figure shows a typical data types pane.
all toolbox blocks with fixed-point capabilities share a set of common parameters, but each block can have a different subset of these fixed-point parameters. the following table provides an overview of the most common fixed-point block parameters.
fixed-point data type parameter | description |
---|---|
rounding mode | specifies the rounding mode for the block to use when the specified data type and scaling cannot exactly represent the result of a fixed-point calculation. see rounding modes for more information on the available options. |
saturate on integer overflow | when you select this parameter, the block saturates the result of its fixed-point operation. when you clear this parameter, the block wraps the result of its fixed-point operation. for details on saturate and wrap, see overflow handling for fixed-point operations. |
intermediate product | specifies the data type and scaling of the intermediate product for fixed-point blocks. blocks that feed multiplication results back to the input of the multiplier use the intermediate product data type. see the reference page of a specific block to learn about the intermediate product data type for that block. |
product output | specifies the data type and scaling of the product output for fixed-point blocks that must compute multiplication results. see the reference page of a specific block to learn about the product output data type for that block. for or complex-complex multiplication, the multiplication result is in the accumulator data type. see for more information on complex fixed-point multiplication in toolbox software. |
accumulator | specifies the data type and scaling of the accumulator (sum) for fixed-point blocks that must hold summation results for further calculation. most such blocks cast to the accumulator data type before performing the add operations (summation). see the reference page of a specific block for details on the accumulator data type of that block. |
output | specifies the output data type and scaling for blocks. |
using the data type assistant
the data type assistant is an interactive graphical tool available on the data types pane of some fixed-point designer™ blocks.
to learn more about using the data type assistant to help you specify block data type parameters, see (simulink).
checking signal ranges
some fixed-point designer blocks have minimum and maximum parameters on the data types pane. when a fixed-point data type has these parameters, you can use them to specify appropriate minimum and maximum values for range checking purposes.
to learn how to specify signal ranges and enable signal range checking, see (simulink).
specify system-level settings
you can monitor and control fixed-point settings for fixed-point designer blocks at a system or subsystem level with the fixed-point tool. for more information, see (fixed-point designer).
logging
the fixed-point tool logs overflows, saturations, and simulation minimums and
maximums for fixed-point designer blocks. the fixed-point tool does not log overflows and saturations
when the data overflow
line in the diagnostics >
data integrity pane of the configuration parameters dialog box is
set to none
.
autoscaling
you can use the fixed-point tool autoscaling feature to set the scaling for toolbox fixed-point data types.
data type override
toolbox blocks obey the use local settings
,
double
, single
, and
off
modes of the data type
override parameter in the fixed-point tool. the
scaled double
mode is also supported for
toolboxes source and byte-shuffling blocks, and for some arithmetic blocks such as
difference and normalization.
scaled double is a double data type that retains fixed-point scaling information. using the data type override, you can convert your fixed-point data types to scaled doubles. you can then simulate to determine the ideal floating-point behavior of your system. after you gather that information, you can turn data type override off to return to fixed-point data types, and your quantities still have their original scaling information because it was held in the scaled double data types.
inherit via internal rule
selecting appropriate word lengths and scalings for the fixed-point parameters in your
model can be challenging. to aid you, an inherit via internal
rule
choice is often available for fixed-point block data type
parameters, such as the accumulator and product
output signals. the following sections describe how the word and fraction
lengths are selected for you when you choose inherit via internal
rule
for a fixed-point block data type parameter in toolbox
software:
note
in the equations in the following sections, wl = word length and fl = fraction length.
internal rule for accumulator data types
the internal rule for accumulator data types first calculates the ideal, full-precision result. where n is the number of addends:
for example, consider summing all the elements of a vector of length 6 and data type sfix10_en8. the ideal, full-precision result has a word length of 13 and a fraction length of 8.
the accumulator can be real or complex. the preceding equations are used for both the real and imaginary parts of the accumulator. for any calculation, after the full-precision result is calculated, the final word and fraction lengths set by the internal rule are affected by your particular hardware. see the effect of the hardware implementation pane on the internal rule for more information.
internal rule for product data types
the internal rule for product data types first calculates the ideal, full-precision result:
for example, multiplying together the elements of a real vector of length 2 and data type sfix10_en8. the ideal, full-precision result has a word length of 20 and a fraction length of 16.
for real-complex multiplication, the ideal word length and fraction length is used for both the complex and real portion of the result. for complex-complex multiplication, the ideal word length and fraction length is used for the partial products, and the internal rule for accumulator data types described above is used for the final sums. for any calculation, after the full-precision result is calculated, the final word and fraction lengths set by the internal rule are affected by your particular hardware. see the effect of the hardware implementation pane on the internal rule for more information.
internal rule for output data types
a few toolbox blocks have an inherit via internal rule
choice available for the block output. the internal rule used in these cases is
block-specific, and the equations are listed in the block reference page.
as with accumulator and product data types, the final output word and fraction lengths set by the internal rule are affected by your particular hardware, as described in the effect of the hardware implementation pane on the internal rule.
the effect of the hardware implementation pane on the internal rule
the internal rule selects word lengths and fraction lengths that are appropriate for your hardware. to get the best results using the internal rule, you must specify the type of hardware you are using on the hardware implementation pane of the configuration parameters dialog box. to open this dialog box, click modeling > model settings in the simulink® toolstrip.
asic/fpga. on an asic/fpga target, the ideal, full-precision word length and fraction length calculated by the internal rule are used. if the calculated ideal word length is larger than the largest allowed word length, you receive an error.
other targets. for all targets other than asic/fpga, the ideal, full-precision word length calculated by the internal rule is rounded up to the next available word length of the target. the calculated ideal fraction length is used, keeping the least-significant bits.
if the calculated ideal word length for a product data type is larger than the largest word length on the target, you receive an error. if the calculated ideal word length for an accumulator or output data type is larger than the largest word length on the target, the largest target word length is used.
the largest word length allowed for simulink and toolbox software on any target is 128 bits.
internal rule examples
the following sections show examples of how the internal rule interacts with the hardware implementation pane to calculate accumulator data types and product data types.
accumulator data types
open the ex_internalrule_accumexp
model.
in the blocks, the accumulator parameter is set to inherit: inherit via internal rule
, and the output parameter is set to inherit: same as accumulator
. therefore, you can see the accumulator data type calculated by the internal rule on the output signal in the model.
in the preceding model, the device type parameter in the hardware implementation pane of the configuration parameters dialog box is set to asic/fpga
. therefore, the accumulator data type used by the internal rule is the ideal, full-precision result.
calculate the full-precision word length for each of the difference blocks in the model.
calculate the full-precision fraction length, which is the same for each matrix sum block in this example:
now change the device type parameter in the hardware implementation pane of the configuration parameters dialog box to 32-bit embedded processor
, by changing the parameters as shown in the following figure.
as you can see in the dialog box, this device has 8-, 16-, and 32-bit word lengths available. therefore, the ideal word lengths of 10, 17, and 128 bits calculated by the internal rule cannot be used. instead, the internal rule uses the next largest available word length in each case. you can see this if you rerun the model, as shown in the following figure.
product data types
open the ex_internalrule_prodexp
model.
in the blocks, the product output parameter is set to inherit: inherit via internal rule
, and the output parameter is set to inherit: same as product output
. therefore, you can see the product output data type calculated by the internal rule on the output signal in the model. the setting of the accumulator parameter does not matter because this example uses real values.
for the preceding model, the device type parameter in the hardware implementation pane of the configuration parameters dialog box is set to asic/fpga
. therefore, the product data type used by the internal rule is the ideal, full-precision result.
calculate the full-precision word length for each of the array-vector multiply blocks in the model.
calculate the full-precision fraction length, which is the same for each array-vector multiply block in this example.
now change the device type parameter in the hardware implementation pane of the configuration parameters dialog box to 32-bit embedded processor
, as shown in the following figure.
as you can see in the dialog box, this device has 8-, 16-, and 32-bit word lengths available. therefore, the ideal word lengths of 12 and 31 bits calculated by the internal rule cannot be used. instead, the internal rule uses the next largest available word length in each case. you can see this if you rerun the model, as shown in the following figure.
specify data types for fixed-point blocks
the following sections show you how to use the fixed-point tool to select appropriate
data types for fixed-point blocks in the ex_fixedpoint_tut
model:
prepare the model
open the ex_fixedpoint_tut
model.
this model uses the cumulative sum block to sum the input coming from the fixed-point sources subsystem. the fixed-point sources subsystem outputs two signals with different data types:
the signed source has a word length of 16 bits and a fraction length of 15 bits.
the unsigned source has a word length of 16 bits and a fraction length of 16 bits.
run the model to check for overflow. matlab displays the following warnings at the command line:
warning: overflow occurred. this originated from 'ex_fixedpoint_tut/signed cumulative sum'. warning: overflow occurred. this originated from 'ex_fixedpoint_tut/unsigned cumulative sum'.
according to these warnings, overflow occurs in both cumulative sum blocks.
to investigate the overflows in this model, use the fixed-point tool. you can open the fixed-point tool by selecting tools > fixed-point > fixed-point tool from the model menu. turn on logging for all blocks in your model by setting the fixed-point instrumentation mode parameter to minimums, maximums and overflows
.
now that you have turned on logging, rerun the model by clicking the simulation button.
the results of the simulation appear in a table in the central contents pane of the fixed-point tool. review the following columns:
name: provides the name of each signal in the following format:
subsystem name/block name: signal name
.simdt: the simulation data type of each logged signal.
specifieddt: the data type specified on the block dialog for each signal.
simmin: the smallest representable value achieved during simulation for each logged signal.
simmax: the largest representable value achieved during simulation for each logged signal.
overflowwraps: the number of overflows that wrap during simulation.
you can also see that the simmin and simmax values for the accumulator data types range from 0 to .9997. the logged results indicate that 8,192 overflows wrapped during simulation in the accumulator data type of the signed cumulative sum block. similarly, the accumulator data type of the unsigned cumulative sum block had 16,383 overflows wrap during simulation.
to get more information about each of these data types, highlight them in the contents pane, and click the show details for selected result button.
assume a target hardware that supports 32-bit integers, and set the accumulator word length in both cumulative sum blocks to 32. to do so, perform the following steps:
right-click the
signed cumulative sum: accumulator
row in the fixed-point tool pane, and select highlight block in model.double-click the block in the model, and select the data types pane of the dialog box. open the data type assistant for accumulator by clicking the assistant button in the accumulator data type row. set the mode to
fixed point
. to see the representable range of the current specified data type, click the fixed-point details link. the tool displays the representable maximum and representable minimum values for the current data type.
change the word length to 32, and click the refresh details button in the fixed-point details section to see the updated representable range. when you change the value of the word length parameter, the data type edit box automatically updates.
click ok on the block dialog box to save your changes and close the window.
set the word length of the accumulator data type of the unsigned cumulative sum block to 32 bits. you can do so in one of two ways:
type the data type
fixdt([],32,0)
directly into data type edit box for the accumulator data type parameter.perform the same steps you used to set the word length of the accumulator data type of the signed cumulative sum block to 32 bits.
to verify your changes in word length and check for overflow, rerun your model. to do so, click the simulate button in the fixed-point tool.
the contents pane of the fixed-point tool updates, and you can see that no overflows occurred in the most recent simulation. however, you can also see that the simmin and simmax values range from 0 to 0. this underflow happens because the fraction length of the accumulator data type is too small. the specifieddt cannot represent the precision of the data values. the following sections discuss how to find a floating-point benchmark and use the fixed-point tool to propose fraction lengths.
use data type override to find a floating-point benchmark
the data type override feature of the fixed-point tool allows
you to override the data types specified in your model with floating-point types.
running your model in double
override mode gives you a
reference range to help you select appropriate fraction lengths for your fixed-point
data types. to do so, perform the following steps:
open the fixed-point tool and set data type override to
double
.run your model by clicking the run simulation and store active results button.
examine the results in the contents pane of the fixed-point tool. because you ran the model in
double
override mode, you get an accurate, idealized representation of the simulation minimums and maximums. these values appear in the simmin and simmax parameters.now that you have an accurate reference representation of the simulation minimum and maximum values, you can more easily choose appropriate fraction lengths. before making these choices, save your active results to reference so you can use them as your floating-point benchmark. to do so, select results > move active results to reference from the fixed-point tool menu. the status displayed in the run column changes from
active
toreference
for all signals in your model.
use the fixed-point tool to propose fraction lengths
now that you have your double
override results saved as
a floating-point reference, you are ready to propose fraction lengths.
to propose fraction lengths for your data types, you must have a set of
active
results available in the fixed-point tool. to produce an active set of results, simply rerun your model. the tool now displays both theactive
results and thereference
results for each signal.select the use simulation min/max if design min/max is not available check box. you did not specify any design minimums or maximums for the data types in this model. thus, the tool uses the logged information to compute and propose fraction lengths. for information on specifying design minimums and maximums, see (simulink).
click the propose fraction lengths button (). the tool populates the proposed data types in the proposeddt column of the contents pane. the corresponding proposed minimums and maximums are displayed in the proposedmin and proposedmax columns.
examine the results and accept the proposed scaling
before accepting the fraction lengths proposed by the fixed-point tool, it is important to look at the details of that data type. doing so allows you to see how much of your data the suggested data type can represent. to examine the suggested data types and accept the proposed scaling, perform the following steps:
in the contents pane of the fixed-point tool, you can see the proposed fraction lengths for the data types in your model.
the proposed fraction length for the accumulator data type of both the signed and unsigned cumulative sum blocks is 17 bits.
to get more details about the proposed scaling for a particular data type, highlight the data type in the contents pane of the fixed-point tool.
open the autoscale information window for the highlighted data type by clicking the show autoscale information for the selected result button ().
when the autoscale information window opens, check the value and percent proposed representable columns for the simulation minimum and simulation maximum parameters. you can see that the proposed data type can represent 100% of the range of simulation data.
to accept the proposed data types, select the check box in the accept column for each data type whose proposed scaling you want to keep. then, click the apply accepted fraction lengths button (). the tool updates the specified data types on the block dialog boxes and the specifieddt column in the contents pane.
to verify the newly accepted scaling, set the data type override parameter back to use local settings, and run the model. looking at contents pane of the fixed-point tool, you can see the following details:
the simmin and simmax values of the
active
run match the simmin and simmax values from the floating-pointreference
run.there are no longer any overflows.
the simdt does not match the specifieddt for the accumulator data type of either cumulative sum block. this difference occurs because the cumulative sum block always inherits its signedness from the input signal and only allows you to specify a signedness of
auto
. therefore, the specifieddt for both accumulator data types isfixdt([],32,17)
. however, because the signed cumulative sum block has a signed input signal, the simdt for the accumulator parameter of that block is also signed (fixdt(1,32,17)
). similarly, the simdt for the accumulator parameter of the unsigned cumulative sum block inherits its signedness from its input signal and thus is unsigned (fixdt(0,32,17)
).