当前位置:网站首页>【FPGA】day21-移动平均滤波器
【FPGA】day21-移动平均滤波器
2022-08-11 03:28:00 【春风浅作序】
一、实验分析
1、实验要求
请用 verilog 写一个模块,实现窗口宽度为5的移动平均滤波器。设滤波函数记为 y=avg(x)
其中输入x为一个长度为N的8bit 离散序列。输出y也为长度为N的8bit离散序列。那么该函数可以用以下伪代码表记。(代码可参考以下avg.c)
#define N 32
void avg(unsigned char* x, unsigned char* y)
{
int i, j, k;
int sum;
unsigned char avg;
for (i = 0; i < N; i++)
{
sum = 0;
for (j = -2; j <= 2; j++)
{
k = i + j;
if (k >= 0 && k < N)
sum += x[k];
}
avg = sum * 51 / 256;
y[i] = avg;
}
}
请用 verilog 实现以上函数。
提示:
- 窗口宽度为5,所以在做平均运算除以5时,用 51/256 近似 1/5,才能保证测试通过。
- 请用流水线结构实现。
2、实验设计
结构图如下:
二、项目源码
`timescale 1ns/1ps
/********************************************************** // Copyright 2022.05-2025.05 // Contact with [email protected] ================ xxx.v ====================== >> Author : lzh >> Date : >> Description : 实现滑动平均滤波,滑动窗口为5 >> note : 做了时序优化,最大能够达到150MHz >> : >> V180121 : ************************************************************/
module fir_test3 (
input clk ,
input rst_n ,
input vin , //输入有效信号
input [7:0] din , //输入
output reg vout , //输出有效信号
output reg [7:0] dout //输出
);
//信号定义
reg vin_ff0 ;
reg vin_ff1 ;
reg vin_ff2 ;
reg vin_ff3 ;
reg vin_ff4 ;
reg vin_ff5 ;
reg [7:0] din_ff0 ;
reg [7:0] din_ff1 ;
reg [7:0] din_ff2 ;
reg [7:0] din_ff3 ;
reg [7:0] din_ff4 ;
wire [10:0] sum0_w0 ; //第一级流水
wire [10:0] sum0_w1 ;
reg [10:0] sum0_r0 ;
reg [10:0] sum0_r1 ;
reg [10:0] sum0_r2 ;
wire [10:0] sum1_w0 ; //第二级流水
reg [10:0] sum1_r0 ;
wire [15:0] prod_w0 ; //第三级流水线
reg [15:0] prod_r0 ;
wire [11:0] ave_w0 ; //第四级输出
reg [11:0] ave_r0 ;
//打拍
always @(posedge clk or negedge rst_n) begin
if(!rst_n)begin
din_ff0 <= 1'b0;
din_ff1 <= 1'b0;
din_ff2 <= 1'b0;
din_ff3 <= 1'b0;
din_ff4 <= 1'b0;
vin_ff5 <= 1'b0;
end
else if(vin)begin
din_ff0 <= din ;
din_ff1 <= din_ff0;
din_ff2 <= din_ff1;
din_ff3 <= din_ff2;
din_ff4 <= din_ff3;
vin_ff5 <= vin_ff4;
end
else begin
din_ff0 <= 0 ;
din_ff1 <= din_ff0;
din_ff2 <= din_ff1;
din_ff3 <= din_ff2;
din_ff4 <= din_ff3;
vin_ff5 <= vin_ff4;
end
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n)begin
vin_ff0 <= 1'b0;
vin_ff1 <= 1'b0;
vin_ff2 <= 1'b0;
vin_ff3 <= 1'b0;
vin_ff4 <= 1'b0;
end
else begin
vin_ff0 <= vin ;
vin_ff1 <= vin_ff0;
vin_ff2 <= vin_ff1;
vin_ff3 <= vin_ff2;
vin_ff4 <= vin_ff3;
end
end
assign sum0_w0 = din_ff0 + din_ff1; //第一级流水
assign sum0_w1 = din_ff2 + din_ff3;
always @(posedge clk or negedge rst_n) begin
if(!rst_n)begin
sum0_r0 <= 1'b0;
sum0_r1 <= 1'b0;
sum0_r2 <= 1'b0;
end
else if(vin_ff2)begin
sum0_r0 <= sum0_w0;
sum0_r1 <= sum0_w1;
sum0_r2 <= din_ff4;
end
end
assign sum1_w0 = sum0_r0 + sum0_r1 + sum0_r2; //第二级流水
always @(posedge clk or negedge rst_n) begin
if(!rst_n)begin
sum1_r0 <= 1'b0;
end
else if(vin_ff3)begin
sum1_r0 <= sum1_w0;
end
end
assign prod_w0 = sum1_r0*51; //第三级计算乘积
always @(posedge clk or negedge rst_n) begin
if(!rst_n)begin
prod_r0 <= 1'b0;
end
else if(vin_ff4) begin
prod_r0 <= prod_w0;
end
end
assign ave_w0 = prod_r0>>8; //第四级计算均值,并输出结果
always @(posedge clk or negedge rst_n) begin
if(!rst_n)begin
dout <= 1'b0;
end
else if(vin_ff5)begin
dout <= ave_w0;
end
end
always @(posedge clk or negedge rst_n) begin
if(!rst_n)begin
vout <= 1'b0;
end
else begin
vout <= vin_ff5;
end
end
endmodule
边栏推荐
- CSDN blog replacement skin
- 按摩椅控制板的开发让按摩椅变得简约智能
- En-us is an invalid culture error solution when Docker links sqlserver
- CTO说MySQL单表行数不要超过2000w,为啥?
- Idea (preferred) cherry-pick operation
- typedef定义结构体数组类型
- this question in js
- Detailed explanation of VIT source code
- "Beijing-Taiwan high-speed rail" debuted on Baidu map, can it really be built in 2035?
- oracle的基数会影响到查询速度吗?
猜你喜欢

font

E-commerce project - mall time-limited seckill function system

音视频开发,为什么要学习FFmpeg?应该怎么入手FFmpeg学习?

The problem that Merge will be lost again after code Revert has been solved

互换性与测量技术——表面粗糙度选取和标注方法

What should I do if the channel ServerID is incorrect when EasyCVR is connected to a Hikvision Dahua device and selects another cluster server?

A Practical Arrangement of Map GIS Development Matters (Part 1)
![[BX]和loop](/img/3c/1be08db6898613c3a1c0bcd39e73be.png)
[BX]和loop

A practice arrangement about map GIS (below) GIS practice of Redis

多商户商城系统功能拆解26讲-平台端分销设置
随机推荐
阿里低代码框架 lowcode-engine 之自定义物料篇
【Yugong Series】August 2022 Go Teaching Course 036-Type Assertion
"Beijing-Taiwan high-speed rail" debuted on Baidu map, can it really be built in 2035?
多串口RS485工业网关BL110
Element's BFC attribute
How can users overcome emotional issues in programmatic trading?
索引的创建、查看、删除
荣威imax8ev魔方电池安全感,背后隐藏着哪些黑化膨胀?
你不知道的 console.log 替代品
UNI-APP_iphone苹果手机底部安全区域
How does MSP430 download programs to the board?(IAR MSPFET CCS)
En-us is an invalid culture error solution when Docker links sqlserver
console.log alternatives you didn't know about
JS-DOM element object
"Life Is Like First Seen" is ill-fated, full of characters, and the contrast of Zhu Yawen's characters is too surprising
The "top pillar" slides, and new growth is extremely difficult to shoulder the heavy responsibility. Is Ali "squatting" to jump higher?
Rotary array problem: how to realize the array "overall reverse, internal orderly"?"Three-step conversion method" wonderful array
Qnet弱网测试工具操作指南
Ten Advanced Concepts of SQL Development
The 125th day of starting a business - a note