芯片复制STM32 ADC采样的十种滤波加程序
- 芯片复制#include "led.h"
- 芯片复制#include "delay.h"
- 芯片复制#include "sys.h"
- #include "usart.h"
- #include "lcd.h"
- #include "adc.h"
- #include "usartbo.h"
- u16 ftable[255] = {
- 2048, 2098, 2148, 2198, 2248, 2298, 2348, 2398, 2447, 2496,
- 2545, 2594, 2642, 2690, 2737, 2785, 2831, 2877, 2923, 2968,
- 3013, 3057, 3100, 3143, 3185, 3227, 3267, 3307, 3347, 3385,
- 3423, 3460, 3496, 3531, 3565, 3598, 3631, 3662, 3692, 3722,
- 3750, 3778, 3804, 3829, 3854, 3877, 3899, 3920, 3940, 3958,
- 3976, 3992, 4007, 4021, 4034, 4046, 4056, 4065, 4073, 4080,
- 4086, 4090, 4093, 4095, 4095, 4095, 4093, 4090, 4086, 4080,
- 4073, 4065, 4056, 4046, 4034, 4021, 4007, 3992, 3976, 3958,
- 3940, 3920, 3899, 3877, 3854, 3829, 3804, 3778, 3750, 3722,
- 3692, 3662, 3631, 3598, 3565, 3531, 3496, 3460, 3423, 3385,
- 3347, 3307, 3267, 3227, 3185, 3143, 3100, 3057, 3013, 2968,
- 2923, 2877, 2831, 2785, 2737, 2690, 2642, 2594, 2545, 2496,
- 2447, 2398, 2348, 2298, 2248, 2198, 2148, 2098, 2047, 1997,
- 1947, 1897, 1847, 1797, 1747, 1697, 1648, 1599, 1550, 1501,
- 1453, 1405, 1358, 1310, 1264, 1218, 1172, 1127, 1082, 1038,
- 995, 952, 910, 868, 828, 788, 748, 710, 672, 635,
- 599, 564, 530, 497, 464, 433, 403, 373, 345, 317,
- 291, 266, 241, 218, 196, 175, 155, 137, 119, 103,
- 88, 74, 61, 49, 39, 30, 22, 15, 9, 5,
- 2, 0, 0, 0, 2, 5, 9, 15, 22, 30,
- 39, 49, 61, 74, 88, 103, 119, 137, 155, 175,
- 196, 218, 241, 266, 291, 317, 345, 373, 403, 433,
- 464, 497, 530, 564, 599, 635, 672, 710, 748, 788,
- 828, 868, 910, 952, 995, 1038, 1082, 1127, 1172, 1218,
- 1264, 1310, 1358, 1405, 1453, 1501, 1550, 1599, 1648, 1697,
- 1747, 1797, 1847, 1897, 1947
- };
- int a=0;
- int b=1;
- /*//////////////////////////////////////////////////////////////////////////
- 方法一:限幅滤波法
- 方法:根据经验判断,确定两次采样允许的最大偏差值(设为A),每次检测到新值时判断:
- 如果本次值与上次值之差<=A,则本次值有效,
- 如果本次值与上次值之差>A,则本次值无效,放弃本次值,用上次值代替本次值。
- 优点:能克服偶然因素引起的脉冲干扰
- 缺点:无法抑制周期性的干扰,平滑度差
- //////////////////////////////////////////////////////////////////////////*/
- #define A 51
- u16 Value1;
- u16 filter1()
- {
- u16 NewValue;
- Value1 = ftable[b-1];
- NewValue = ftable[b];
- b++;
- a++;
- if(a==255) a=0;
- if(b==255) b=1;
- if(((NewValue - Value1) > A) || ((Value1 - NewValue) > A))
- {
- print_host(ftable[a],NewValue);
- return NewValue;
- }
- else
- {
- print_host(ftable[a],Value1);
- return Value1;
- }
- }
- /*//////////////////////////////////////////////////////////////////////////
- 方法二:中位值滤波法
- 方法: 连续采样N次(N取奇数),把N次采样值按大小排列,取中间值为本次有效值。
- 优点:克服偶然因素(对温度、液位的变化缓慢的被测参数有良好的滤波效果)
- 缺点:对流量、速度等快速变化的参数不宜
- //////////////////////////////////////////////////////////////////////////*/
- #define N 3
- u16 value_buf[N];
- u16 filter2()
- {
- u16 count,i,j,temp;
- for(count=0;count<N;count++)
- {
- value_buf[count] = ftable[a];
- a++;
- if(a==255) a=0;
- }
- for (j=0;j<N-1;j++)
- {
- for (i=0;i<N-j;i++)
- {
- if ( value_buf[i] > value_buf[i+1] )
- {
- temp = value_buf[i];
- value_buf [i]= value_buf[i+1];
- value_buf[i+1] = temp;
- }
- }
- }
- // printf("%d\n",value_buf[(N-1)/2]);
- return value_buf[(N-1)/2];
- }
- void pros2()
- {
- print_host(4,filter2());
- }
- /*//////////////////////////////////////////////////////////////////////////
- 方法三:算术平均滤波法
- 方法:连续取N个采样值进行算术平均运算:( N值的选取:一般流量,N=12;压力:N=4。)
- N值较大时:信号平滑度较高,但灵敏度较低;
- N值较小时:信号平滑度较低,但灵敏度较高;
- 优点:适用于对一般具有随机干扰的信号进行滤波;这种信号的特点是有一个平均值,信号在某一数值范围附近上下波动
- 缺点:对于测量速度较慢或要求数据计算速度较快的实时控制不适用,比较浪费RAM。
- ////////////////////////////////////////////////////////////////////////*/
- #define N 5
- u16 filter3()
- {
- u16 sum = 0,count;
- for ( count=0;count<N;count++)
- {
- sum = sum+ ftable[a];
- a++;
- if(a==255) a=0;
- }
- print_host(4,sum/N);
- // printf("%d\n",sum/N);
- return (sum/N);
- }

芯片解密