Main Content

Handling Constants in HDL and HLS Code Generation

When generating HDL or High-Level Synthesis (HLS) code from MATLAB®, handling constants efficiently is important for optimizing performance and resource utilization. You can do this by using the functions coder.const and coder.load.

Specify Constants in Generated Code

To optimize the code, specify the constants in the generated code by using coder.const.

In the table, the MATLAB code consists of two functions, filter_lowpass and calc_lowpass. The filter_lowpass function applies a low-pass filter to an input sample in and outputs a single value representing the current filtered sample of the input signal. The calc_lowpass function calculates the filter coefficients for a low-pass filter given a normalized cutoff frequency fc.

Using coder.const in the filter_lowpass function instructs the code generator to treat the coefficients as constants.

MATLAB CodeMATLAB Test Bench
% MATLAB code
function out=filter_lowpass(in)
    persistent delayline;
    if isempty(delayline)
        delayline=zeros(1,21);
    end

    fc = 0.3;

    coefficients=coder.const(calc_lowpass(fc));
 
    delayline=[in delayline(1:20)];
    out=sum(delayline.*coefficients);
end
function coeffs=calc_lowpass(fc)
    x = (-10:10)*fc;
    % sinc and gausswin are not supported with fi datatype
    % Cast it to single or double.
    coeffs=sinc(single(x)).*gausswin(single(21)).';
    coeffs=coeffs/sum(coeffs);
end
n = 100;
t = 0:n;
signal=sin(2*pi*(t/n)/2.*(t));

filtered=zeros(size(signal));
for i=1:length(t)
    filtered(i)=filter_lowpass(signal(i));
end

Load Compile-Time Constants from MAT-file

You can also load compile-time constants from external files into your MATLAB design by using coder.load. The function coder.load loads data at compile time, not at run time.

In this example, the MATLAB code defines a low-pass filter filter_lowpass that uses coefficients loaded from a file coeffs.mat to filter an input signal. The MATLAB test bench generates the signals using calc_lowpass and then saves them in coeffs.mat.

MATLAB CodeMATLAB Test Bench
% MATLAB code
function out=filter_lowpass(in)
    persistent delayline coeffs;
    if isempty(delayline)
        % load the mat file for the coeffs
        coeffs_struct = coder.load('coeffs.mat');
        coeffs = coeffs_struct.coeffs;
        delayline=zeros(1,21);
    end
 
    delayline=[in delayline(1:20)];
    out=sum(delayline.*coeffs);
end
function coeffs=calc_lowpass(fc)
    x = (-10:10)*fc;
    coeffs=sinc(single(x)).*gausswin(single(21)).';
    coeffs=coeffs/sum(coeffs);
end
n = 100;
t = 0:n;
signal=sin(2*pi*(t/n)/2.*(t));

% create a mat file for the constant 
% coefficients from calc_lowpass function
fc = fi(0.3, 0, 14, 15, hdlfimath);
coeffs = calc_lowpass(fc);
save coeffs.mat coeffs;
 
filtered=zeros(size(signal));
for i=1:length(t)
    filtered(i)=filter_lowpass(signal(i));
end

Generate HDL or HLS Code

To generate HDL or HLS code for the above defined MATLAB functions use these commands.

fname = 'filter_lowpass';

% HLS Codegen
cfg = coder.config('hdl');
% cfg.workflow = "Generic ASIC/FPGA"; % For HDL code generation
cfg.workflow = "High Level Synthesis";
tbname = [fname,'_tb'];
cfg.TestBenchName = tbname;
 
fixptCfg = coder.config('fixpt');
fixptCfg.TestBenchName = cfg.TestBenchName;
 
outname = [fname,'_sc_fixpt'];
codegen(fname,'-config',cfg,'-report','-float2fixed',fixptCfg);