野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11731|回复: 10

10种简单的数字滤波算法(C语言源程序)

[复制链接]
发表于 2013-11-16 12:12:48 | 显示全部楼层 |阅读模式
假定从8位AD中读取数据(如果是更高位的AD可定义数据类型为int),子程序为get_ad();  
1、限副滤波
  1. /*  A值可根据实际情况调整
  2.     value为有效值,new_value为当前采样值   
  3.     滤波程序返回有效的实际值  */
  4. #define A 10
  5. char value;
  6. char filter()
  7. {
  8.    char  new_value;
  9.    new_value = get_ad();
  10.    if ( ( new_value - value > A ) || ( value - new_value > A )
  11.       return value;
  12.    return new_value;
  13. }
复制代码
2、中位值滤波法
  1. /*  N值可根据实际情况调整
  2.     排序采用冒泡法*/
  3. #define N  11
  4. char filter()
  5. {
  6.    char value_buf[N];
  7.    char count,i,j,temp;
  8.    for ( count=0;count<N;count++)
  9.    {
  10.       value_buf[count] = get_ad();
  11.       delay();
  12.    }
  13.    for (j=0;j<N-1;j++)
  14.    {
  15.       for (i=0;i<N-j;i++)
  16.       {
  17.          if ( value_buf[i]>value_buf[i+1] )
  18.          {
  19.             temp = value_buf[i];
  20.             value_buf[i] = value_buf[i+1];  
  21.              value_buf[i+1] = temp;
  22.          }
  23.       }
  24.    }
  25.    return value_buf[(N-1)/2];
  26. }
复制代码
3、算术平均滤波法
  1. /*
  2. */
  3. #define N 12
  4. char filter()
  5. {
  6.    int  sum = 0;
  7.    for ( count=0;count<N;count++)
  8.    {
  9.       sum + = get_ad();
  10.       delay();
  11.    }
  12.    return (char)(sum/N);
  13. }
复制代码
4、递推平均滤波法(又称滑动平均滤波法)
  1. /*
  2. */
  3. #define N 12  
  4. char value_buf[N];
  5. char i=0;
  6. char filter()
  7. {
  8.    char count;
  9.    int  sum=0;
  10.    value_buf[i++] = get_ad();
  11.    if ( i == N )   i = 0;
  12.    for ( count=0;count<N,count++)
  13.       sum = value_buf[count];
  14.    return (char)(sum/N);
  15. }
复制代码
5、中位值平均滤波法(又称防脉冲干扰平均滤波法)
  1. /*
  2. */
  3. #define N 12
  4. char filter()
  5. {
  6.    char count,i,j;
  7.    char value_buf[N];
  8.    int  sum=0;
  9.    for  (count=0;count<N;count++)
  10.    {
  11.       value_buf[count] = get_ad();
  12.       delay();
  13.    }
  14.    for (j=0;j<N-1;j++)
  15.    {
  16.       for (i=0;i<N-j;i++)
  17.       {
  18.          if ( value_buf[i]>value_buf[i+1] )
  19.          {
  20.             temp = value_buf[i];
  21.             value_buf[i] = value_buf[i+1];  
  22.              value_buf[i+1] = temp;
  23.          }
  24.       }
  25.    }
  26.    for(count=1;count<N-1;count++)
  27.       sum += value[count];
  28.    return (char)(sum/(N-2));
  29. }
复制代码
6、限幅平均滤波法
  1. /*
  2. */   
  3. 略 参考子程序1、3
  4. 7、一阶滞后滤波法
  5. [code]/* 为加快程序处理速度假定基数为100,a=0~100 */
  6. #define a 50
  7. char value;
  8. char filter()
  9. {
  10.    char  new_value;
  11.    new_value = get_ad();
  12.    return (100-a)*value + a*new_value;  
  13. }
复制代码
8、加权递推平均滤波法
  1. /* coe数组为加权系数表,存在程序存储区。*/
  2. #define N 12
  3. char code coe[N] = {1,2,3,4,5,6,7,8,9,10,11,12};
  4. char code sum_coe = 1+2+3+4+5+6+7+8+9+10+11+12;
  5. char filter()
  6. {
  7.    char count;
  8.    char value_buf[N];
  9.    int  sum=0;
  10.    for (count=0,count<N;count++)
  11.    {
  12.       value_buf[count] = get_ad();
  13.       delay();
  14.    }
  15.    for (count=0,count<N;count++)
  16.       sum += value_buf[count]*coe[count];
  17.    return (char)(sum/sum_coe);
  18. }
复制代码
9、消抖滤波法
  1. #define N 12
  2. char filter()
  3. {
  4.    char count=0;
  5.    char new_value;
  6.    new_value = get_ad();
  7.    while (value !=new_value);
  8.    {
  9.       count++;
  10.       if (count>=N)   return new_value;
  11.        delay();
  12.       new_value = get_ad();
  13.    }
  14.    return value;     
  15. }
复制代码
10、限幅消抖滤波法
略 参考子程序1、9
回复

使用道具 举报

发表于 2013-11-16 13:10:12 | 显示全部楼层
路过,学习下
回复 支持 反对

使用道具 举报

发表于 2013-11-16 14:47:31 | 显示全部楼层
学习了,不错,讲的太有道理了
回复 支持 反对

使用道具 举报

发表于 2013-11-16 18:02:32 | 显示全部楼层
学习了,不错,讲的太有道理了
回复 支持 反对

使用道具 举报

发表于 2013-11-17 10:41:24 | 显示全部楼层
之前看过,不过温故一下,收藏了
回复 支持 反对

使用道具 举报

发表于 2013-11-17 14:14:31 | 显示全部楼层
不错,收藏了!~
回复 支持 反对

使用道具 举报

发表于 2013-11-26 11:17:57 | 显示全部楼层
不错正是我需要的收藏了嘻嘻
回复 支持 反对

使用道具 举报

发表于 2013-11-26 11:18:16 | 显示全部楼层
不错正是我需要的收藏了嘻嘻
回复 支持 反对

使用道具 举报

发表于 2014-5-4 23:48:51 | 显示全部楼层
强烈支持,非常感谢哥们强烈支持,非常感谢哥们强烈支持,非常感谢哥们
回复 支持 反对

使用道具 举报

发表于 2015-1-11 09:57:38 | 显示全部楼层
学习一下,感谢分享
回复 支持 反对

使用道具 举报

发表于 2015-6-14 20:19:21 | 显示全部楼层
学习论了,谢谢
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-5-6 06:51 , Processed in 0.044268 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表