MATLAB生成函数的模块化HDL代码
这个例子展示了如何从包含函数的MATLAB代码中生成模块化HDL代码。
默认情况下,HDL Coder内联所有在顶层设计函数体中调用的MATLAB函数体。这种内联导致生成单个文件,其中包含函数的HDL代码。要生成模块化HDL代码,请使用生成可实例化代码进行函数设置。当您启用此设置时,HDL Coder为每个功能生成单个VHDL实体或Verilog或SystemVerilog模块。
一、LMS滤波器的MATLAB设计
示例中使用的MATLAB设计是LMS(最小均方)滤波器的实现。
LMS滤波器是一类自适应滤波器,用于识别嵌入噪声中的FIR滤波器信号。MATLAB中的LMS滤波器设计实现由顶层函数mlhdlc_lms_fcn组成,该函数计算最优滤波器系数以减小输出信号与期望信号之间的差异。
design_name = ‘mlhdlc_lms_fcn’;
testbench_name = ‘mlhdlc_lms_fir_id_tb’;
1、MATLAB代码设计
design_name = ‘mlhdlc_lms_fcn’;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% MATLAB Design: Adaptive Noise Canceler algorithm using Least Mean Square
% (LMS) filter implemented in MATLAB
%
% Key Design pattern covered in this example:
% (1) Use of function calls
% (2) Function inlining vs instantiation knobs available in the coder
% (3) Use of system objects in the testbench to stream test vectors into the design
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%#codegen
function [filtered_signal, y, fc] = mlhdlc_lms_fcn(input, ...
desired, step_size, reset_weights)
% 'input' : The signal from Exterior Mic which records the ambient noise.
% 'desired': The signal from Pilot's Mic which includes
% original music signal and the noise signal
% 'err_sig': The difference between the 'desired' and the filtered 'input'
% It represents the estimated music signal (output of this block)
%
% The LMS filter is trying to retrieve the original music signal('err_sig')
% from Pilot's Mic by filtering the Exterior Mic's signal and using it to
% cancel the noise in Pilot's Mic. The coefficients/weights of the filter
% are updated(adapted) in real-time based on 'input' and 'err_sig'.
% register filter coefficients
persistent filter_coeff;
if isempty(filter_coeff)
filter_coeff = zeros(1, 40);
end
% Variable Filter: Call 'mtapped_delay_fcn' function on path to create
% 40-step tapped delay
delayed_signal = mtapped_delay_fcn(input);
% Apply filter coefficients
weight_applied = delayed_signal .* filter_coeff;
% Call treesum function on matlab path to sum up the results
filtered_signal = mtreesum_fcn(weight_applied);
% Output estimated Original Signal
td = desired;
tf = filtered_signal;
esig = td - tf;
y = esig;
% Update Weights: Call 'update_weight_fcn' function on MATLAB path to
% calculate the new weights
updated_weight = update_weight_fcn(step_size, esig, delayed_signal, ...
filter_coeff, reset_weights);
% update filter coeffi