本博客是用于自己学习总结,禁止转载。
有错误欢迎私聊指出,共同进步。
三、编程题
[findpeak RTL实现]
findpeak函数为matlab中的求矩阵极值点函数,其中极值定义: 若当前值比相邻的两个点都大,则当前点为极值,要求使用RTL实现fndpeak函数;当数据串行输入时,可以找出其中最大3个极值点。
备注:1、din_last_vld为当前数据串最后一个数据有效信号;2、不存在的极值保持为0;
module find3peak
#(
parameter DWIDTH=8
)
(
input clk,
input rst_n,
input [DWIDTH-1:0] din;
input din_vld,
input din_last_vld,
output reg [DWIDTH-1:0] dout_1st, //最大极值
output reg [DWIDTH-1:0] dout_2nd, //第二大极值
output reg [DWIDTH-1:0] dout_3rd, //第三大极值
output reg dout_vld
);
//---------------------------------------------------
//待完善
//--------------------------------------------------
endmodule
/*
findpeaks函数作用是在数据序列中找到局部极大值;原理是判断该点值大于相邻两点值。
*/
module find3peak #(parameter DWIDTH = 8)
(
input clk,
input rst_n,
input [DWIDTH-1:0] din,
input din_vld,
input din_last_vld,
output reg [DWIDTH-1:0] dout_1st,
output reg [DWIDTH-1:0] dout_2nd,
output reg [DWIDTH-1:0] dout_3rd,
output reg dout_vld
);
reg [7:0] prev_din;
reg [7:0] peak1,peak2,peak3;
always @(posedge clk) begin
if(!rst_n) begin
peak1 <= 8'h0;
peak2 <= 8'h0;
peak3 <= 8'h0;
end else begin
if(din_vld && prev_din<din && din>din_last_vld) begin
if(din > peak1) begin
peak3 <= peak2;
peak2 <= peak1;
peak1 <= din;
end else if(din > peak2) begin
peak3 <= peak2;
peak2 <= din;
end else if(din > peak3) begin
peak3 <= din;
end
end
prev_din <= din;
if(din_last_vld && !dout_vld) begin
dout_1st <= peak1;
dout_2nd <= peak2;
dout_3rd <= peak3;
dout_vld <= 1'b1;
end
end
end
endmodule