/// <summary>
///滑动平均滤波算法(递推平均滤波法)
/// </summary>
/// <param name="ADNum"为获得的AD数></param>
/// GN为数组value_buf[]的元素个数,该函数主要被调用,利用参数的数组传值
/// <returns></returns>
private const int GN = 12;
private int filterPtr = 0;
private bool isFirstGF = true;
public float gSum = 0;
float[] gbuf = new float[GN];
public float GlideFilterAD(float ADNum)
{
if (isFirstGF)
{
isFirstGF = false;
for (int i = 0; i < GN; i++)
gbuf[i] = ADNum;
gSum = ADNum * GN;
return ADNum;
}
else
{
gSum += ADNum - gbuf[filterPtr] ;
gbuf[filterPtr++] = ADNum;
if (filterPtr == GN)
filterPtr = 0; //先进先出,再求平均值
return (gSum / GN);
}
}
/// <summary>
/// 限幅防抖滤波法
/// </summary>
/// <param name="ADNum"采样值></param>
/// RANG:幅度 LPNUM :测试计数
/// <returns></returns>
private const int RANGE = 100;
private const int LPNUM = 5;
private float currentValue = 0;
private int tmpCount = 0;
private bool isFirstLF = true;
public float LimitFilterAD(float ADNum)
{
if (isFirstLF)
{
isFirstLF = false;
currentValue = ADNum;
}
if (Math.Abs(ADNum - currentValue) > RANGE)
{
if(tmpCount++ >LPNUM)
{
isFirstLF = true; // 初始化滑动平均值
tmpCount =0;
currentValue = ADNum;
}
}
else
{
tmpCount =0;
}
return currentValue;
}