前言
在对数字信号进行解调或者滤波的时候,经常会使用均值滤波器。均值滤波器也是一种低通滤波器,均值滤波器能够使信号变得更加平滑,有效地减小噪声对信号的影响。使用HLS或者FPGA来实现均值滤波器都是比较简单的,在这篇博客中就使用HLS来完成均值滤波器的设计。
1 HLS设计均值滤波器
1.1 滤波器实现模块
均值滤波器的设计比较简单,就是将输入的数据进行延时,累加之后求一个平均值就可以了。均值滤波器的设计跟FIR滤波器的设计其实是一样的。
void mean_filter(ap_int<32> firData, ap_int<32> *meanOut)
{
#pragma HLS INTERFACE ap_ctrl_none port=return
#pragma HLS PIPELINE
#pragma HLS INTERFACE axis register both port=meanOut
#pragma HLS INTERFACE axis register both port=firData
static ap_int<32> shiftReg[32] = {0}; // FIR N stage
ap_int<36> sum = 0; // FIR Sum value
for(int i = 15; i >= 0; i--) // For loop: Delay, Multiply and Sum
{
#pragma HLS PIPELINE rewind
if(i == 0)
{
shiftReg[0] = firData;
sum += shiftReg[0] ;
}
else
{
shiftReg[i] = shiftReg[i - 1];
sum += shiftReg[i];
}
}
*meanOut = sum.range(35, 4); // Get the final result
}
HLS仿真文件
HLS仿真文件实现的是首先通过ask调制模块,将信号采用ASK的方式来进行调制,调制完成之后,经过解调模块。解调模块中通过包络检波地方式得到信号地包络。最终将包络给到均值滤波模块,完成对包络的平滑处理。
#include "../src/mean_filter.h"
const ap_int<16> coe[32] = {13 ,45 ,103 ,207 ,377 ,627 ,963 ,1385 ,
1879 ,2426 ,2994 ,3549 ,4052 ,4467 ,4764 ,4918 ,
4918 ,4764 ,4467 ,4052 ,3549 ,2994 ,2426 ,1879 ,
1385 ,963 ,627 ,377 ,207 ,103 ,45 ,13 ,};
void tx_ask(ap_uint<1> symbol, ap_int<12> *sin, ap_int<12> *cos)
{
// counter for lookup table
static ap_int<8> counter = 0;
// lut store the wave
ap_int<12> sin_table[32] = {0 ,50 ,98 ,142 ,180 ,212 ,236 ,250 ,
255 ,250 ,236 ,212 ,180 ,142 ,98 ,50 ,
0 ,-50 ,-98 ,-142 ,-180 ,-212 ,-236 ,-250 ,
-255 ,-250 ,-236 ,-212 ,-180 ,-142 ,-98 ,-50 };
ap_int<12> cos_table[32] = {255 ,250 ,236 ,212 ,180 ,142 ,98 ,50 ,
0 ,-50 ,-98 ,-142 ,-180 ,-212 ,-236 ,-250 ,
-255 ,-250 ,-236 ,-212 ,-180 ,-142 ,-98 ,-50 ,
0 ,50 ,98 ,142 ,180 ,212 ,236 ,250 };
// ASK modulation
if(symbol == 1)
{
*sin = sin_table[counter.range(4, 0)];
*cos = cos_table[counter.range(4, 0)];
}
else
{
*sin = 0;
*cos = 0;
}
// counter self increase
counter += 1;
// clear the counter
if(counter == 32)
{
counter = 0;
}
}
void rx_ask(ap_int<12> data_in, ap_int<32> *firOut)
{
static ap_int<12> shiftReg[32] = {0}; // FIR N stage
ap_int<32> sum = 0; // FIR Sum value
ap_uint<12> abs_din = 0;
abs_din = abs(data_in);
for(int i = 31; i >= 0; i--) // For loop: Delay, Multiply and Sum
{
if(i == 0)
{
shiftReg[0] = abs_din;
sum += (ap_int<32>)(shiftReg[0] * coe[0]);
}
else
{
shiftReg[i] = shiftReg[i - 1];
sum += (ap_int<32>)(shiftReg[i] * coe[i]);
}
}
*firOut = sum; // Get the final result
}
int main()
{
ap_uint<1> symbol[64] = {0, 1, 1, 0, 0, 0, 1, 1,
0, 1, 1, 0, 0, 0, 1, 1,
0, 1, 1, 0, 0, 0, 1, 1,
0, 1, 1, 0, 0, 0, 1, 1,
0, 1, 1, 0, 0, 0, 1, 1,
0, 1, 1, 0, 0, 0, 1, 1,
0, 1, 1, 0, 0, 0, 1, 1,
0, 1, 1, 0, 0, 0, 1, 1,};
ap_int<12> sin, cos;
ap_int<32> firOut;
ap_int<32> meanOut;
for(int i = 0; i < 64; i++)
{
for(int j = 0; j < 400; j++)
{
tx_ask(symbol[i], &sin, &cos);
rx_ask(cos, &firOut);
mean_filter(firOut, &meanOut);
}
}
return 0;
}
从最终的结果上来看,使用了均值滤波对信号进行平滑处理之后,检测到的包络比由包络检波得到的低通滤波的信号要更加地平滑。
参考:
- v3尤老师