首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

Vivado HLS之滤波器实现

Vivado HLS之滤波器实现

环境:win7 64 vivado 2014.1
开发板:zedboard version d xc7z020clg484-1
目标:使用HLS创建一个FIR滤波器,并对其源程序、HLS优化等进行分析。

说明:文本在参考官网资料的基础上整理而成。
注意:本文中所有的源码、工程文件在“我的资源”中可以找到,如果没有请联系作者本人。转载请注明出处。
正文:
本文将分为以下步骤:
1. 使用HLS建一个工程,添加源文件,进行功能测试
2. fir原理介绍和源程序分析
3. 导出IP
4. 总结
1. 使用HLS建一个工程,添加源文件,进行功能测试
1)建立HLS工程。可以选择GUI的方式进行创建,详见ug871;本文选择tcl的方式。
运行Vivado HLS 2014.1 Command Prompt,使用cd命令切换到工程目录(即包含“我的资源”中“run_hls.tcl”的目录,也可以自己重新创建)。
打开后,可以使用dir命令查看是否包含此文件。
键入:vivado_hls -f run_hls.tcl
此时会自动创建一个HLS工程并出现GUI界面。如果没有出现,继续键入:vivado_hls –p fir_prj即可


2)此时可以查看源文件(fir.c)和testbench(fir_test.c)文件,还可以看到out.gold.dat文件,此文件为标准的输出结果,用于对比验证。
注意,fir.c中的函数名fir即为将要生成的IP的名称,必须相同
3)选择run c Simulation进行逻辑验证。此步骤可以验证程序的语法、功能等问题。但是调试功能不强,因此建议最好先在外面验证好代码的正确性,再在HLS中进行操作。
验证后,出现下图,说明验证成功。


2. fir原理介绍和源程序分析
4)fir原理介绍








5)fir.c程序分析
[cpp] view plaincopy
#include "fir.h"

void fir (
data_t *y,
coef_t c[N],
data_t x
) {
#pragma HLS INTERFACE ap_vld port=y

#pragma HLS INTERFACE ap_vld port=x
#pragma HLS RESOURCE variable=c core=RAM_1P_BRAM //for array,generally BRAM
static data_t shift_reg[N];
#pragma HLS ARRAY_PARTITION variable=shift_reg complete dim=1
// Creates an additional valid port (
_vld) to operate in conjunction with this data port
// For input ports, a read stalls the function until its associated input valid port is asserted.
// An output port has its output valid signal asserted when it writes data
acc_t acc;
data_t data;
int i;acc=0;
Shift_Accum_Loop:
for (i=N-1;i>=0;i--)
{
#pragma HLS UNROLL

if (i==0)
{
shift_reg[0]=x;
data = x;
} else
{
shift_reg=shift_reg[i-1];
data = shift_reg;
}
acc+=data*c;;
}
*y=acc;
}
这里给出了fir数据存储的图,最新的数据将把最老的数据挤出,然后缓冲区的数据和系数矩阵相乘加和得到一个输出结果。



如果上图不是很清晰,博主给出了开始自己的分析图,很傻却能说明问题:


6)fir_test.c程序分析
testbench用于测试fir模块的正确性。本例选取的600个采样点进行测试,数据为:从0到+75,再到-75,再到+75,再到-75,以此类推,间隔为1.把fir的结果和一个标准结果文件out.gold.dat进行比较,如果相同,说明程序正确。
[cpp] view plaincopy
#include
#include
#include "fir.h"

int main () {
const int SAMPLES=600; //number of samples
FILE *fp;

data_t signal, output;
coef_t taps[N] = {0,-10,-9,23,56,63,56,23,-9,-10,0,}; //

int i, ramp_up;
signal = 0;
ramp_up = 1;

fp=fopen("out.dat","w");
for (i=0;i<=SAMPLES;i++) //signal is: 0-(+75)-(-75)-(+75)-(-75)-...step=1
{
if (ramp_up == 1)
signal = signal + 1;
else
signal = signal - 1;

// Execute the function with latest input
fir(&output,taps,signal);

if ((ramp_up == 1) && (signal >= 75))
ramp_up = 0;
else if ((ramp_up == 0) && (signal <= -75))
ramp_up = 1;

// Save the results.
fprintf(fp,"%i %d %d\n",i,signal,output);
}
fclose(fp);

printf ("Comparing against output data \n"); //compare the standard output and the result
if (system("diff -w out.dat out.gold.dat")) {

fprintf(stdout, "*******************************************\n");
fprintf(stdout, "FAIL: Output DOES NOT match the golden output\n");
fprintf(stdout, "*******************************************\n");
return 1;
} else {
fprintf(stdout, "*******************************************\n");
fprintf(stdout, "PASS: The output matches the golden output!\n");
fprintf(stdout, "*******************************************\n");
return 0;
}
}

3. 导出IP
选择export RTL,导出格式选择IP Catalog。



4. 总结.
使用HLS设计了fir滤波器的IP,此IP可以使用,具体方法可见前面博文提到的LAB56,在vivado的IP catalog中添加此IP,然后在设计时选用即可。
记录学习中的点点滴滴,让每一天过的更加有意义!
返回列表