作者:桂。

时间:2018-02-06  12:10:14

链接:http://www.cnblogs.com/xingshansi/p/8421001.html


前言

本文主要记录基本的FIR实现,以及相关的知识点。

 一、基本型实现

首先从最基本的FIR入手:

对应module:

`default_nettype   none
//
module  smplfir(i_clk, i_ce, i_val, o_val);parameter            IW=15;localparam           OW=IW+1;input wire            i_clk, i_ce;input   wire    [(IW-1):0]  i_val;output    reg [(OW-1):0]  o_val;reg   [(IW-1):0]  delayed;initial delayed = 0;always @(posedge i_clk)if (i_ce)delayed <= i_val;always @(posedge i_clk)if (i_ce)o_val <= i_val + delayed;endmodule

  

二、通用版FIR

  前文里最多涉及阶数为5的FIR,这里给出适用任意阶、给定位宽的FIR。

  A-参数转化

vivado仿真用到浮点->定点,需要将给定数据转为定点补码、或通过补码读取数据。

1)浮点转定点补码

clc;clear all;close all;
%=============产生输入信号==============%
N=12;           %数据位宽
load fir128.mat;
y_n = fir128;
y_n=round(y_n*(2^(N-3)-1));      %N比特量化;如果有n个信号相加,则设置(N-n)
%=============设置系统参数==============%
L=length(y_n);         %数据长度
%=================画图==================%
stem(1:L,y_n);
%=============写入外部文件==============%
fid=fopen('win.txt','w');    %把数据写入sin_data.txt文件中,如果没有就创建该文件
for k=1:length(y_n)B_s=dec2bin(y_n(k)+((y_n(k))<0)*2^N,N);for j=1:Nif B_s(j)=='1'tb=1;elsetb=0;endfprintf(fid,'%d',tb);endfprintf(fid,'\r\n');
endfprintf(fid,';');
fclose(fid);

  原型滤波器fir128为128阶的FIR滤波器。

生成的txt调用:$readmemb("*.txt",data);

2)给定补码,读取原数据:

clc;clear all;close all;
filename = 'win.txt';
fid = fopen(filename);
data_cell = textscan(fid,'%s','HeaderLines',0);
data = data_cell{1,1};Nbit = 12;%number of bits
len = length(data)-1;%length of filter
wins = zeros(1,len);
for i = 1:lenstr_win = data{i};if (str_win(1) == '0')wins(i) = bin2dec(str_win(2:end));endif (str_win(1) == '1')wins(i) = -bin2dec(num2str(ones(1,Nbit-1)))+bin2dec(str_win(2:end));end
end
wvtool(wins)

  得到滤波器特性如下图所示,当然也可以hex2dec转为16进制,思路一致。

  B-仿真模型

testbench:

`timescale 1ns / 1ps
module tb;// Inputsreg Clk;reg rst;// Outputsparameter datawidth = 12;wire signed [2*datawidth-1:0] Yout;//Generate a clock with 10 ns clock period.
initial  Clk <= 0;always #5 Clk = ~Clk;//Initialize and apply the inputs.
//-------------------------------------//
parameter data_num = 32'd1024;
integer   i = 0;
reg [datawidth-1:0]  Xin[1:data_num];
reg  [datawidth-1:0]  data_out;initial beginrst = 1;
#20rst = 0;$readmemb("D:/PRJ/vivado/simulation_ding/009_lpf6tap/matlab/sin_data.txt",Xin);
endalways @(posedge Clk) beginif(rst)begindata_out <= 0;endelse   begindata_out <= Xin[i];i <= i + 8'd1;end
end   fastfir firinst(
.i_clk(Clk),
.i_reset(rst),
.i_ce(1'b1),
.i_sample(data_out),
.o_result(Yout)
);
endmodule

  fast.v:

//
`default_nettype   none
//
module  fastfir(i_clk, i_reset, i_ce, i_sample, o_result);parameter     NTAPS=127, IW=12, TW=IW, OW=2*IW+7;input   wire            i_clk, i_reset;//input  wire            i_ce;input  wire    [(IW-1):0]  i_sample;output wire signed [(2*IW-1):0]    o_result;reg    [(TW-1):0] tap      [0:NTAPS];wire  [(TW-1):0] tapout   [NTAPS:0];wire  [(IW-1):0] sample   [NTAPS:0];wire  [(OW-1):0] result   [NTAPS:0];wire      tap_wr;// The first sample in our sample chain is the sample we are givenassign sample[0]   = i_sample;// Initialize the partial summing accumulator with zeroassign   result[0]   = 0;//observe filterreg [IW-1:0] fir_coef;integer i = 0;always @(posedge i_clk)beginif(i_reset) fir_coef <= 0;elsebeginfir_coef <= tap[i];i <= i+ 8'd1;end endgenvar   k;generatebegininitial $readmemb("D:/PRJ/vivado/simulation_ding/009_lpf6tap/matlab/win.txt", tap);assign  tap_wr = 1'b1;endfor(k=0; k<NTAPS; k=k+1)begin: FILTERfirtap #(.FIXED_TAPS(1'b1),.IW(IW), .OW(OW), .TW(TW),.INITIAL_VALUE(0))tapk(.i_clk(i_clk), .i_reset(i_reset), .i_tap_wr(tap_wr), .i_tap( tap[k]), .o_tap(tapout[k+1]),.i_ce(i_ce), .i_sample(sample[0]), .o_sample(sample[k+1]),.i_partial_acc(result[k]), .o_acc( result[k+1]));end endgenerateassign    o_result = result[NTAPS][2*IW-1:0];endmodule

  firtap.v:

//
`default_nettype   none
//
module  firtap(i_clk, i_reset, i_tap_wr, i_tap, o_tap,i_ce, i_sample, o_sample,i_partial_acc, o_acc);parameter      IW=12, TW=IW, OW=IW+TW+8;parameter [0:0]       FIXED_TAPS=1;parameter [(TW-1):0]  INITIAL_VALUE=0;//input    wire            i_clk, i_reset;//input  wire            i_tap_wr;input  wire    [(TW-1):0]  i_tap;output    wire signed [(TW-1):0]  o_tap;//input   wire            i_ce;input  wire signed [(IW-1):0]  i_sample;output reg     [(IW-1):0]  o_sample;//input    wire    [(OW-1):0]  i_partial_acc;output    reg     [(OW-1):0]  o_acc;//reg     [(IW-1):0]  delayed_sample;reg  signed  [(TW+IW-1):0]  product;// Determine the tap we are usinggenerateif (FIXED_TAPS != 0)// If our taps are fixed, the tap is given by the i_tap// external input.  This allows the parent module to be// able to use readmemh to set all of the taps in a filterassign    o_tap = i_tap;else begin// If the taps are adjustable, then use the i_tap_wr signal// to know when to adjust the tap.  In this case, taps are// strung together through the filter structure--our output// tap becomes the input tap of the next tap module, and// i_tap_wr causes all of them to shift forward by one.reg [(TW-1):0]  tap;initial tap = INITIAL_VALUE;always @(posedge i_clk)if (i_tap_wr)tap <= i_tap;assign o_tap = tap;end endgenerate// Forward the sample on down the line, to be the input sample for the// next componentalways @(posedge i_clk)if (i_reset)begindelayed_sample <= 0;o_sample <= 0;end else if (i_ce)begin// Note the two sample delay in this forwarding// structure.  This aligns the inputs up so that the// accumulator structure (below) works.delayed_sample <= i_sample;o_sample <= delayed_sample;end// Multiply the filter tap by the incoming samplealways @(posedge i_clk)if (i_reset)product <= 0;else if (i_ce)product <= o_tap * i_sample;// Continue summing together the output components of the FIR filteralways @(posedge i_clk)if (i_reset)o_acc <= 0;else if (i_ce)o_acc <= i_partial_acc+ { {(OW-(TW+IW)){product[(TW+IW-1)]}},product };// Make verilator happy// verilate lint_on  UNUSEDwire unused;assign   unused = i_tap_wr;// verilate lint_off UNUSED
endmodule

仿真结果:

转载于:https://www.cnblogs.com/xingshansi/p/8421001.html

FIR仿真module_04相关推荐

  1. quartus FIR仿真笔记

    第一章: 最近百度了一些fir滤波器的资料,都没有自己想要的.容我吐槽一大段文字> 在旧版的quartus中,比如13.0,有两个fir滤波器的选项,如下所示: 网上很多都是讲不带II的那个,而 ...

  2. FIR特性及仿真实现_01

    作者:桂. 时间:2018-02-05  19:01:21 链接:http://www.cnblogs.com/xingshansi/p/8419007.html 前言 本文主要记录FIR(finit ...

  3. FIR调用DSP48E_05

    作者:桂. 时间:2018-02-06  17:52:38 链接:http://www.cnblogs.com/xingshansi/p/8423457.html 前言 到目前为止,本文没有对滤波器实 ...

  4. 语音合成的思路、语音的声学特征、声音采样的一些资料

    语音合成:把语音波形文件重现,以一种灵活的方式,只用极少数的基础数据,比如元音辅音的语音参数,那么首先需要研究元音辅音的语音学性质. 先从元音开始,根据相关资料,不同的元音是由相同的原始声带音通过不同 ...

  5. (多图) 基于Verilog HDL的FIR数字滤波器设计与仿真

    引言:数字滤波器是语音与图像处理.模式识别.雷达信号处理.频谱分析等应用中的一种基本的处理部件,它能满足波器对幅度和相位特性的严格要求,避免模拟滤波器所无法克服的电压漂移.温度漂移和噪声等问题.有限冲 ...

  6. 【 FPGA 】MATLAB 生成 FIR 滤波器的操作步骤(包括生成Verilog HDL代码以及仿真过程)

    使用MATLAB生成滤波器有很多学问,这里只是作为初步的探索,和FPGA的更多结合,也正在探索中,相关博文例如:[ FPGA ]FIR滤波器目录,该专题目录正在记录我学习FIR滤波器的过程. MATL ...

  7. (多图) 基于FPGA的FIR数字滤波器设计与仿真

    1 引言 实现数字化是控制系统的重要发展方向,而数字信号处理已在通信.语音.图像.自动控制.雷达.军事.航空航天等领域广泛应用.数字信号处理方法通常涉及变换.滤波.频谱分析.编码解码等处理.数字滤波是 ...

  8. FIR设置过采样率 matlab,Xilinx FIR IP的介绍与仿真

    作者: OpenSLee 来源: 1 xilinx fir ip简介 1)符合AXI4-Stream的接口 2)高性能有限脉冲响应(FIR),多相抽取器,多相内插器,半带,半带抽取器和半带内插器,希尔 ...

  9. 基于matlab的语音信号去噪毕业论文,MATLAB的FIR数字滤波器语音信号的去噪研究和仿真...

    摘要:语音信号作为人类语言交流的重要手段,语音处理的质量直接影响人们的正常通信.本文基于MATLAB对语音信号及加噪信号进行时域和频域分析,设计了FIR数字滤波器,完成对加噪信号滤波的处理.结果表明设 ...

最新文章

  1. INS-20802 PRVF-9802 PRVF-5184 PRVF-5186 After Successful Upgradeto 11gR2 Grid Infrastructure
  2. 深度学习如何验证自己的想法
  3. Google App Engine使用简介
  4. linux字符处理工具 新手教程
  5. Android之在一个类里面注册Handler发送消息在另外一个类里面接收消息
  6. tcp抓包返回fin_TCP/IP学习二TCP链接建立与断开
  7. 前端学习(2601):什么是跨域请求
  8. 新东方在线战略亏损:扩张提速or高层动荡?
  9. 报 java.lang.ExceptionInInitializerError 的常见解决方法
  10. 讨论:癌症能被人类攻克吗
  11. 2015蓝桥杯C++A:饮料换购
  12. 线程池原理_JAVA并发编程:详解线程池的工作原理
  13. python微信语音转发方法_涨知识,微信语音能转发给别人啊,方法还那么简单
  14. 怎么备考2022年改革后的PMP考试?
  15. 华为开发者学堂 | 囤课畅学 码住未来
  16. 软件项目文档及其必要性
  17. 设置linux默认音频设备,ubuntu设置默认声音设备
  18. VMWare IOS MAC分区教程
  19. C# 获取文件名和扩展名(后缀名)
  20. 前沿计算技术于推动设计技术发展

热门文章

  1. Python(basic)Day-1
  2. 两个点击事件共用一个方法_工作必技:教你简单方法一个电脑开两个,多个微信!...
  3. SpringBoot与jackson.databind兼容报错问题
  4. scala的多种集合的使用(6)之映射Map的操作方法
  5. 第 7 章 异常处理结构、代码测试与调试
  6. zzuli 2177 Contest - 河南省多校连萌(四)(简单题)
  7. Codeforces Round #420 E
  8. nodejs+vue.js+webpack
  9. 关于监听UITextField的问题
  10. python使用xlrd模块读写excel