Propose Data Types Based on Simulation Ranges
This example shows how to propose fixed-point
data types based on simulation range data using the fiaccel
function.
Prerequisites
To complete this example, you must install the following products:
MATLAB®
Fixed-Point Designer™
C compiler
See Supported Compilers.
You can use
mex -setup
to change the default compiler. See Change Default Compiler.
Create a New Folder and Copy Relevant Files
Create a local working folder, for example,
c:\ex_2ndOrder_filter
.Change to the
docroot\toolbox\fixpoint\examples
folder. At the MATLAB command line, enter:cd(fullfile(docroot, 'toolbox', 'fixpoint', 'examples'))
Copy the
ex_2ndOrder_filter.m
andex_2ndOrder_filter_test.m
files 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.
Type Name Description Function code ex_2ndOrder_filter.m
Entry-point MATLAB function Test file ex_2ndOrder_filter_test.m
MATLAB script that tests ex_2ndOrder_filter.m
The ex_2ndOrder_filter Function
The ex_2ndOrder_filter_test Script
Set Up the Fixed-Point Configuration Object
Create a fixed-point configuration object and configure the test file name.
cfg = coder.config('fixpt'); cfg.TestBenchName = 'ex_2ndOrder_filter_test';
Collect Simulation Ranges and Generate Fixed-Point Code
Use the fiaccel
function to convert the floating-point MATLAB function, ex_2ndOrder_filter
,
to fixed-point MATLAB code. Set the default
word length for the fixed-point data types to 16.
cfg.ComputeSimulationRanges = true; cfg.DefaultWordLength = 16; % Derive ranges and generate fixed-point code fiaccel -float2fixed cfg ex_2ndOrder_filter
fiaccel
analyzes the floating-point
code. Because you did not specify the input types for the ex_2ndOrder_filter
function,
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
report.
View Range Information
Click the link to the type proposal report for the ex_2ndOrder_filter
function, ex_2ndOrder_filter_report.html
.
The report opens in a web browser.
View Generated Fixed-Point MATLAB Code
fiaccel
generates a fixed-point version
of the ex_2ndOrder_filter.m
function, ex_2ndOrder_filter_fixpt.m
,
and a wrapper function that calls ex_2ndOrder_filter_fixpt
.
These files are generated in the codegen\ex_2ndOrder_filter\fixpt
folder
in your local working folder.
function y = ex_2ndOrder_filter_fixpt(x) %#codegen fm = get_fimath(); persistent z if isempty(z) z = fi(zeros(2,1), 1, 16, 15, fm); end % [b,a] = butter(2, 0.25) b = fi([0.0976310729378175, 0.195262145875635, 0.0976310729378175], 0, 16, 18, fm); a = fi([ 1, -0.942809041582063, 0.3333333333333333], 1, 16, 14, fm); y = fi(zeros(size(x)), 1, 16, 14, fm); for i=1:length(x) y(i) = b(1)*x(i) + z(1); z(1) = fi_signed(b(2)*x(i) + z(2)) - a(2) * y(i); z(2) = fi_signed(b(3)*x(i)) - a(3) * y(i); end end function y = fi_signed(a) coder.inline( 'always' ); if isfi( a ) && ~(issigned( a )) nt = numerictype( a ); new_nt = numerictype( 1, nt.WordLength + 1, nt.FractionLength ); y = fi( a, new_nt, fimath( a ) ); else y = a; end end function fm = get_fimath() fm = fimath('RoundingMethod', 'Floor', 'OverflowAction', 'Wrap', 'ProductMode', 'FullPrecision', 'MaxProductWordLength', 128, 'SumMode', 'FullPrecision', 'MaxSumWordLength', 128); end