This example shows how to define simulation scenarios and use the Fixed-Point Tool to collect ranges by running simulations using these scenarios. You can then use the Fixed-Point Tool to optimize the fixed-point data types of the system.
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.
Simulink.SimulationInput object in the setup allows you to
define additional simulation scenarios to consider during the optimization. A comprehensive
set of input signals can help to ensure that the full operating range of your design is
exercised during the optimization process.
Open the model. In this example, you optimize the data types of the Controller subsystem. The model is set up to use either a ramp input, or a random input.
model = 'ex_controllerHarness'; open_system(model)
Simulink.SimulationInput object that contains the different scenarios. Use both the ramp input as well as four different seeds for the random input.
si = Simulink.SimulationInput.empty(5, 0); % scan through 4 different seeds for the random input rng(1); seeds = randi(1e6, [1 4]); for sIndex = 1:length(seeds) si(sIndex) = Simulink.SimulationInput(model); si(sIndex) = si(sIndex).setVariable('SOURCE', 2); % SOURCE == 2 corresponds to the random input si(sIndex) = si(sIndex).setBlockParameter([model '/Random/uniformRandom'], 'Seed', num2str(seeds(sIndex))); % scan through the seeds si(sIndex) = si(sIndex).setUserString(sprintf('random_%i', seeds(sIndex))); end % setting SOURCE == 1 corresponds to the ramp input si(5) = Simulink.SimulationInput(model); si(5) = si(5).setVariable('SOURCE', 1); si(5) = si(5).setUserString('Ramp');
To optimize the data types in the mode, use the Fixed-Point Tool.
In the Apps gallery of the
ex_controllerHarness model, select Fixed-Point
In the Fixed-Point Tool, under New workflow, select
Optimized Fixed-Point Conversion.
Under System Under Design (SUD), select the subsystem for which
you want to optimize the data types. In this example, select
Under Range Collection Mode, select Simulation Ranges as the range collection method.
Under Simulation Inputs, you can specify
Simulink.SimulationInput objects to exercise your design over its
full operating range. In this example, use the simulation scenarios you defined. Set
Simulation Inputs to
You can specify tolerances for any signal in the model with signal logging enabled in the table under Signal Tolerances. In this example, the Signal Tolerances section indicates that the model contains no logged signals. Because this model uses an Assertion block from the Model Verification library to verify the numerical behavior of the system during optimization, specifying signal tolerances is optional. For more information, see Specify Behavioral Constraints.
In the toolstrip, click Prepare. The Fixed-Point Tool checks the system under design for compatibility with the conversion process and reports any issues found in the model. When possible, the Fixed-Point Tool automatically changes settings that are not compatible. For more information, see Use the Fixed-Point Tool to Prepare a System for Conversion.
To specify settings to use during the optimization, in the toolstrip, click Settings.
In this example, use the following settings.
Set Allowable Word Lengths to
This setting defines the word lengths that can be used in your optimized system. Use this setting to target the neighborhood search of the optimization process. The final result of the optimization uses word lengths in the intersection of this setting and word lengths compatible with hardware constraints specified in the Hardware Implementation pane of your model.
Set Max Iterations to
This setting specifies the maximum number of iterations to perform in the optimization. The optimization process iterates through different solutions until it finds an ideal solution, reaches the maximum number of iterations, or reaches another stopping criteria.
Set Patience to
This setting defines the maximum number of iterations where no new best solution is found. The optimization continues as long as the algorithm continues to find new best solutions.
Set Objective Function to Bit Width Sum. Using this setting instructs the optimization to minimize the total bit width of the final design while meeting the specified constraints.
For more information about optimization settings, see
To optimize the data types in the model according to the specified settings, click Optimize Data Types. During the optimization process, the software analyzes ranges of objects in your system under design and the constraints specified in the settings to apply heterogeneous data types to your system while minimizing the objective function.
When the optimization completes, the Fixed-Point Tool displays a table that contains all of the solutions found during the optimization process. The first solution in the table corresponds to the solution with the lowest cost (smallest total bit width).
To apply the optimized data types to the model, in the table, select the solution that you want to apply. In the Explore section of the toolstrip, click Apply and Compare. The Fixed-Point Tool applies the selected solution that contains optimized fixed-point data types to the model and opens the Simulation Data Inspector.
In this example, select Solution 1, then click Apply and Compare.
In the Controller subsystem, you can see the applied, optimized fixed-point data types.
The Fixed-Point Tool uses the Simulation Data Inspector tool plotting capabilities that enable you to plot logged signals for graphical analysis. Because this model does not contain any logged signals, the Compare button remains disabled in this example. To compare results of data type optimization using the Simulation Data Inspector, log one or more signals in your model.
After optimizing data types in the Fixed-Point Tool, you can choose to export
optimization workflow steps to a MATLAB® script. This allows you to save the current optimization workflow steps and
continue data type optimization using
the command line.
In the toolstrip, click Export Script. The Fixed-Point Tool
exports a script called
fxpOptimizationScript.m to the current working
model = 'ex_controllerHarness'; sud = 'ex_controllerHarness/Controller'; options = fxpOptimizationOptions(); options.MaxIterations = 300; % Maximum number of iterations to perform. options.Patience = 50; % Maximum number of iterations where no new best solution is found. options.AllowableWordLengths = 2:32; % Word lengths that can be used in your optimized system under design. savedOptions = load('fxpOptimizationScript'); options.AdvancedOptions.SimulationScenarios = savedOptions.simulationScenarios; result = fxpopt(model, sud, options); explore(result);
during optimization is exported to a MAT-file called