在单片机开发中,经常需要对输入的数据进行过滤处理,如传感器数据输出,AD采样等,合适的滤波处理能达到更好效果。下面分享几种较简单而常用的滤波算法:
A、方法:
B、优点:
C、缺点:
int Filter_Value;
int Value;
void setup() {
Serial.begin(9600); // 初始化串口通信
randomSeed(analogRead(0)); // 产生随机种子
Value = 300;
}
void loop() {
Filter_Value = Filter(); // 获得滤波器输出值
Value = Filter_Value; // 最近一次有效采样的值,该变量为全局变量
Serial.println(Filter_Value); // 串口输出
delay(50);
}
// 用于随机产生一个300左右的当前值
int Get_AD() {
return random(295, 305);
}
// 限幅滤波法(又称程序判断滤波法)
#define FILTER_A 1
int Filter() {
int NewValue;
NewValue = Get_AD();
if(((NewValue - Value) > FILTER_A) || ((Value - NewValue) > FILTER_A))
return Value;
else
return NewValue;
}
A、方法:
B、优点:
C、缺点:
/* N值可根据实际情况调整排序采用冒泡法*/
#define N 11
char filter()
{
char value_buf[N];
char count,i,j,temp;
for ( count=0;count<N;count++)
{
value_buf[count] =get_ad();
delay();
}
for (j=0;j<N-1;j++)
{
for (i=0;i<N-j;i++)
{
if (value_buf>value_buf[i+1] )
{
temp = value_buf;
value_buf = value_buf[i+1];
value_buf[i+1] = temp;
}
}
}
return value_buf[(N-1)/2];
}
A、方法:
N值的选取:一般流量,N=12;压力:N=4
B、优点:
C、缺点:
#define N 12
char filter()
{
int sum = 0;
for (count=0;count<N;count++)
{
sum + = get_ad();
delay();
}
return (char)(sum/N);
}
A、方法:
B、优点:
C、缺点:
// 递推平均滤波法(又称滑动平均滤波法)
#define FILTER_N 12
int filter_buf[FILTER_N + 1];
int Filter() {
int i;
int filter_sum = 0;
filter_buf[FILTER_N] = Get_AD();
for(i = 0; i < FILTER_N; i++) {
filter_buf[i] = filter_buf[i + 1]; // 所有数据左移,低位仍掉
filter_sum += filter_buf[i];
}
return (int)(filter_sum / FILTER_N);
}
A、方法:
B、优点:
C、缺点:
// 中位值平均滤波法(又称防脉冲干扰平均滤波法)(算法1)
#define FILTER_N 100
int Filter() {
int i, j;
int filter_temp, filter_sum = 0;
int filter_buf[FILTER_N];
for(i = 0; i < FILTER_N; i++) {
filter_buf[i] = Get_AD();
delay(1);
}
// 采样值从小到大排列(冒泡法)
for(j = 0; j < FILTER_N - 1; j++) {
for(i = 0; i < FILTER_N - 1 - j; i++) {
if(filter_buf[i] > filter_buf[i + 1]) {
filter_temp = filter_buf[i];
filter_buf[i] = filter_buf[i + 1];
filter_buf[i + 1] = filter_temp;
}
}
}
// 去除最大最小极值后求平均
for(i = 1; i < FILTER_N - 1; i++) filter_sum += filter_buf[i];
return filter_sum / (FILTER_N - 2);
}
// 中位值平均滤波法(又称防脉冲干扰平均滤波法)(算法2)
#define FILTER_N 100
int Filter() {
int i;
int filter_sum = 0;
int filter_max, filter_min;
int filter_buf[FILTER_N];
for(i = 0; i < FILTER_N; i++) {
filter_buf[i] = Get_AD();
delay(1);
}
filter_max = filter_buf[0];
filter_min = filter_buf[0];
filter_sum = filter_buf[0];
for(i = FILTER_N - 1; i > 0; i--) {
if(filter_buf[i] > filter_max)
filter_max=filter_buf[i];
else if(filter_buf[i] < filter_min)
filter_min=filter_buf[i];
filter_sum = filter_sum + filter_buf[i];
filter_buf[i] = filter_buf[i - 1];
}
i = FILTER_N - 2;
filter_sum = filter_sum - filter_max - filter_min + i / 2; // +i/2 的目的是为了四舍五入
filter_sum = filter_sum / i;
return filter_sum;
}
A、方法:
B、优点:
C、缺点:
// 限幅平均滤波法
#define FILTER_A 1
int Filter() {
int i;
int filter_sum = 0;
filter_buf[FILTER_N - 1] = Get_AD();
if(((filter_buf[FILTER_N - 1] - filter_buf[FILTER_N - 2]) > FILTER_A) || ((filter_buf[FILTER_N - 2] - filter_buf[FILTER_N - 1]) > FILTER_A))
filter_buf[FILTER_N - 1] = filter_buf[FILTER_N - 2];
for(i = 0; i < FILTER_N - 1; i++) {
filter_buf[i] = filter_buf[i + 1];
filter_sum += filter_buf[i];
}
return (int)filter_sum / (FILTER_N - 1);
}
A、方法:
B、优点:
C、缺点:
// 一阶滞后滤波法
#define FILTER_A 0.01
int Filter() {
int NewValue;
NewValue = Get_AD();
Value = (int)((float)NewValue * FILTER_A + (1.0 - FILTER_A) * (float)Value);
return Value;
}
A、方法:
B、优点:
C、缺点:
// 加权递推平均滤波法
#define FILTER_N 12
int coe[FILTER_N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; // 加权系数表
int sum_coe = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12; // 加权系数和
int filter_buf[FILTER_N + 1];
int Filter() {
int i;
int filter_sum = 0;
filter_buf[FILTER_N] = Get_AD();
for(i = 0; i < FILTER_N; i++) {
filter_buf[i] = filter_buf[i + 1]; // 所有数据左移,低位仍掉
filter_sum += filter_buf[i] * coe[i];
}
filter_sum /= sum_coe;
return filter_sum;
}
A、方法:
B、优点:
C、缺点:
// 消抖滤波法
#define FILTER_N 12
int i = 0;
int Filter() {
int new_value;
new_value = Get_AD();
if(Value != new_value) {
i++;
if(i > FILTER_N) {
i = 0;
Value = new_value;
}
}
else
i = 0;
return Value;
}
A、方法:
B、优点:
C、缺点:
// 限幅消抖滤波法
#define FILTER_A 1
#define FILTER_N 5
int i = 0;
int Filter() {
int NewValue;
int new_value;
NewValue = Get_AD();
if(((NewValue - Value) > FILTER_A) || ((Value - NewValue) > FILTER_A))
new_value = Value;
else
new_value = NewValue;
if(Value != new_value) {
i++;
if(i > FILTER_N) {
i = 0;
Value = new_value;
}
}
else
i = 0;
return Value;
}