Overview of Blockset Features

This section provides a brief overview of the most important Fixed-Point Blockset features. After reading this section and Example: Converting from Doubles to Fixed-Point, you should be able to configure simple fixed-point models that suit your own application needs.

Configuring Fixed-Point Blocks

You configure fixed-point blocks with a parameter dialog box. Block configuration consists of supplying values for parameters via editable text fields, check boxes, and parameter lists. The dialog box for the FixPt Gateway In block is shown below.


The parameters associated with this block are discussed below. For detailed information about each fixed-point block, refer to Chapter 9, Block Reference.

Real-World Values Versus Integer Values

You can configure the fixed-point gateway blocks to treat signals as real-world values or as stored integers with the Treat input as parameter list. The possible values are Real World Value and Stored Integer.

In terms of the variables defined in The General Slope/Bias Encoding Scheme, the real-world value is given by V and the stored integer value is given by Q. You may want to treat numbers as stored integer values if you are modeling hardware that produces integers as output.

Selecting the Output Data Type

For many fixed-point blocks, you have the option of specifying the output data type via the block dialog box, or inheriting the output data type from another block. You control how the output data type is selected with the Output data type and scaling parameter list. The possible values are Specify via dialog, Inherit via internal rule, and Inherit via back propagation. Some blocks support only two of these values.

The Fixed-Point Blockset supports several fixed-point and floating-point data types. Fixed-point data types are characterized by their word size in bits and radix (binary) point. The radix point is the means by which fixed-point values are scaled. Additionally:

  • Unsigned and two's complement formats are supported.

  • The fixed-point word size can range from 1 to 128 bits.

  • The radix point is not required to be contiguous with the fixed-point word.

Floating-point data types are characterized by their sign bit, fraction (mantissa) field, and exponent field. The Fixed-Point Blockset supports IEEE singles, IEEE doubles, and a nonstandard IEEE-style floating-point data type.

You can create Fixed-Point Blockset data types directly in the MATLAB workspace and then pass the resulting structure to a fixed-point block, or you can specify the data type directly with the block dialog box.

 

Integers.   You specify unsigned and signed integers with the uint and sint functions, respectively.

For example, to specify a 16-bit unsigned integer via the block dialog box, you configure the Output data type parameter as uint(16). To specify a 16-bit signed integer, you configure the Output data type parameter as sint(16). 

For integer data types, the default radix point is assumed to lie to the right of all bits.

Fractional Numbers.   You specify unsigned and signed fractional numbers with the ufrac and sfrac functions, respectively. 

For example, to configure the output as a 16-bit unsigned fractional number via the block dialog box, you specify the Output data type parameter to be ufrac(16). To configure a 16-bit signed fractional number, you specify Output data type to be sfrac(16). 

Fractional numbers are distinguished from integers by their default scaling. Whereas signed and unsigned integer data types have a default radix point to the right of all bits, unsigned fractional data types have a default radix point to the left of all bits, while signed fractional data types have a default radix point to the right of the sign bit.

Both unsigned and signed fractional data types support guard bits, which act to "guard" against overflow. For example, sfrac(16,4) specifies a 16-bit signed fractional number with 4 guard bits. The guard bits lie to the left of the default radix point.

Generalized Fixed-Point Numbers.   You specify unsigned and signed generalized fixed-point numbers with the ufix and sfix functions. respectively. 

For example, to configure the output as a 16-bit unsigned generalized fixed-point number via the block dialog box, you specify the Output data type parameter to be ufix(16). To configure a 16-bit signed generalized fixed-point number, you specify Output data type to be sfix(16).

Generalized fixed-point numbers are distinguished from integers and fractionals by the absence of a default scaling. For these data types, you must explicitly specify the scaling with the Output scalingdialog box parameter, or inherit the scaling from another block. Refer to Selecting the Output Scaling for more information.

Floating-Point Numbers.   The Fixed-Point Blockset supports single-precision and double-precision floating-point numbers as defined by the IEEE Standard 754-1985 for Binary Floating-Point Arithmetic. You specify floating-point numbers with the float function.

For example, to configure the output as a single-precision floating-point number via the block dialog box, you specify the Output data type parameter to be float('single'). To configure a double-precision floating-point number, you specify Output data type to be float('double').

You can also specify a nonstandard floating-point number that mimics the IEEE style. For this data type, the fraction (mantissa) can range from 1 to 52 bits and the exponent can range from 1 to 11 bits. For example, to configure a nonstandard floating-point number having 32 total bits and 9 exponents bits, you specify Output data type to be float(32,9).

These numbers are normalized with a hidden leading 1 for all exponents except the smallest possible exponent. However, the largest possible exponent might not be treated as a flag for infinity or NaNs.

Selecting the Output Scaling

Most data types supported by the Fixed-Point Blockset have a default scaling that you cannot change. However, for generalized fixed-point data types, you have the option of specifying the output scaling via the block dialog box, or inheriting the output scaling from another block. You control how the output scaling is selected with the Output data type and scaling parameter list.

The Fixed-Point Blockset supports two general scaling modes: radix point-only scaling and slope/bias scaling. In addition to these general scaling modes, the blockset provides you with additional block-specific scaling choices for constant vectors and constant matrices. These scaling choices are based on radix point-only scaling and are designed to maximize precision. Refer to Example: Constant Scaling for Best Precision in Chapter 3 for more information.

To help you understand the supported scaling modes, the general slope/bias encoding scheme is presented in the next section.

The General Slope/Bias Encoding Scheme.   When representing an arbitrarily precise real-world value with a fixed-point number, it is often useful to define a general slope/bias encoding scheme

where:

  • V is the real-world value.

  • á¹¼ is the approximate real-world value.

  • Q is an integer that encodesV.

  • B is the bias.

  • S = F2E is the slope.

The slope is partitioned into two components:

  • 2E specifies the radix point. E is the fixed power-of-two exponent.

  • F is the fractional slope. It is normalized such that 1 â‹œ F < 2.

Radix Point-Only Scaling.   This is "powers-of-two" scaling since it involves moving only the radix point. Radix point-only scaling does not require the radix point to be contiguous with the data word. The advantage of this scaling mode is the number of processor arithmetic operations is minimized.

You specify radix point-only scaling with the syntax 2^-E where E is unrestricted. This creates a MATLAB structure with a bias B = 0 and a fractional slope F = 1.0.

For example, if you specify the value 2^-10 for the Output scaling parameter, then the generalized fixed-point number has a power-of-two exponent E = -10. This value defines the radix point location to be 10 places to the left of the least significant bit.

Slope/Bias Scaling.   With this scaling mode, you can provide a slope and a bias. The advantage of slope/bias scaling is that it typically provides more efficient use of a finite number of bits.

You specify slope/bias scaling with the syntax [slope bias], which creates a MATLAB structure with the given slope and bias. 

For example, if you specify the value [5/9 10] for the Output scaling parameter, then the generalized fixed-point number has a slope of 5/9 and a bias of 10. The blockset would automatically store Fas 1.1111 and E as -1 due to the normalization condition 1 ⋜ F < 2.

Rounding

You specify how fixed-point numbers are rounded with the Round toward parameter list. These rounding modes are supported:

  • Zero - This mode rounds toward zero and is equivalent to MATLAB's fix function.

  • Nearest - This mode rounds toward the nearest representable number, with the exact midpoint rounded toward positive infinity. Rounding toward nearest is equivalent to MATLAB's roundfunction.

  • Ceiling - This mode rounds toward positive infinity and is equivalent to MATLAB's ceil function.

  • Floor - This mode rounds toward negative infinity and is equivalent to MATLAB's floor function.

Overflow Handling

You control how overflow conditions are handled for fixed-point operations with the Saturate to max or min when overflows occur check box. 

If checked, then overflows saturate to either the maximum or minimum value represented by the data type. For example, an overflow associated with a signed 8-bit integer can saturate to -128 or 127. If unchecked, then overflows wrap to the appropriate value that is representable by the data type. For example, the number 130 does not fit in a signed 8-bit integer, and would wrap to -126.

Locking the Output Scaling

If the output data type is a generalized fixed-point number, then you have the option of locking its scaling by checking the Lock output scaling so autoscaling tool can't change it check box.

When locked, the automatic scaling script autofixexp will not change the output scaling. Otherwise, the autofixexp is free to adjust the scaling.

Overriding with Doubles

By checking the Override data type(s) with doubles check box, you can override any data type with doubles. This feature is useful when debugging a simulation. For example, if you are simulating hardware that is constrained to output integers, you can override the constraint to determine whether the hardware warrants modification or replacement.

Logging Simulation Values

By checking the Log minimums and maximums check box, you can save the maximum and minimum values encountered during a simulation to the MATLAB workspace. You can then access these values with the automatic scaling script autofixexp.

Additional Features and Capabilities

In addition to the features described in Configuring Fixed-Point Blocks, the Fixed-Point Blockset provides you with these features and capabilities:

  • An automatic scaling tool

  • Code generation capabilities

Automatic Scaling

You can use the autofixexp script to automatically change the scaling for each block that has generalized fixed-point output and does not have its scaling locked. The script uses the maximum and minimum values logged during the last simulation run. The scaling is changed such that the simulation range is covered and the precision is maximized.

As an alternative to (and extension of) the automatic scaling script, you can use the Fixed-Point Blockset Interface tool. This tool allows you to easily control the parameters associated with automatic scaling and display the simulation results for a given model. Additionally, you can:

  • Turn on or turn off logging for all blocks

  • Override the output data type with doubles for all blocks

  • Invoke the automatic scaling script

To learn how to use the Fixed-Point Blockset Interface tool, refer to Chapter 6, Tutorial: Feedback Controller Simulation.

Code Generation

With the Real-Time Workshop, the Fixed-Point Blockset can generate C code. The code generated from fixed-point blocks uses only integer types and automatically includes all operations, such as shifts, needed to account for differences in fixed-point locations. 

You can use the generated code on embedded fixed-point processors or rapid prototyping systems even if they contain a floating-point processor. The code is structured so that key operations can be readily replaced by optimized target-specific libraries that you supply. You can also use the Target Language CompilerTM to customize the generated code. Refer to Appendix A for more information about code generation using fixed-point blocks.