Generated FixedPoint Code
Location of Generated FixedPoint Files
By default, the fixedpoint conversion process generates files
in a folder named codegen/fcn_name/fixpt
in your
local working folder. fcn_name
is the name of
the MATLAB^{®} function that you are converting to fixed point.
File name  Description 

fcn_name_fixpt.m  Generated fixedpoint MATLAB code. To integrate this fixedpoint code into a larger application, consider generating a MEXfunction for the function and calling this MEXfunction in place of the original MATLAB code. 
fcn_name_fixpt_exVal.mat  MATfile containing:

fcn_name_fixpt_report.html  Link to the type proposal report that displays the generated fixedpoint code and the proposed type information. 
fcn_name_report.html  Link to the type proposal report that displays the original MATLAB code and the proposed type information. 
fcn_name_wrapper_fixpt.m  File that converts the floatingpoint data values supplied
by the test file to the fixedpoint types determined for the inputs
during the conversion step. These fixedpoint values are fed into
the converted fixedpoint function, 
Minimizing fi
casts to Improve Code Readability
The conversion process tries to reduce the number of fi
casts
by analyzing the floatingpoint code. If an arithmetic operation is
comprised of only compiletime constants, the conversion process does
not cast the operands to fixed point individually. Instead, it casts
the entire expression to fixed point.
For example, here is the fixedpoint code generated for the
constant expression x = 1/sqrt(2)
when the selected
word length is 14.
Original MATLAB Code  Generated FixedPoint Code 

x = 1/sqrt(2);  x = fi(1/sqrt(2), 0, 14, 14, fm);

Avoiding Overflows in the Generated FixedPoint Code
The conversion process avoids overflows by:
Using fullprecision arithmetic unless you specify otherwise.
Avoiding arithmetic operations that involve double and
fi
data types. Otherwise, if the word length of thefi
data type is not able to represent the value in the double constant expression, overflows occur.Avoiding overflows when adding and subtracting non fixedpoint variables and fixedpoint variables.
The fixedpoint conversion process casts non
fi
expressions to the correspondingfi
type.For example, consider the following MATLAB algorithm.
% A = 5; % B = ones(300, 1) function y = fi_plus_non_fi(A, B) % '1024' is nonfi, cast it y = A + 1024; % 'size(B, 1)*length(A)' is a nonfi, cast it y = A + size(B, 1)*length(A); end
The generated fixedpoint code is:
%#codegen % A = 5; % B = ones(300,1) function y = fi_plus_non_fi_fixpt(A,B) % '1024' is nonfi, cast it fm = fimath('RoundingMethod','Floor',... 'OverflowAction','Wrap',... 'ProductMode','FullPrecision',... 'MaxProductWordLength',128,... 'SumMode','FullPrecision',... 'MaxSumWordLength',128); y = fi(A + fi(1024,0,11,0,fm),0,11,0,fm); % 'size(B, 1)*length(A)' is a nonfi, cast it y(:) = A + fi(size(B,fi(1,0,1,0,fm))*length(A),0,9,0,fm); end
Controlling Bit Growth
The conversion process controls bit growth by using subscripted assignments, that is, assignments that use the colon (:) operator, in the generated code. When you use subscripted assignments, MATLAB overwrites the value of the lefthand side argument but retains the existing data type and array size. Using subscripted assignment keeps fixedpoint variables fixed point rather than inadvertently turning them into doubles. Maintaining the fixedpoint type reduces the number of type declarations in the generated code. Subscripted assignment also prevents bit growth which is useful when you want to maintain a particular data type for the output.
Avoiding Loss of Range or Precision
Avoiding Loss of Range or Precision in Unsigned Subtraction Operations
When the result of the subtraction is negative, the conversion process promotes the left operand to a signed type.
For example, consider the following MATLAB algorithm.
% A = 1; % B = 5 function [y,z] = unsigned_subtraction(A,B) y = A  B; C = 20; z = C  B; end
In the original code, both A
and B
are
unsigned and the result of AB
can be negative.
In the generated fixedpoint code, A
is promoted
to signed. In the original code, C
is signed, so
does not require promotion in the generated code.
%#codegen % A = 1; % B = 5 function [y,z] = unsigned_subtraction_fixpt(A,B) fm = fimath('RoundingMethod','Floor',... 'OverflowAction','Wrap',... 'ProductMode','FullPrecision',... 'MaxProductWordLength',128,... 'SumMode','FullPrecision',... 'MaxSumWordLength',128); y = fi(fi_signed(A)  B,1,3,0,fm); C = fi(20,1,6,0,fm); z = fi(C  B,1,6,0,fm); 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
Avoiding Loss of Range When Concatenating Arrays of FixedPoint Numbers
If you concatenate matrices using vertcat
and horzcat
,
the conversion process uses the largest numerictype among the expressions
of a row and casts the leftmost element to that type. This type is
then used for the concatenated matrix to avoid loss of range.
For example, consider the following MATLAB algorithm.
% A = 1, B = 100, C = 1000 function [y, z] = lb_node(A, B, C) %% single rows y = [A B C]; %% multiple rows z = [A 5; A B; A C]; end
In the generated fixedpoint code:
For the expression
y = [A B C]
, the leftmost element,A
, is cast to the type ofC
becauseC
has the largest type in the row.For the expression
[A 5; A B; A C]
:In the first row,
A
is cast to the type ofC
becauseC
has the largest type of the whole expression.In the second row,
A
is cast to the type ofB
becauseB
has the larger type in the row.In the third row,
A
is cast to the type ofC
becauseC
has the larger type in the row.
%#codegen % A = 1, B = 100, C = 1000 function [y,z] = lb_node_fixpt(A,B,C) %% single rows fm = fimath('RoundingMethod','Floor',... 'OverflowAction','Wrap',... 'ProductMode','FullPrecision',... 'MaxProductWordLength',128,... 'SumMode','FullPrecision',... 'MaxSumWordLength',128); y = fi([fi(A,0,10,0,fm) B C],0,10,0,fm); %% multiple rows z = fi([fi(A,0,10,0,fm) 5; ... fi(A,0,7,0,fm) B; ... fi(A,0,10,0,fm) C],0,10,0,fm); end
Handling NonConstant mpower Exponents
If the function that you are converting has a scalar input,
and the mpower
exponent input is not constant,
the conversion process sets the fimath
ProductMode
to SpecifyPrecision
in
the generated code. With this setting , the output data type can be
determined at compile time.
For example, consider the following MATLAB algorithm.
% a = 1 % b = 3 function y = exp_operator(a, b) % exponent is a constant so no need to specify precision y = a^3; % exponent is not a constant, use 'SpecifyPrecision' for 'ProductMode' y = b^a; end
In the generated fixedpoint code, for the expression y
= a^3
, the exponent is a constant, so there is no
need to specify precision. For the expression, y = b^a
,
the exponent is not constant, so the ProductMode
is
set to SpecifyPrecision
.
%#codegen % a = 1 % b = 3 function y = exp_operator_fixpt(a,b) % exponent is a constant so no need to specify precision fm = fimath('RoundingMethod','Floor',... 'OverflowAction','Wrap',... 'ProductMode','FullPrecision',... 'MaxProductWordLength',128,... 'SumMode','FullPrecision',... 'MaxSumWordLength',128); y = fi(a^3,0,2,0,fm); % exponent is not a constant, use 'SpecifyPrecision' for 'ProductMode' y(:) = fi(b,'ProductMode','SpecifyPrecision',... 'ProductWordLength',2,... 'ProductFractionLength',0)^a; end