Convert a Model to Fixed Point Using the Command Line
This example shows how to refine the data types of a model using the command line.
Open the fxpdemo_feedback
model.
model = 'fxpdemo_feedback';
open_system(model);
The Controller subsystem uses fixed-point data types.
sud = 'fxpdemo_feedback/Controller';
open_system(sud)
Create a DataTypeWorkflow.Converter
object to refine the data types of the Controller subsystem of the fxpdemo_feedback
model.
converter = DataTypeWorkflow.Converter(sud);
Simulate the model and store the results in a run titled InitialRun
.
converter.CurrentRunName = 'InitialRun';
converter.simulateSystem();
Determine any overflows occurred during the run.
saturations = converter.saturationOverflows('InitialRun')
saturations = Result with properties: ResultName: 'fxpdemo_feedback/Controller/Up Cast' SpecifiedDataType: 'fixdt(1,16,14)' CompiledDataType: 'fixdt(1,16,14)' ProposedDataType: '' Wraps: [] Saturations: 23 WholeNumber: 0 SimMin: -2 SimMax: 1.9999 DerivedMin: [] DerivedMax: [] RunName: 'InitialRun' Comments: {'An output data type cannot be specified on this result. The output type is the same as the input type.'} DesignMin: [] DesignMax: []
wraps = converter.wrapOverflows('InitialRun')
wraps = []
A saturation occurs in the Up Cast block of the Controller subsystem during the simulation. There are no wrapping overflows. Refine the data types of the model so that there are no saturations.
Configure the model for conversion using a shortcut. Find the shortcuts that are available for the system by accessing the ShortcutsForSelectedSystem
property of the converter object.
shortcuts = converter.ShortcutsForSelectedSystem
shortcuts = 6x1 cell array {'Range collection using double override' } {'Range collection with specified data types' } {'Range collection using single override' } {'Disable range collection' } {'Remove overrides and disable range collection'} {'Range collection using scaled double override'}
To collect idealized ranges for the system, using the 'Range collection using double override'
shortcut, override the system with double-precision data types and enable instrumentation.
converter.applySettingsFromShortcut(shortcuts{1});
This shortcut also updates the current run name property of the converter object.
baselineRun = converter.CurrentRunName
baselineRun = 'Ranges(Double)'
Simulate the model again to gather the idealized range information. These results are stored in the run baselineRun
.
converter.simulateSystem();
Create a ProposalSettings
object to control the data type proposal settings and specify tolerances for signals in the model.
propSettings = DataTypeWorkflow.ProposalSettings;
Specify a relative tolerance of 20% for the output signal of the PlantOutput
signal in the model.
addTolerance(propSettings, 'fxpdemo_feedback/Analog Plant', 1, 'RelTol', 2e-1);
You can view all tolerances specified for a system using the showTolerances
method.
showTolerances(propSettings)
Path Port_Index Tolerance_Type Tolerance_Value _________________________________ __________ ______________ _______________ {'fxpdemo_feedback/Analog Plant'} 1 {'RelTol'} 0.2
Propose data types for the system using the proposal settings specified in propSettings
, and the ranges stored in the baselineRun
run.
converter.proposeDataTypes(baselineRun, propSettings)
Apply data types proposed for the baselineRun
run to the model.
converter.applyDataTypes(baselineRun)
Verify that the behavior of the model using the new data types meets the tolerances specified on the proposal settings object, propSettings
. The verify
method removes the data type override and simulates the model using the updated fixed-point data types. It returns a DataTypeWorkflow.VerificationResult
object.
result = verify(converter, baselineRun, 'FixedRun')
result = VerificationResult with properties: ScenarioResults: [0x0 DataTypeWorkflow.VerificationResult] RunName: 'FixedRun' BaselineRunName: 'Ranges(Double)' Status: 'Pass' MaxDifference: 0.0351
Using the explore
method of the DataTypeWorkflow.VerificationResult
object, open the Simulation Data Inspector and examine the signals for which you specified a tolerance.
explore(result)