Main Content

Automated Fixed-Point Conversion

Automated Fixed-Point Conversion Capabilities

You can convert floating-point MATLAB® code to fixed-point code at the command line using the codegen function -float2fixed option. You can choose to propose data types based on simulation range data, derived (also known as static) range data, or both.

For a list of supported MATLAB features and functions, see MATLAB Language Features Supported for Automated Fixed-Point Conversion (Fixed-Point Designer).

During fixed-point conversion, you can:

  • Propose fraction lengths based on default word lengths.

  • Propose word lengths based on default fraction lengths.

  • Test numerics by running the test file with the fixed-point types applied.

  • Detect overflows.

Proposing Data Types

To define input types, specify a test bench that calls the MATLAB function. When you generate fixed-point code, the test file is used to infer the types for entry-point input arguments.

Automated fixed-point conversion proposes fixed-point data types based on computed ranges and the word length or fraction length setting. The computed ranges are based on simulation range data, derived range data, or both. If you run a simulation and compute derived ranges, the simulation and derived ranges are merged.

Note

You cannot propose data types based on derived ranges for MATLAB classes.

Derived range analysis is not supported for non-scalar variables.

Create a Configuration Object for Fixed-Point Conversion

Use a coder.FixPtConfig object to set configuration parameters for fixed-point conversion. A coder.FixPtConfig object contains the parameters that are required to generate fixed-point code

Running a Simulation

To manually run a simulation with generated fixed-point code, call the instrumented MEX function within the test file or files.

Test files should exercise your algorithm over its full operating range. The quality of the proposed fixed-point data types depends on how well the test file covers the operating range of the algorithm with the desired accuracy. You can run more than one test file during the simulation to improve your results. If the test file fails, the errors are displayed in the command window.

Function Replacements

If your MATLAB code uses functions that do not have fixed-point support, you can choose to replace unsupported functions with a custom function replacement or with a lookup table.

If you enter a function replacement for a function, the replacement function is used when you build the project. If you do not enter a replacement, the tool uses the type specified in the original MATLAB code for the function.

Alternatively, you can exclude functions from fixed-point conversion using coder.float2fixed.skip. For example, you may want to exclude a function from fixed-point conversion if you are using a custom function that already uses fixed-point data types, or if you wish to take advantage of native floating point support for HDL Code generation. For more information, see coder.float2fixed.skip.

Testing Numerics

Set the TestNumerics property of your coder.FixPtConfig object to true to verify the behavior of the fixed-point MATLAB algorithm. The software uses the test file that was used to define inputs or run a simulation to test numerics.

You can compare the numerical behavior of the generated fixed-point MATLAB code with the original floating-point MATLAB code. Set the LogIOForComparisonPlotting parameter to true to log inputs and outputs for comparison plots and generate a plot for each scalar output. This plot shows the floating-point and fixed-point results and the difference between them. For nonscalar outputs, only the error information is shown.

After fixed-point simulation, if the numerical results do not meet the accuracy that you want, modify fixed-point data type settings and repeat the type validation and numerical testing steps. You might have to iterate through these steps multiple times to achieve the results that you want.

Detecting Overflows

Setting the DetectFixPtOverflows enables overflow detection. When this option is selected, the conversion process runs the simulation using scaled double versions of the proposed fixed-point types. Because scaled doubles store their data in double-precision floating-point, they carry out arithmetic in full range. They also retain their fixed-point settings, so they are able to report when a computation goes out of the range of the fixed-point type.

If overflows are detected, a list of variables and expressions that overflowed and information on how much each variable overflowed are reported in the command window.

See Also

Detect OverflowsDetect Overflows (Fixed-Point Designer)

Convert Fixed-Point Conversion Project to MATLAB Scripts