滑动平均值滤波的VERILOG实现

滑动平均值滤波的VERILOG实现
滑动平均值滤波是指先在RAM中建⽴⼀个数据缓冲区,依顺序存放N个采样数据,每采进⼀个新数据,就将最早采集的那个数据丢掉,⽽后求包括新数据在内的N个数据的算术平均值或加权平均值。这样,每进⾏⼀次采样,就可计算出⼀个新的平均值,从⽽加快了数据处理的速度。
滑动平均值滤波程序设计的关键是:每采样⼀次,移动⼀次数据块,然后求出新⼀组数据之和,再求平均值。滑动平均值滤波程序有两种,⼀种是滑动算术平均值滤波,⼀种是滑动滤波。不管是算术平均值滤波,还是加权平均值滤波,都需连续采样N个数据,然后求算术平均值或加权平均值。
上述两段⽂字来⾃百度。这⾥做的是算术平均值滤波。以下代码来⾃openofdm的moving_avg.v⽂件,就是严格按照这个思路写的。
module moving_avg
#(
parameter DATA_WIDTH = 32,
parameter WINDOW_SHIFT = 4,
parameter SIGNED = 0
)
(
input clock,
input enable,
input reset,
input signed [DATA_WIDTH-1:0] data_in,
input input_strobe,
output reg signed [DATA_WIDTH-1:0] data_out,
output reg output_strobe
);
localparam WINDOW_SIZE = 1<<WINDOW_SHIFT;
localparam SUM_WIDTH = DATA_WIDTH + WINDOW_SHIFT;
reg signed [(SUM_WIDTH-1):0] running_sum;
wire signed [DATA_WIDTH-1:0] old_data;
wire signed [DATA_WIDTH-1:0] new_data = data_in;
wire signed [SUM_WIDTH-1:0] ext_old_data = {{WINDOW_SHIFT{old_data[DATA_WIDTH-1]}}, old_data};
wire signed [SUM_WIDTH-1:0] ext_new_data = {{WINDOW_SHIFT{new_data[DATA_WIDTH-1]}}, new_data};
reg [WINDOW_SHIFT-1:0] addr;
reg full;
ram_2port  #(.DWIDTH(DATA_WIDTH), .AWIDTH(WINDOW_SHIFT)) delay_line (
.clka(clock),
.ena(1),
.wea(input_strobe),
.addra(addr),
.dia(data_in),
.doa(),
.clkb(clock),
.enb(input_strobe),
.web(0),网购蛇被咬进ICU
.addrb(addr),
.dib(32'hFFFF),
.
dob(old_data)
);
integer i;
always @(posedge clock) begin
if (reset) begin
if (reset) begin
addr <= 0;
running_sum <= 0;
full <= 0;
data_out <= 0;
end else if (enable) begin
if (input_strobe) begin闭式引流
addr <= addr + 1;
data_out <= running_sum[SUM_WIDTH-1:WINDOW_SHIFT];
if (addr == WINDOW_SIZE-1) begin
full <= 1;
end
if (full) begin
running_sum <= running_sum + ext_new_data- ext_old_data;
end else begin
running_sum <= running_sum + ext_new_data;
end
output_strobe <= full;
end else begin
output_strobe <= 0;
end
end else begin
output_strobe <= 0;
end
end
endmodule
这⾥我们sum记录了ram⾥⾯的所存的前WORD_SIZE-1个数值的算术和。当到达这些位数之后,每输⼊⼀位就有⼀位溢出(或者说被移除),溢出的序号正好等于输⼊的那个序号,好让位该空间⽤于存储。可见这⾥要求ram_2port读的延迟必须是⼀个周期,写的延迟呢,应该是⼩于WINDOW_SHIFT就
好,WINDOWS_SHIFT最⼩可以是2,那么写延迟最⼩可以是1.因此我们也就实现⼀级写延迟。⼀级读写延迟的RAM_2PORT实现代码如下:
//
// Copyright 2011 Ettus Research LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see </licenses/>.
//
module ram_2port
#(
parameter DWIDTH=32,
parameter AWIDTH=9
)
(
input clka,
input ena,
input wea,
input [AWIDTH-1:0] addra,
input [AWIDTH-1:0] addra,
input [DWIDTH-1:0] dia,
output reg [DWIDTH-1:0] doa,
input clkb,
input enb,
input web,
input [AWIDTH-1:0] addrb,
input [DWIDTH-1:0] dib,
output reg [DWIDTH-1:0] dob
);
reg [DWIDTH-1:0] ram [(1<<AWIDTH)-1:0];
integer      i;
initial begin
for(i=0;i<(1<<AWIDTH);i=i+1)
ram[i] <= {DWIDTH{1'b0}};
doa <= 0;
dob <= 0;
end
always @(posedge clka) begin
if (ena)
begin
if (wea)
ram[addra] <= dia;
doa <= ram[addra];
digital chaosend锚钉
end
always @(posedge clkb) begin
if (enb)
双重危机
begin
if (web)
ram[addrb] <= dib;
dob <= ram[addrb];
end
end
endmodule // ram_2port
这份RAM_2PORT代码来⾃ETTUS的USRP产品的源码,被OPENOFDM引⽤。
作为效果我看看看仿真图
这是在openofdm⾥⾯内嵌的⼀个4平均值的滑动滤波器效果,整个就是openofdm正常实例仿真的数据。我们看到蓝⾊输出相对与绿⾊的输⼊确实平滑了很多。
这⾥这个32位4平均数值的模块实现过于⿇烦了,⼀⼤堆寄存器和⼀个双⼝RAM。其实对于2,4,8,16这些2的数次⽅的特例是有可以⽤另外使⽤移位寄存器。思路我⽤代码简单写写核⼼部分如下:狸御殿
reg [31:0] r0,r1,r2,r3;
reg [31+2:0]sum;
always @ (posedge clk ) if (en) {r3,r2,r1,r0}<={r2,r1,r0,sig_in} ;
always @ (posedge clk )sum <= r3+r2+r1+r0 ;
assign sig_out = sum [31+2:2] ;
这⾥注意输出实际是左移动2位后的sum,也就是sum除以4。如果要滑动5位就是能这样左移位了,在verilog⾥⾯实现除以5的代价远不是加减肥那样简单,所以要再次评估实现⽅式了。
如果需要out_storbe信号,就将in_strobe和sig_in⼀起按照时序移动就好。
话回来,现在FPGA都资源冗余,具体⼯程中也没有必要在实现细节上死磕资源。具体使⽤时候可以根据情况权衡实现⽅式。

本文发布于:2024-09-21 17:26:49,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/328678.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:平均值   数据   实现   滤波
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议