针对16位非负数的滑动滤波函数_Andy_Issta_新浪博客
介绍一个个人编写的滑动滤波函数:
#define OHG_BUF_IDX_MOV_STEP 1U
#define OHG_BUF_FLG_FULL 1U
#define OHG_BUF_FLG_NOT_FULL 0U
#define OHG_BUF_IDX_RESET 0U
typedef struct _T_OHG_FILTER
{
INT16U u16_mFilterRult;
INT32U u32_mDataSum;
INT8U u8_mBufIndex;
INT8U u8_mBufFullFlg;
INT8U u8_mSampFactor;
}T_OHG_FILTER_TYP;
函数原型:
void MovingFilter(uint16_t sampData, T_ATE_FILTER_TYP *pMovFliterObj, uint16_t *pBuf)
{
pMovFliterObj->u32_mDataSum += sampData;
if (pMovFliterObj->u8_mBufFullFlg != FILTER_BUF_FLG_UNDF)
{
pMovFliterObj->u32_mDataSum -= pBuf[pMovFliterObj->u8_mBufIndex];
pMovFliterObj->u16_mFilterRult = (uint16_t)(pMovFliterObj->u32_mDataSum / (pMovFliterObj->u8_mSampFactor));
pBuf[pMovFliterObj->u8_mBufIndex] = sampData;
pMovFliterObj->u8_mBufIndex = (uint8_t)((pMovFliterObj->u8_mBufIndex + FILTER_IDX_MOV_STEP) % (pMovFliterObj->u8_mSampFactor));
}
else
{
pBuf[pMovFliterObj->u8_mBufIndex] = sampData;
pMovFliterObj->u8_mBufIndex += FILTER_IDX_MOV_STEP;
if (pMovFliterObj->u8_mBufIndex == pMovFliterObj->u8_mSampFactor)
{
pMovFliterObj->u8_mBufIndex = FILTER_BUF_IDX_CLAR;
pMovFliterObj->u8_mBufFullFlg = FILTER_BUF_FLG_FULL;
pMovFliterObj->u16_mFilterRult = (uint16_t)(pMovFliterObj->u32_mDataSum / (pMovFliterObj->u8_mSampFactor));
}
else
{
pMovFliterObj->u16_mFilterRult = (uint16_t)(pMovFliterObj->u32_mDataSum / pMovFliterObj->u8_mBufIndex);
}
}
}
STC_CurMovFilterObj.u16_mFilterRult = 0U;
STC_CurMovFilterObj.u32_mDataSum = 0U;
STC_CurMovFilterObj.u8_mBufIndex = OHG_BUF_IDX_RESET;
STC_CurMovFilterObj.u8_mBufFullFlg = OHG_BUF_FLG_NOT_FULL;
STC_CurMovFilterObj.u8_mSampFactor = OHG_CUR_SAMP_FACTOR;
3:周期调用滤波函数:
hOverHeat_vMovFilter(((INT16U)MotorActualCur), &STC_CurMovFilterObj, hOverHeat_aCurFilterBuf);
4:需要时读取结果:
hOverHeat_u16SysCurrent = STC_CurMovFilterObj.u16_mFilterRult;
5:简单测试:
int main(array ^args)
{
UInt16 InputSampData = 0;
//Console::WriteLine(L"Hello World");
MovingFilterObjInit();
while(1)
{
cin >> InputSampData;
MovingFilter(InputSampData, &FogCapMovFilterObj, FogCapFilterBuf);
Console::WriteLine("---{0}", FogCapMovFilterObj.u16_mFilterRult);
Console::WriteLine("------{0} {1} {2} {3}", FogCapFilterBuf[0], FogCapFilterBuf[1], FogCapFilterBuf[2], FogCapFilterBuf[3]);
}
return 0;
}