您的位置:首页 > 编程语言 > MATLAB

使用MATLAB中的HDL coder工具箱,对一个线性调频信号进行FIR滤波,最终在FPGA上实现

2017-11-24 23:24 1571 查看

HDL coder帮助文档的解读

HDL Coder

Generate VHDL and Verilogcode for FPGA and ASIC designs
HDL Coder™ generates portable, synthesizable VHDL® andVerilog® code
from MATLAB® functions,Simulink® models, and Stateflowcharts.
The generated HDL code can be used for FPGA programming orASIC prototyping and design.
HDL Coder provides a workflow advisor that automates theprogramming of Xilinx® andAltera® FPGAs.
You can control HDLarchitecture and implementation, highlight critical paths, and generatehardware resource utilization estimates. HDL Coder provides traceabilitybetween your Simulink model and the generated Verilog and VHDL code, enablingcode verification
for high-integrity applications adhering to DO-254 and otherstandards.
针对FPGA与ASIC设计生成VHDL/Verilog代码
HDLCoder™可基于MATLAB函数、Simulink模型和Stateflow图表来生成可移植、可综合的VHDL/Verilog代码。生成出的代码可以用于FPGA的编程或ASIC原型开发与设计。
HDL Coder提供了一种使Xilinx和Altera
FPGA编程自动化的工作流程指导。你可以控制硬件描述语言的架构和执行,强调关键(信号)路径,生成硬件资源率利用评估。HDL
Coder提供了Simulink模型与生成的VHDL/Verilog代码间的可追溯特性,为符合DO-254和其他标准的高完整性的应用启用代码验证。
注:【DO-254标准的有效性得到了美国联邦航空局和欧洲航空安全局等业界权威的一致确认,为机载电子硬件的设计开发提供了指导与保障。DO-254全称“机载电子硬件设计保证指南”。该标准对硬件设计生命周期各阶段的目标、开展的设计保证活动以及产生的设计数据进行了详尽的阐述。DO-254标准将硬件设计过程分为需求获取、概要设计、详细设计、实现与产品转换过程,这与传统的集成电路设计流程基本相同,不同的是,DO-254详细定义了支持过程,包括确认过程、验证过程、配置管理、过程保证以及审定联络,保证了硬件设计生命周期及其输出正确可控。DO-254标准代表了工业界、适航当局、机载领域硬件设计人员在内的大多数专家的一致意见,是机载电子硬件开发保证过程最好的实践经验的集合。DO-254标准的有效性得到了美国联邦航空局和欧洲航空安全局等业界权威的一致确认,为机载电子硬件的设计开发提供了指导与保障。
可编程逻辑器件在航空机载设备中的应用越来越多,其设计也变得愈发复杂,适航认证已成为航空器件发展的必然趋势。对于针对特种行业的FPGA开发,了解此标准很重要】

Key Features

·       Target-independent,synthesizable VHDL and Verilog code
与目标(器件)无关,可综合

·       Codegeneration support for MATLAB functions, System objects and Simulink blocks

·       Mealyand Moore finite-state machines and control logic implementations usingStateflow

<
12c15
span style="color:rgb(64,64,64);">使用Stateflow工具实现Moore/Mealy状态机及控制逻辑
·       Workflowadvisor for programming Xilinx and Altera application boards

·       Resourcesharing and retiming for area-speed tradeoffs
平衡面积——速度的资源共享与时序重排技术

·       Code-to-modeland model-to-code traceability for DO-254

·       Legacycode integration
旧代码集成

Getting Started

Learn thebasics of HDL Coder
入门指南:学习HDL Coder的基础

HDL Code Generation from MATLAB

GenerateHDL Code from MATLAB algorithms
基于MATLAB算法的HDL代码生成

HDL Code Generation from Simulink

GenerateHDL code from Simulink models
基于Simulink模块的HDL代码生成

Hardware-Software Codesign

Deploypartitioned hardware and software on a target hardware platform
软硬件协同设计:在目标硬件平台上有效部署软件与硬件

Supported Hardware

Supportfor third-party hardware, such as Altera and Xilinx FPGA boards
硬件支持:对例如Xilinx和Altera
FPGA板卡的第三方硬件支持

Getting Started with HDL Coder

·       HDL
CoderProduct Description HDL Coder产品描述
·       System Requirements
系统要求
·       Tool Setup工具安装
·       SupportedThird-Party
Tools and Hardware 受支持的第三方工具与硬件
·       VHDL
and VerilogLanguage Support VHDL与Verilog语言支持
 

Tutorials

·       HDL
CodeGeneration from a MATLAB Algorithm
·       HDL
CodeGeneration from a Simulink Model

About Designing for HDLCode Generation

·       FunctionsSupported
for HDL Code Generation — Alphabetical List 按字母排序的受支持函数清单
·       FunctionsSupported
for HDL Code Generation — Categorical List 按类别的受支持函数清单
·       MATLAB
LanguageSupport MATLAB语言支持
·       HardwareModeling
with MATLAB Code 使用MATLAB代码对硬件进行建模
·       Prepare
SimulinkModel For HDL Code Generation 准备适合生成HDL代码的Simulink模型
·       Show
BlocksSupported for HDL Code Generation
·       Supported Blocks受支持的模块
·       SimulinkTemplates
For HDL Code Generation 可生成HDL代码的Simulink模板
Tool Setup工具安装流程
Synthesis Tool Path Setup
综合工具路径配置

·       hdlsetuptoolpath
Function
·       Add
Synthesis Tool for Current HDL Workflow AdvisorSession
·       Check
Your Synthesis Tool Setup
·       Supported
Tool Versions
hdlsetuptoolpath Function
使用hdlsetuptoolpath 函数

To use HDL Coder™ with one of the supported third-party FPGAsynthesis tools, add the tool to your system path using the hdlsetuptoolpath function.Add
the tool to your system path before opening the HDL Workflow Advisor. Ifyou already have the HDL Workflow Advisor open, see Add
Synthesis Tool for Current HDL Workflow AdvisorSession.
Add Synthesis Tool for Current HDL WorkflowAdvisor Session
Simulink to HDL Workflow
1.     At the MATLAB® commandline, use the hdlsetuptoolpath functionto
add the synthesis tool.
2.     In the HDL Workflow Advisor, inthe Set Target > Set Target Device and SynthesisTool step, to the right of
Synthesis tool, click Refresh.
Thesynthesis tool is now available.
MATLAB to HDL Workflow
1.     At the MATLAB command line, usethe hdlsetuptoolpath functionto
add the synthesis tool.
2.     In the HDL Workflow Advisor, inthe Select Code Generation Target step, to the right of Synthesistool, click
Refresh list.
Thesynthesis tool is now available.
Check Your Synthesis Tool Setup
To check your Altera® Quartussynthesis tool setup in MATLAB, try launching the tool with the followingcommand:
!quartus

To check your Xilinx® Vivado® synthesistool
setup in MATLAB, try launching the tool with the following command:
!vivado

To check your Xilinx ISE synthesis tool setup in MATLAB, trylaunching the tool with the following command:
!ise

Supported Tool Versions
For supported tool versions, see Third-Party
Synthesis Tools and Version Support.



如上图所示,通过调用函数成功配置了VIVADO综合器接口。通过!vivado指令可以检查出MATLAB与VIVADO是否完成综合器的匹配

开始HDL coder生成流程

现在开始在.m文件中编写相应的算法。这里为了更好的演示整个HDL coder工作的流程,我们选择一个具有代表性的数字信号处理典例:线性调频LFM信号通过FIR滤波器进行滤波,并通过Testbench来从输入输出的波形中验证FPGA是否能较好的完成滤波工作。

 

首先值得注意的是,我们平时在MATLAB环境中的运算基本上都是浮点运算(floating point arithmetic),而在FPGA中的实现几乎都是定点运算(fixed
point arithmetic),这就使得在HDL coder的过程中需要一步关键操作:

浮点到定点的转换

 

我们先写出FIR滤波器算法的.m文件,在这个文件中,加法和乘法都是直接使用+、*运算符,属于浮点操作:

%#codegen

function [y_out, delayed_xout] =mlhdlc_sfir(x_in, h_in1, h_in2, h_in3, h_in4)

% Symmetric FIR Filter

 

persistent ud1 ud2 ud3 ud4 ud5 ud6ud7 ud8;

if isempty(ud1)

    ud1 = 0;ud2 = 0; ud3 = 0; ud4 = 0; ud5 = 0; ud6 = 0; ud7 = 0; ud8 = 0;

end

 

a1 = ud1 + ud8; a2 = ud2 + ud7;

a3 = ud3 + ud6; a4 = ud4 + ud5;

 

m1 = h_in1 * a1; m2 = h_in2 * a2;

m3 = h_in3 * a3; m4 = h_in4 * a4;

 

a5 = m1 + m2; a6 = m3 + m4;

% filtered output

y_out = a5 + a6;

% delayout input signal

delayed_xout = ud8;

 

% update the delay line

ud8 = ud7;

ud7 = ud6;

ud6 = ud5;

ud5 = ud4;

ud4 = ud3;

ud3 = ud2;

ud2 = ud1;

ud1 = x_in;

 

end

 

以及测试FIR滤波器用的_tb.m文件:

clear all;

% input signal with noise

x_in =cos(2.*pi.*(0:0.001:2).*(1+(0:0.001:2).*75)).';

% filter coefficients

h1 = -0.1339; h2 = -0.0838; h3 = 0.2026; h4 =0.4064;

len = length(x_in);

y_out = zeros(1,len);

x_out = zeros(1,len);

for ii=1:len

    data =x_in(ii);

    %call to the design 'mlhdlc_sfir' that is targeted for hardware

   [y_out(ii), x_out(ii)] = mlhdlc_sfir(data, h1, h2, h3, h4);   

end

figure('Name',[mfilename,
'_plot']);

subplot(2,1,1); plot(1:len,x_in);

subplot(2,1,2); plot(1:len,y_out);

 

_tb.m文件的作用是产生一个线性调频信号x_in输入给FIR滤波器,得到它的输出,并作图来显示滤波的效果。

 

执行_tb.m文件的结果如下:



可见,经过滤波后的线性调频信号的高频部分被抑制了,说明在算法层面我们的代码没有问题。下一步就是调出HDL coder工具箱,然后按说明添加两个.m文件进去:





按照MATLAB中内置的Getting Start Guide,一步步进行HDL的生成。在第二步中,Fixed
Point Conversion是将浮点算法转换为定点实现的重要一环:



可以看到我们代码中的运算形式发生了改变:

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%                                                                                     

%          Generated by MATLAB 9.0 and Fixed-Point Designer 5.2             

%                                                                                    

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%   #codegen

function [y_out, delayed_xout] =mlhdlc_sfir_fixpt(x_in, h_in1, h_in2, h_in3, h_in4)

% Symmetric FIR Filter

 

fm = get_fimath();

 

persistent ud1 ud2 ud3 ud4 ud5 ud6ud7 ud8;

if isempty(ud1)

ud1 = fi(0, 1, 14, 12, fm); ud2 = fi(0, 1, 14, 12,fm); ud3 = fi(0, 1, 14, 12, fm); ud4 = fi(0, 1, 14, 12, fm); ud5 = fi(0, 1, 14,12, fm); ud6 = fi(0, 1, 14, 12, fm); ud7 = fi(0, 1, 14, 12, fm); ud8 = fi(0, 1,14, 12, fm);

end

 

a1 = fi(ud1 + ud8, 1, 14, 12, fm); a2 = fi(ud2 +ud7, 1, 14, 12, fm);

a3 = fi(ud3 + ud6, 1, 14, 12, fm); a4 = fi(ud4 +ud5, 1, 14, 11, fm);

 

m1 = fi(h_in1 * a1, 1, 14, 14, fm); m2 = fi(h_in2 *a2, 1, 14, 15, fm);

m3 = fi(h_in3 * a3, 1, 14, 14, fm); m4 = fi(h_in4 *a4, 1, 14, 13, fm);

 

a5 = fi(m1 + m2, 1, 14, 14, fm); a6 = fi(m3 + m4, 1,14, 12, fm);

 

% filtered output

y_out = fi(a5 + a6, 1, 14, 12, fm);

% delayout input signal

delayed_xout = fi(ud8, 1, 14, 12, fm);

 

% update the delay line

ud8(:) = ud7;

ud7(:) = ud6;

ud6(:) = ud5;

ud5(:) = ud4;

ud4(:) = ud3;

ud3(:) = ud2;

ud2(:) = ud1;

ud1(:) = x_in;

 

end

 

 

function fm = get_fimath()

    fm =fimath('RoundingMethod',
'Floor',...

         'OverflowAction',
'Wrap',...

         'ProductMode','FullPrecision',...

         'MaxProductWordLength',128,...

         'SumMode','FullPrecision',...

         'MaxSumWordLength',128);

end




 
经过定点转换后,数据多少会在精度上有所损失,基于此可以在MATLAB中将定点转换后的算法再次运行一遍,得到的结果和浮点比较,如果精度损失过多,我们可以通过增加定点数据的存储位宽来弥补,使其更接近真实数据,但同时也会增加资源占用率。如下图所示,HDL
coder给出了定点算法运行结果与原浮点算法结果的对比,并给出了误差波形:



可以看出,在使用默认定点位宽的条件下,转换误差在级别,对大部分应用可以忽略。我们继续向下执行,直到这一步,生成得到两个.v文件和用于测试的三个信号数据.dat文件:



最终生成的Code Generation报告:



我们建立一个VIVADO工程,把生成的.v文件加入到DesignSource、Simulation
Source中,同时把三个测试用的信号数据.dat文件加入,VIVADO会自动识别出并放置到Data
Files文件夹里:



一路综合、实现:





然后在VIVADO中仿真,检查我们HDL coder生成的.v文件在FPGA中是否可以真正实现FIR滤波器功能。在这里为了显示正确的信号波形,我们应该选择Radix为Signed
Decimal,Waveform Style为Analog形式,此时才可以看清输入输出的线性调频信号波形





最终如仿真图所示,经过对仿真信号的格式做适当修改,可以看到FIR滤波器在FPGA中成功实现了:



HDL coder同时可以针对某特定型号的FPGA芯片进行深度优化,实现更多更有意思的算法,从雷达、通信系统的数字信号处理到矩阵运算,再到ECC、加解密编码甚至机器学习领域,它都可以作为一种快速生成原型的验证工具,帮助工程师快速将算法转化为工程实现。本文仅仅是在原版官方说明的拙劣解释和模仿,如有需要可以深入研究MATLAB关于此的详细说明,也希望读者能随时指正本文的错误,交流学习
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐