This example shows how to propose fixed-point
data types based on static ranges using the
fiaccel function. The advantage of
proposing data types based on derived ranges is that you do not have
to provide test files that exercise your algorithm over its full operating
range. Running such test files often takes a very long time so you
can save time by deriving ranges instead.
Derived range analysis is not supported for non-scalar variables.
To complete this example, you must install the following products:
Create a local working folder, for example,
Change to the
At the MATLAB command line, enter:
cd(fullfile(docroot, 'toolbox', 'fixpoint', 'examples'))
to your local working folder.
It is best practice to create a separate test script to do all the pre- and post-processing such as loading inputs, setting up input values, calling the function under test, and outputting test results.
|Function code||Entry-point MATLAB function|
|Test file||MATLAB script that tests |
Create a fixed-point configuration object and configure the test file name.
fixptcfg = coder.config('fixpt'); fixptcfg.TestBenchName = 'dti_test';
Specify design range information for the
fixptcfg.addDesignRangeSpecification('dti', 'u_in', -1.0, 1.0)
Select to run the test file to verify the generated fixed-point MATLAB code. Log inputs and outputs for comparison plotting and select to use the Simulation Data Inspector to plot the results.
fixptcfg.TestNumerics = true; fixptcfg.LogIOForComparisonPlotting = true; fixptcfg.PlotWithSimulationDataInspector = true;
fiaccel function to convert the floating-point MATLAB function,
to fixed-point MATLAB code. Set the default
word length for the fixed-point data types to 16.
fixptcfg.ComputeDerivedRanges = true; fixptcfg.ComputeSimulationRanges = false; fixptcfg.DefaultWordLength = 16; % Derive ranges and generate fixed-point code fiaccel -float2fixed fixptcfg dti
fiaccel analyzes the floating-point
code. Because you did not specify the input types for the
the conversion process infers types by simulating the test file. The
conversion process then derives ranges for variables in the algorithm.
It uses these derived ranges to propose fixed-point types for these
variables. When the conversion is complete, it generates a type proposal
Click the link to the type proposal report for the
The report opens in a web browser.
fiaccel generates a fixed-point version
and a wrapper function that calls
files are generated in the
in your local working folder.
function [y, clip_status] = dti_fixpt(u_in) %#codegen % Discrete Time Integrator in MATLAB % % Forward Euler method, also known as Forward Rectangular, or left-hand % approximation. The resulting expression for the output of the block at % step 'n' is y(n) = y(n-1) + K * u(n-1) % fm = get_fimath(); init_val = fi(1, 0, 1, 0, fm); gain_val = fi(1, 0, 1, 0, fm); limit_upper = fi(500, 0, 9, 0, fm); limit_lower = fi(-500, 1, 10, 0, fm); % variable to hold state between consecutive calls to this block persistent u_state; if isempty(u_state) u_state = fi(init_val+fi(1, 0, 1, 0, fm), 1, 16, 6, fm); end % Compute Output if (u_state > limit_upper) y = fi(limit_upper, 1, 16, 6, fm); clip_status = fi(-2, 1, 16, 13, fm); elseif (u_state >= limit_upper) y = fi(limit_upper, 1, 16, 6, fm); clip_status = fi(-1, 1, 16, 13, fm); elseif (u_state < limit_lower) y = fi(limit_lower, 1, 16, 6, fm); clip_status = fi(2, 1, 16, 13, fm); elseif (u_state <= limit_lower) y = fi(limit_lower, 1, 16, 6, fm); clip_status = fi(1, 1, 16, 13, fm); else y = fi(u_state, 1, 16, 6, fm); clip_status = fi(0, 1, 16, 13, fm); end % Update State tprod = fi(gain_val * u_in, 1, 16, 14, fm); u_state(:) = y + tprod; end function fm = get_fimath() fm = fimath('RoundingMethod', 'Floor', 'OverflowAction', 'Wrap', 'ProductMode', 'FullPrecision', 'MaxProductWordLength', 128, 'SumMode', 'FullPrecision', 'MaxSumWordLength', 128); end
Because you selected to log inputs and outputs for comparison plots and to use the Simulation Data Inspector for these plots, the Simulation Data Inspector opens.
You can use the Simulation Data Inspector to view floating-point
and fixed-point run information and compare results. For example,
to compare the floating-point and fixed-point values for the output
on the Compare tab, select
and then click Compare Runs.
The Simulation Data Inspector displays a plot of the baseline floating-point run against the fixed-point run and the difference between them.