Main Content

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.

你can find most fixed-point parameters on theData Types窗格的工具箱。下面的图显示了a typicalData Typespane.

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.

SeeRounding Modesfor 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, seeOverflow Handling为定点操作。

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. SeeMultiplication Data Typesfor 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

TheData Type Assistantis an interactive graphical tool available on theData Typespane of some Fixed-Point Designer™ blocks.

To learn more about using theData Type Assistantto help you specify block data type parameters, seeSpecify Data Types Using Data Type Assistant(Simulink)

Checking Signal Ranges

Some Fixed-Point Designer blocks haveMinimumandMaximumparameters on theData Typespane. 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, seeSpecify Signal Ranges(Simulink)

Specify System-Level Settings

你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, seeFixed-Point Tool(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 theData overflowline in theDiagnostics > Data Integritypane of the Configuration Parameters dialog box is set toNone

Autoscaling

你can use the Fixed-Point Tool autoscaling feature to set the scaling for toolbox fixed-point data types.

Data type override

toolbox blocks obey theUse local settings,Double,Single, andOffmodes of theData type overrideparameter in the Fixed-Point Tool. TheScaled doublemode 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, anInherit via internal rulechoice is often available for fixed-point block data type parameters, such as theAccumulatorandProduct outputsignals. The following sections describe how the word and fraction lengths are selected for you when you chooseInherit via internal rulefor a fixed-point block data type parameter in toolbox software:

Note

In the equations in the following sections,WL= word length andFL= fraction length.

Internal Rule for Accumulator Data Types

The internal rule for accumulator data types first calculates the ideal, full-precision result. WhereNis the number of addends:

W L i d e a l a c c u m u l a t o r = W L i n p u t t o a c c u m u l a t o r + floor ( log 2 ( N 1 ) ) + 1

F L i d e a l a c c u m u l a t o r = F L i n p u t t o a c c u m u l a t o r

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. SeeThe Effect of the Hardware Implementation Pane on the Internal Rulefor more information.

Internal Rule for Product Data Types

The internal rule for product data types first calculates the ideal, full-precision result:

W L i d e a l p r o d u c t = W L i n p u t 1 + W L i n p u t 2

F L i d e a l p r o d u c t = F L i n p u t 1 + F L i n p u t 2

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. SeeThe Effect of the Hardware Implementation Pane on the Internal Rulefor more information.

Internal Rule for Output Data Types

A few toolbox blocks have anInherit via internal rulechoice 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 inThe 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 theHardware Implementationpane of the Configuration Parameters dialog box. To open this dialog box, clickModeling>Model Settingsin 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 theHardware Implementationpane to calculateaccumulator data typesandproduct data types

Accumulator Data Types.Consider the following modelex_internalRule_accumExp

In theDifferenceblocks, theAccumulatorparameter is set toInherit: Inherit via internal rule, and theOutputparameter is set toInherit: 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, theDevice typeparameter in theHardware Implementationpane of the Configuration Parameters dialog box is set toASIC/FPGA.因此,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:

W L i d e a l a c c u m u l a t o r = W L i n p u t t o a c c u m u l a t o r + floor ( log 2 ( n u m b e r o f a c c u m u l a t i o n s ) ) + 1 W L i d e a l a c c u m u l a t o r = 9 + floor ( log 2 ( 1 ) ) + 1 W L i d e a l a c c u m u l a t o r = 9 + 0 + 1 = 10 W L i d e a l a c c u m u l a t o r 1 = W L i n p u t t o a c c u m u l a t o r 1 + floor ( log 2 ( n u m b e r o f a c c u m u l a t i o n s ) ) + 1 W L i d e a l a c c u m u l a t o r 1 = 16 + floor ( log 2 ( 1 ) ) + 1 W L i d e a l a c c u m u l a t o r 1 = 16 + 0 + 1 = 17 W L i d e a l a c c u m u l a t o r 2 = W L i n p u t t o a c c u m u l a t o r 2 + floor ( log 2 ( n u m b e r o f a c c u m u l a t i o n s ) ) + 1 W L i d e a l a c c u m u l a t o r 2 = 127 + floor ( log 2 ( 1 ) ) + 1 W L i d e a l a c c u m u l a t o r 2 = 127 + 0 + 1 = 128

Calculate the full-precision fraction length, which is the same for each Matrix Sum block in this example:

F L i d e a l a c c u m u l a t o r = F L i n p u t t o a c c u m u l a t o r F L i d e a l a c c u m u l a t o r = 4

Now change theDevice typeparameter in theHardware Implementationpane of the Configuration Parameters dialog box to32–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.Consider the following modelex_internalRule_prodExp

In theArray-Vector Multiplyblocks, theProduct Outputparameter is set toInherit: Inherit via internal rule, and theOutputparameter is set toInherit: 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 theAccumulatorparameter does not matter because this example uses real values.

For the preceding model, theDevice typeparameter in theHardware Implementationpane of the Configuration Parameters dialog box is set toASIC/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:

W L i d e a l p r o d u c t = W L i n p u t a + W L i n p u t b W L i d e a l p r o d u c t = 7 + 5 = 12 W L i d e a l p r o d u c t 1 = W L i n p u t a + W L i n p u t b W L i d e a l p r o d u c t 1 = 16 + 15 = 31

Calculate the full-precision fraction length, which is the same for each Array-Vector Multiply block in this example:

F L i d e a l a c c u m u l a t o r = F L i n p u t t o a c c u m u l a t o r F L i d e a l a c c u m u l a t o r = 4

Now change theDevice typeparameter in theHardware Implementationpane of the Configuration Parameters dialog box to32–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 theex_fixedpoint_tutmodel:

Prepare the Model

  1. Open the model by typingex_fixedpoint_tutat the MATLAB®command line.

    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.

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

  3. To investigate the overflows in this model, use the Fixed-Point Tool. You can open the Fixed-Point Tool by selectingTools>Fixed-Point>Fixed-Point Toolfrom the model menu. Turn on logging for all blocks in your model by setting the定点测量模式parameter toMinimums, maximums and overflows

  4. Now that you have turned on logging, rerun the model by clicking the Simulation button.

  5. The results of the simulation appear in a table in the centralContentspane 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.

    你can also see that theSimMinandSimMaxvalues for the Accumulator data types range from0to.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 theContentspane, and click theShow details for selected resultbutton ()

  6. Assume a target hardware that supports 32-bit integers, and set the Accumulator word length in both Cumulative Sum blocks to32.To do so, perform the following steps:

    1. Right-click theSigned Cumulative Sum: Accumulatorrow in the Fixed-Point Tool pane, and selectHighlight Block In Model

    2. Double-click the block in the model, and select theData Typespane of the dialog box.

    3. Open theData Type Assistantfor Accumulator by clicking the Assistant button () in the Accumulator data type row.

    4. Set theModetoFixed Point.To see the representable range of the current specified data type, click theFixed-point detailslink. The tool displays the representable maximum and representable minimum values for the current data type.

    5. Change theWord lengthto32, and click theRefresh detailsbutton in theFixed-point detailssection to see the updated representable range. When you change the value of theWord lengthparameter, theData Typeedit box automatically updates.

    6. ClickOKon the block dialog box to save your changes and close the window.

    7. Set the word length of the Accumulator data type of the Unsigned Cumulative Sum block to32bits. You can do so in one of two ways:

      • Type the data typefixdt([],32,0)directly intoData Typeedit box for the Accumulator data type parameter.

      • 执行相同的步骤你这个词用于设置勒ngth of the Accumulator data type of the Signed Cumulative Sum block to32bits.

  7. To verify your changes in word length and check for overflow, rerun your model. To do so, click theSimulatebutton in the Fixed-Point Tool.

    TheContentspane 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 theSimMinandSimMaxvalues range from0to0.This underflow happens because the fraction length of the Accumulator data type is too small. TheSpecifiedDTcannot 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

TheData type overridefeature of the Fixed-Point tool allows you to override the data types specified in your model with floating-point types. Running your model inDouble给你一个参考范围帮助覆盖模式you select appropriate fraction lengths for your fixed-point data types. To do so, perform the following steps:

  1. Open the Fixed-Point Tool and setData type overridetoDouble

  2. Run your model by clicking theRun simulation and store active resultsbutton.

  3. Examine the results in theContentspane of the Fixed-Point Tool. Because you ran the model inDoubleoverride mode, you get an accurate, idealized representation of the simulation minimums and maximums. These values appear in theSimMinandSimMax参数。

  4. 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, selectResults>Move Active Results To Referencefrom the Fixed-Point Tool menu. The status displayed in theRuncolumn changes fromActivetoReferencefor all signals in your model.

Use the Fixed-Point Tool to Propose Fraction Lengths

Now that you have yourDoubleoverride results saved as a floating-point reference, you are ready to propose fraction lengths.

  1. To propose fraction lengths for your data types, you must have a set ofActiveresults available in the Fixed-Point Tool. To produce an active set of results, simply rerun your model. The tool now displays both theActiveresults and theReferenceresults for each signal.

  2. Select theUse simulation min/max if design min/max is not availablecheck 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, seeSpecify Signal Ranges(Simulink)

  3. Click thePropose fraction lengthsbutton (). The tool populates the proposed data types in theProposedDTcolumn of theContentspane. The corresponding proposed minimums and maximums are displayed in theProposedMinandProposedMaxcolumns.

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:

  1. In theContentspane 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 theContentspane of the Fixed-Point Tool.

    • Open the Autoscale Information window for the highlighted data type by clicking theShow autoscale information for the selected resultbutton ().

  2. When the Autoscale Information window opens, check theValueandPercent Proposed Representablecolumns for theSimulation MinimumandSimulation Maximum参数。你can see that the proposed data type can represent 100% of the range of simulation data.

  3. To accept the proposed data types, select the check box in theAcceptcolumn for each data type whose proposed scaling you want to keep. Then, click theApply accepted fraction lengthsbutton (). The tool updates the specified data types on the block dialog boxes and theSpecifiedDTcolumn in theContentspane.

  4. To verify the newly accepted scaling, set theData type overrideparameter back toUse local settings, and run the model. Looking atContentspane of the Fixed-Point Tool, you can see the following details:

    • TheSimMinandSimMaxvalues of theActiverun match theSimMinandSimMaxvalues from the floating-pointReferencerun.

    • There are no longer any overflows.

    • TheSimDTdoes not match theSpecifiedDTfor the Accumulator data type of either Cumulative Sum block. This difference occurs because the Cumulative Sum block always inherits itsSignednessfrom the input signal and only allows you to specify aSignednessofAuto.Therefore, theSpecifiedDTfor both Accumulator data types isfixdt([],32,17).However, because the Signed Cumulative Sum block has a signed input signal, theSimDTfor the Accumulator parameter of that block is also signed (fixdt(1,32,17)). Similarly, theSimDTfor the Accumulator parameter of the Unsigned Cumulative Sum block inherits itsSignednessfrom its input signal and thus is unsigned (fixdt(0,32,17)).