detect overflows
this example shows how to detect overflows using the fixed-point converter app. at the numerical testing stage in the conversion process, you choose to simulate the fixed-point code using scaled doubles. the app then reports which expressions in the generated code produce values that overflow the fixed-point data type.
prerequisites
this example requires the following products:
matlab®
fixed-point designer™
c compiler
see .
you can use
mex -setup
to change the default compiler. see change default compiler.
create a new folder and copy relevant files
in a local, writable folder, create a function
overflow.m
.function y = overflow(b,x,reset) if nargin<3, reset = true; end persistent z p if isempty(z) || reset p = 0; z = zeros(size(b)); end [y,z,p] = fir_filter(b,x,z,p); end function [y,z,p] = fir_filter(b,x,z,p) y = zeros(size(x)); nx = length(x); nb = length(b); for n = 1:nx p=p 1; if p>nb, p=1; end z(p) = x(n); acc = 0; k = p; for j=1:nb acc = acc b(j)*z(k); k=k-1; if k<1, k=nb; end end y(n) = acc; end end
create a test file,
overflow_test.m
, to exercise theoverflow
algorithm. you use this test file to define input types forb
,x
, andreset
, and, later, to verify the fixed-point version of the algorithm.function overflow_test % the filter coefficients were computed % using the fir1 function from % signal processing toolbox. % b = fir1(11,0.25); b = [-0.004465461051254 -0.004324228005260 0.012676739550326 0.074351188907780 0.172173206073645 0.249588554524763 0.249588554524763 0.172173206073645 0.074351188907780 0.012676739550326 -0.004324228005260 -0.004465461051254]'; % input signal nx = 256; t = linspace(0,10*pi,nx)'; % impulse x_impulse = zeros(nx,1); x_impulse(1) = 1; % max gain % the maximum gain of a filter will occur when the % inputs line up with the signs of the filter's % impulse response. x_max_gain = sign(b)'; x_max_gain = repmat(x_max_gain,ceil(nx/length(b)),1); x_max_gain = x_max_gain(1:nx); % sums of sines f0=0.1; f1=2; x_sines = sin(2*pi*t*f0) 0.1*sin(2*pi*t*f1); % chirp f_chirp = 1/16; % target frequency x_chirp = sin(pi*f_chirp*t.^2); % linear chirp x = [x_impulse,x_max_gain,x_sines,x_chirp]; titles = {'impulse','max gain','sum of sines','chirp'}; y = zeros(size(x)); for i=1:size(x,2) reset = true; y(:,i) = overflow(b,x(:,i),reset); end test_plot(1,titles,t,x,y) end function test_plot(fig,titles,t,x,y1) figure(fig) clf sub_plot = 1; font_size = 10; for i=1:size(x,2) subplot(4,1,sub_plot) sub_plot = sub_plot 1; plot(t,x(:,i),'c',t,y1(:,i),'k') axis('tight') xlabel('t','fontsize',font_size); title(titles{i},'fontsize',font_size); ax = gca; ax.fontsize = 10; end figure(gcf) end
it is a best practice is to create a separate test script to do pre- and post-processing, such as:
loading inputs.
setting up input values.
outputting test results.
for more information, see create a test file.
type | name | description |
---|---|---|
function code | overflow.m | entry-point matlab function |
test file | overflow_test.m | matlab script that tests
overflow.m |
open the fixed-point converter app
navigate to the work folder that contains the file for this example.
on the matlab toolstrip apps tab, under code generation, click the app icon.
select source files
to add the entry-point function
overflow
to the project, browse to the fileoverflow.m
, and then click open. by default, the app saves information and settings for this project in the current folder in a file namedoverflow.prj
.click next to go to the define input types step.
the app screens
overflow.m
for code violations and fixed-point conversion readiness issues. the app does not find issues inoverflow.m
.
define input types
on the define input types page, to add
overflow_test
as a test file, browse tooverflow_test.m
, and then click open.click autodefine input types.
the test file runs. the app determines from the test file that the input type of
b
isdouble(1x12)
,x
isdouble(256x1)
, andreset
islogical(1x1)
.click next to go to the convert to fixed point step.
convert to fixed point
the app generates an instrumented mex function for your entry-point matlab function. the app displays compiled information — type, size, and complexity — for variables in your code. for more information, see .
on the function replacements tab the app displays functions that are not supported for fixed-point conversion. see .
to view the fimath settings, click the settings arrow . set the fimath product mode and sum mode to
keeplsb
. these settings model the behavior of integer operations in the c language.click analyze.
the test file,
overflow_test
, runs. the app displays simulation minimum and maximum ranges on the variables tab. using the simulation range data, the software proposes fixed-point types for each variable based on the default type proposal settings, and displays them in the proposed type column.to convert the floating-point algorithm to fixed point, click convert.
the software validates the proposed types and generates a fixed-point version of the entry-point function.
if errors and warnings occur during validation, the app displays them on the output tab. see .
test numerics and check for overflows
click the test arrow . verify that the test file is
overflow_test.m
. select use scaled doubles to detect overflows, and then click test.the app runs the test file that you used to define input types to test the fixed-point matlab code. because you selected to detect overflows, it also runs the simulation using scaled double versions of the proposed fixed-point types. scaled doubles store their data in double-precision floating-point, so they carry out arithmetic in full range. because they retain their fixed-point settings, they can report when a computation goes out of the range of the fixed-point type.
the simulation runs. the app detects an overflow. the app reports the overflow on the overflow tab. to highlight the expression that overflowed, click the overflow.
determine whether it was the sum or the multiplication that overflowed.
in the fimath settings, set product mode to
fullprecision
, and then repeat the conversion and test the fixed-point code again.the overflow still occurs, indicating that it is the addition in the expression that is overflowing.