set data types using min/max instrumentation -凯发k8网页登录
in this example, you set fixed-point data types by instrumenting matlab® code for min/max logging then use the tools to propose data types. you use the buildinstrumentedmex
function to build the mex function with instrumentation enabled, then use the showinstrumentationresults
to show instrumentation results and the clearinstrumentationresults
funciton to clear instrumentation results.
define the unit under test
the function that you convert to fixed-point in this example is a second-order direct-form 2 transposed filter. you can substitute your own function in place of this one to reproduce these steps in your own work.
function [y,z] = fi_2nd_order_df2t_filter(b,a,x,y,z) for i=1:length(x) y(i) = b(1)*x(i) z(1); z(1) = b(2)*x(i) z(2) - a(2) * y(i); z(2) = b(3)*x(i) - a(3) * y(i); end end
for a matlab® function to be instrumented, it must be suitable for code generation. for information on code generation, see the buildinstrumentedmex
reference page. a matlab® coder™ license is not required to use the buildinstrumentedmex
function.
in the function fi_2nd_order_df2t_filter
, the variables y
and z
are used as both inputs and outputs. this is an important pattern because:
you can set the data type of
y
andz
outside the function, thus allowing you to re-use the function for both fixed-point and floating-point types.the generated c code will create
y
andz
as references in the function argument list.
use design requirements to determine data types
in this example, the requirements of the design determine the data type of input x
. these requirements are signed, 16-bit, and fractional.
clearvars n = 256; x = fi(zeros(n,1),1,16,15);
the requirements of the design also determine the fixed-point math for a dsp target with a 40-bit accumulator. this example uses floor rounding and wrap on overflow to produce efficient generated code.
f = fimath('roundingmethod','floor',... 'overflowaction','wrap',... 'productmode','keeplsb',... 'productwordlength',40,... 'summode','keeplsb',... 'sumwordlength',40);
the following coefficients correspond to a second-order lowpass filter created by
[num,den] = butter(2,0.125)
the values of the coefficients influence the range of the values that will be assigned to the filter output and states.
num = [0.0299545822080925 0.0599091644161849 0.0299545822080925]; den = [1 -1.4542435862515900 0.5740619150839550];
the data type of the coefficients, determined by the requirements of the design, are specified as 16-bit word length and scaled to best-precision. to create fi
objects from constant coefficients:
1. cast the coefficients to fi
objects using the default round-to-nearest and saturate on overflow settings, which gives the coefficients better accuracy.
b = fi(num,1,16); a = fi(den,1,16);
2. attach fimath
with floor rounding and wrap on overflow settings to control arithmetic, which leads to more efficient c code.
b = setfimath(b,f); a = setfimath(a,f);
use values of the coefficients and inputs to determine data types
the values of the coefficients and values of the inputs determine the data types of output y
and state vector z
. create them with a scaled double datatype so their values will attain full range and you can identify potential overflows and propose data types.
yisd = fi(zeros(n,1),1,16,15,'datatype','scaleddouble','fimath',f); zisd = fi(zeros(2,1),1,16,15,'datatype','scaleddouble','fimath',f);
instrument the matlab function as a scaled-double mex function
to instrument the matlab® code, you create a mex function from the matlab® function using the buildinstrumentedmex
function. the inputs to buildinstrumentedmex
are the same as the inputs to , but buildinstrumentedmex
has no fi
-object restrictions. the output of buildinstrumentedmex
is a mex function with instrumentation inserted. when the mex function is run, the simulated minimum and maximum values are recorded for all named variables and intermediate values.
use the '-o'
option to name the mex function that is generated. if you do not use the '-o'
option, then the mex function is the name of the matlab® function with '_mex'
appended. you can also name the mex function the same as the matlab® function, but you need to remember that mex functions take precedence over matlab® functions and so changes to the matlab® function will not run until either the mex function is re-generated, or the mex function is deleted and cleared.
hard-code the filter coefficients into the implementation of this filter by passing them as constants to the buildinstrumentedmex
function.
buildinstrumentedmex fi_2nd_order_df2t_filter ... -o filter_scaled_double ... -args {coder.constant(b),coder.constant(a),x,yisd,zisd}
test bench with chirp input
the test bench for this system is set up to run chirp and step signals. in general, test benches for systems should cover a wide range of input signals.
the first test bench uses a chirp input. a chirp signal is a good representative input because it covers a wide range of frequencies.
t = linspace(0,1,n); % time vector from 0 to 1 second f1 = n/2; % target frequency of chirp set to nyquist xchirp = sin(pi*f1*t.^2); % linear chirp from 0 to fs/2 hz in 1 second x(:) = xchirp; % cast the chirp to fixed-point
run the instrumented mex function to record min/max values
the instrumented mex function must be run to record minimum and maximum values for that simulation run. subsequent runs accumulate the instrumentation results until they are cleared with clearinstrumentationresults
.
note that the numerator and denominator coefficients were compiled as constants so they are not provided as input to the generated mex function.
ychirp = filter_scaled_double(b,a,x,yisd,zisd);
the plot of the filtered chirp signal shows the lowpass behavior of the filter with these particular coefficients. low frequencies are passed through and higher frequencies are attenuated.
plot(t,x,'c',t,ychirp,'bo-') title('chirp') legend('input','scaled-double output')
show instrumentation results with proposed fraction lengths for chirp
the showinstrumentationresults
function displays the code generation report with instrumented values. the input to the showinstrumentationresults
function is the name of the instrumented mex function for which you wish to show results.
potential overflows are only displayed for fi
objects with scaled double data type.
this particular design is for a dsp where the word lengths are fixed, so use the -proposefl
flag to propose fraction lengths.
showinstrumentationresults filter_scaled_double -proposefl
hover over expressions or variables in the instrumented code generation report to see the simulation minimum and maximum values. in this design, the inputs fall between -1 and 1, and the values of all variables and intermediate results also fall between -1 and 1. this suggests that the data types can all be fractional (fraction length one bit less than the word length). however, this will not always be true for this function for other kinds of inputs and it is important to test many types of inputs before setting final fixed-point data types.
test bench with step input
the next test bench is run with a step input. a step input is a good representative input because it is often used to characterize the behavior of a system.
xstep = [ones(n/2,1);-ones(n/2,1)]; x(:) = xstep;
run the instrumented mex function with step input
the instrumentation results are accumulated until they are cleared with clearinstrumentationresults
.
ystep = filter_scaled_double(b,a,x,yisd,zisd); plot(t,x,'c',t,ystep,'bo-') title('step') legend('input','scaled-double output')
show accumulated instrumentation results
even though the inputs for step and chirp inputs are both full range as indicated by x
at 100 percent current range in the instrumented code generation report, the step input causes overflow while the chirp input did not. this is an illustration of the necessity to have many different inputs for your test bench. for the purposes of this example, only two inputs were used, but real test benches should be more thorough.
showinstrumentationresults filter_scaled_double -proposefl
apply proposed fixed-point properties
to prevent overflow, set proposed fixed-point properties based on the proposed fraction lengths of 14-bits for y
and z
from the instrumented code generation report.
at this point in the workflow, you use true fixed-point types (as opposed to the scaled double types that were used in the earlier step of determining data types).
yi = fi(zeros(n,1),1,16,14,'fimath',f); zi = fi(zeros(2,1),1,16,14,'fimath',f);
instrument the matlab function as a fixed-point mex function
create an instrumented fixed-point mex function by using fixed-point inputs and the buildinstrumentedmex
function.
buildinstrumentedmex fi_2nd_order_df2t_filter ... -o filter_fixed_point ... -args {coder.constant(b),coder.constant(a),x,yi,zi}
validate the fixed-point algorithm
after converting to fixed-point, run the test bench again with fixed-point inputs to validate the design.
validate with chirp input
run the fixed-point algorithm with a chirp input to validate the design.
x(:) = xchirp; [y,z] = filter_fixed_point(b,a,x,yi,zi); [ysd,zsd] = filter_scaled_double(b,a,x,yisd,zisd); err = double(y) - double(ysd);
compare the fixed-point outputs to the scaled-double outputs to verify that they meet your design criteria.
subplot(211); plot(t,x,'c',t,ysd,'bo-',t,y,'mx') xlabel('time (s)'); ylabel('amplitude') legend('input','scaled-double output','fixed-point output'); title('fixed-point chirp') subplot(212); plot(t,err,'r');title('error');xlabel('t'); ylabel('err');
inspect the variables and intermediate results to ensure that the min/max values are within range.
showinstrumentationresults filter_fixed_point
validate with step inputs
run the fixed-point algorithm with a step input to validate the design.
run the following code to clear the previous instrumentation results to see only the effects of running the step input.
clearinstrumentationresults filter_fixed_point
run the step input through the fixed-point filter and compare with the output of the scaled double filter.
x(:) = xstep; [y,z] = filter_fixed_point(b,a,x,yi,zi); [ysd,zsd] = filter_scaled_double(b,a,x,yisd,zisd); err = double(y) - double(ysd);
plot the fixed-point outputs against the scaled-double outputs to verify that they meet your design criteria.
subplot(211); plot(t,x,'c',t,ysd,'bo-',t,y,'mx') title('fixed-point step'); legend('input','scaled-double output','fixed-point output') subplot(212); plot(t,err,'r');title('error');xlabel('t'); ylabel('err');
inspect the variables and intermediate results to ensure that the min/max values are within range.
showinstrumentationresults filter_fixed_point
suppress code analyzer warnings.
%#ok<*asglu>