Code covered by the BSD License

# Spline based function approximation for HDL synthesis

### Sean Little (view profile)

This is a suite of simple utilities that allow for efficient approximation of complex functions in a

example
function example
% demonstrates the use of the HDL function approximation utilities.
%
% Sean Little
% Copyright 2010 The MathWorks, Inc.

% suppose I needed an efficient approximation for the EXP function that is
% compatible with the HDL coder. I could do something like the following:

% declare the function
fcn=@(x) exp(-x.^2);

% declare the range over which the function will be defined (this is
% practical because actual fixed point implementations have a finite
% precision)
rng=[0,5.5];

% declare the tolerance (maximum error over the given interval) for the
% approximation
tol=1e-5;

% interactively get the approximation (note that you will have to hit
% <enter> several times to move through the approximation iterations)
fprintf('you will need to hit <enter> several times...\n');
figure();
exp_table=[table.breaks(2:end)', table.coefs];

% write the lookup table EML code
word_size=32;
write_lookup(table, word_size, 'exp_lookup.m');

% compute the fixed point sizes for the lookup table
fractional=compute_fp_tables(word_size, exp_table);

% convert to fixed point and output the data for use in Simulink
fsz=min(fractional);
% fsz=ones(1,size(fractional,2))*min(fractional(:));
F=hdlfimath();
assignin('base', 'bp', fi(exp_table(:,1), numerictype(1,word_size,fsz(1)), F));
for i=2:size(exp_table,2)
assignin('base', sprintf('c%d',i-1),...
fi(exp_table(:,i), numerictype(1,word_size,fsz(i)), F));
end

% assign other required parameters for the test model
assignin('base','wl',word_size);
assignin('base','fl',28);