Main Content

Data type optimization seeks to minimize an objective function, such as the total bit-width or an estimated count of operators in generated code for a specified system, while maintaining original system behavior within a specified tolerance. During the optimization, the software establishes a baseline by simulating the original model. It then constructs different fixed-point versions of your model and runs simulations to determine the behavior using the new data types. The optimization selects the model that minimizes the objective function while also meeting the specified behavioral constraints.

The model containing the system you want to optimize must have the following characteristics:

All blocks in the model must support fixed-point data types.

The design ranges specified on blocks in the model must be consistent with the simulation ranges.

If the model contains a MATLAB Function block, it must use MATLAB

^{®}language features supported for fixed-point conversion. For more information, see MATLAB Language Features Supported for Automated Fixed-Point Conversion.The data logging format of the model must be set to

`Dataset`

.To configure this setting, in the Configuration Parameters, in the

**Data Import/Export**pane, set**Format**to`Dataset`

.The model must have finite simulation stop time.

During the optimization process, the software makes the following model configuration changes:

In the Configuration Parameters, in the

**Diagnostics**>**Data Validity**pane, set the**Simulation range checking**parameter to`error`

.If any simulation ranges violate the specified design ranges, the optimization stops.

In the Configuration Parameters, in the

**Diagnostics**>**Data Validity**pane, set the**Detect precision loss**,**Detect underflow**, and**Detect overflow**parameters to`none`

.To optimize the data types of a system, the optimization lowers the precision of the baseline model. Therefore, some overflows, underflows, and precision loss is expected. You can set the acceptable level of quantization using the

`addTolerance`

method.

You can restore these diagnostics after the optimization in the Configuration Parameters dialog box.

To determine if the behavior of a new fixed-point implementation is acceptable, the
optimization requires well-defined behavioral constraints. To define a constraint, use the
`addTolerance`

method of the
`fxpOptimizationOptions`

object, or use one
or more Model Verification blocks in your model. For more
information, see Specify Behavioral Constraints.

When the **Lock data types against changes by the fixed-point tools**
setting of a block within the system you want to optimize is enabled, it minimizes the
freedom of the optimization process to find new solutions.

The `fxpopt`

function returns an `OptimizationResult`

object containing a
series of fixed-point implementations called solutions. If the optimization process finds a
fixed-point implementation that meets the specified behavioral constraints, the solutions
are sorted by cost, giving the best solution with the smallest cost (bit width or operator
counts) as the first element of the array.

In cases where the optimization is not able to find a fixed-point implementation meeting the behavioral constraints, the solutions are ordered by maximum absolute difference from the baseline model, with the smallest difference as the first element.

Explore the best found solution using the `explore`

method of the
`OptimizationResult`

object. You can also explore any of the other found
solutions in the same manner. Do not save and close the model until you select the solution
you want to keep. Closing or saving the model inhibits further exploration of different
solutions.

This example shows how to optimize the data types used by a system based on specified tolerances.

To begin, open the system for which you want to optimize the data types.

model = 'ex_auto_gain_controller'; sud = 'ex_auto_gain_controller/sud'; open_system(model)

Create an `fxpOptimizationOptions`

object to define constraints and tolerances to meet your design goals. Set the `UseParallel`

property of the `fxpOptimizationOptions`

object to `true`

to run iterations of the optimization in parallel. You can also specify word lengths to allow in your design through the `AllowableWordLengths`

property.

opt = fxpOptimizationOptions('AllowableWordLengths', 10:24, 'UseParallel', true)

opt = fxpOptimizationOptions with properties: MaxIterations: 50 MaxTime: 600 Patience: 10 Verbosity: High AllowableWordLengths: [10 11 12 13 14 15 16 17 18 19 20 21 22 23 24] UseParallel: 1 Advanced Options AdvancedOptions: [1×1 struct]

Use the `addTolerance`

method to define tolerances for the differences between the original behavior of the system, and the behavior using the optimized fixed-point data types.

tol = 10e-2; addTolerance(opt, [model '/output_signal'], 1, 'AbsTol', tol);

Use the `fxpopt`

function to run the optimization. The software analyzes ranges of objects in your system under design and the constraints specified in the `fxpOptimizationOptions`

object to apply heterogeneous data types to your system while minimizing total bit width.

result = fxpopt(model, sud, opt);

Starting parallel pool (parpool) using the 'local' profile ... Connected to the parallel pool (number of workers: 4). + Preprocessing + Modeling the optimization problem - Constructing decision variables + Running the optimization solver Analyzing and transferring files to the workers ...done. - Evaluating new solution: cost 180, does not meet the tolerances. - Evaluating new solution: cost 198, does not meet the tolerances. - Evaluating new solution: cost 216, does not meet the tolerances. - Evaluating new solution: cost 234, does not meet the tolerances. - Evaluating new solution: cost 252, does not meet the tolerances. - Evaluating new solution: cost 270, does not meet the tolerances. - Evaluating new solution: cost 288, does not meet the tolerances. - Evaluating new solution: cost 306, meets the tolerances. - Evaluating new solution: cost 324, meets the tolerances. - Evaluating new solution: cost 342, meets the tolerances. - Evaluating new solution: cost 360, meets the tolerances. - Evaluating new solution: cost 378, meets the tolerances. - Evaluating new solution: cost 396, meets the tolerances. - Evaluating new solution: cost 414, meets the tolerances. - Evaluating new solution: cost 432, meets the tolerances. - Updated best found solution, cost: 306 - Evaluating new solution: cost 304, meets the tolerances. - Evaluating new solution: cost 304, meets the tolerances. - Evaluating new solution: cost 301, meets the tolerances. - Evaluating new solution: cost 305, does not meet the tolerances. - Evaluating new solution: cost 305, meets the tolerances. - Evaluating new solution: cost 301, meets the tolerances. - Evaluating new solution: cost 299, meets the tolerances. - Evaluating new solution: cost 299, meets the tolerances. - Evaluating new solution: cost 296, meets the tolerances. - Evaluating new solution: cost 299, meets the tolerances. - Evaluating new solution: cost 291, meets the tolerances. - Evaluating new solution: cost 296, does not meet the tolerances. - Evaluating new solution: cost 299, meets the tolerances. - Evaluating new solution: cost 300, meets the tolerances. - Evaluating new solution: cost 296, does not meet the tolerances. - Evaluating new solution: cost 301, meets the tolerances. - Evaluating new solution: cost 303, meets the tolerances. - Evaluating new solution: cost 299, meets the tolerances. - Evaluating new solution: cost 304, does not meet the tolerances. - Evaluating new solution: cost 300, meets the tolerances. - Updated best found solution, cost: 304 - Updated best found solution, cost: 301 - Updated best found solution, cost: 299 - Updated best found solution, cost: 296 - Updated best found solution, cost: 291 - Evaluating new solution: cost 280, meets the tolerances. - Evaluating new solution: cost 287, meets the tolerances. - Evaluating new solution: cost 288, does not meet the tolerances. - Evaluating new solution: cost 287, does not meet the tolerances. - Evaluating new solution: cost 283, meets the tolerances. - Evaluating new solution: cost 283, does not meet the tolerances. - Evaluating new solution: cost 262, does not meet the tolerances. - Evaluating new solution: cost 283, does not meet the tolerances. - Evaluating new solution: cost 282, does not meet the tolerances. - Evaluating new solution: cost 288, meets the tolerances. - Evaluating new solution: cost 289, meets the tolerances. - Evaluating new solution: cost 288, meets the tolerances. - Evaluating new solution: cost 290, meets the tolerances. - Evaluating new solution: cost 281, does not meet the tolerances. - Evaluating new solution: cost 286, does not meet the tolerances. - Evaluating new solution: cost 287, meets the tolerances. - Evaluating new solution: cost 284, meets the tolerances. - Evaluating new solution: cost 282, meets the tolerances. - Evaluating new solution: cost 285, does not meet the tolerances. - Evaluating new solution: cost 277, meets the tolerances. - Updated best found solution, cost: 280 - Updated best found solution, cost: 277 - Evaluating new solution: cost 272, meets the tolerances. - Evaluating new solution: cost 266, meets the tolerances. - Evaluating new solution: cost 269, meets the tolerances. - Evaluating new solution: cost 271, does not meet the tolerances. - Evaluating new solution: cost 274, meets the tolerances. - Evaluating new solution: cost 275, meets the tolerances. - Evaluating new solution: cost 274, does not meet the tolerances. - Evaluating new solution: cost 275, meets the tolerances. - Evaluating new solution: cost 276, does not meet the tolerances. - Evaluating new solution: cost 271, meets the tolerances. - Evaluating new solution: cost 267, meets the tolerances. - Evaluating new solution: cost 270, meets the tolerances. - Evaluating new solution: cost 272, meets the tolerances. - Evaluating new solution: cost 264, does not meet the tolerances. - Evaluating new solution: cost 265, does not meet the tolerances. - Evaluating new solution: cost 269, meets the tolerances. - Evaluating new solution: cost 270, meets the tolerances. - Evaluating new solution: cost 269, meets the tolerances. - Evaluating new solution: cost 276, meets the tolerances. - Evaluating new solution: cost 274, meets the tolerances. - Updated best found solution, cost: 272 - Updated best found solution, cost: 266 + Optimization has finished. - Neighborhood search complete. - Maximum number of iterations completed. + Fixed-point implementation that met the tolerances found. - Total cost: 266 - Maximum absolute difference: 0.087035 - Use the explore method of the result to explore the implementation.

Use the `explore`

method of the `OptimizationResult`

object, `result`

, to launch Simulation Data Inspector and explore the design containing the smallest total number of bits while maintaining the numeric tolerances specified in the `opt`

object.

explore(result);

You can revert your model back to its original state using the `revert`

method of the `OptimizationResult`

object.

revert(result);