This commit is contained in:
2026-03-20 21:16:58 +08:00
parent 286ff98b8e
commit 90c3d919df
248 changed files with 94554 additions and 0 deletions

74
user/Utility/filter.c Executable file
View File

@@ -0,0 +1,74 @@
#include "../main/SystemInclude.h"
//==============================================================================
//u16 L;
u16 SAMPLES[ANX][FILTER_ANX];
//u16 tempV, *filterPointer, tempMax=0, tempMin=0xffff;
//******************************************************************************
u16 ReadDataFromMovingAverage(u16 channel)
{
u16 L;
u32 voltageSum;
// calculate voltageSum
voltageSum = 0;
//for (L = 1; L <= FILTER_SIZE; L++) voltageSum += (u32)SAMPLES[channel][L];
//voltageSum /= (u32)FILTER_SIZE;
for (L = 0; L < FILTER_ANX; L++) voltageSum += (u32)SAMPLES[channel][L];
voltageSum /= (u32)FILTER_ANX;
return (u16)voltageSum;
}
//******************************************************************************
u16 MovingAverage(u16 voltageBuffer, u16 updateScale, u16 channel)
{
u16 L, tempV, *filterPointer, tempMax=0, tempMin=0xffff;
u16 voltageFilter;
u32 voltageSum;
voltageFilter = SAMPLES[channel][FILTER_END];
voltageSum = (u32)voltageFilter; //
voltageSum *= (u32)updateScale; //
updateScale = (u16)(voltageSum >> 9); //
if(voltageBuffer > voltageFilter) tempV = voltageBuffer - voltageFilter; //
else tempV = voltageFilter - voltageBuffer; //
if(tempV > updateScale)
{
filterPointer = &SAMPLES[channel][0];
for (L = 0; L <= FILTER_END; L++) *filterPointer++ = voltageBuffer;
return voltageBuffer ;
}
filterPointer = &SAMPLES[channel][1];
voltageSum = 0;
for (L = 1; L <= FILTER_END; L++)
{
voltageFilter = *filterPointer;
filterPointer--;
*filterPointer = voltageFilter;
filterPointer += 2;
voltageSum += (u32)voltageFilter;
if(voltageFilter > tempMax) tempMax = voltageFilter;
if(voltageFilter < tempMin) tempMin = voltageFilter;
}
SAMPLES[channel][FILTER_END] = voltageBuffer;
voltageSum += (u32)voltageBuffer;
if(voltageBuffer > tempMax) tempMax = voltageBuffer;
if(voltageBuffer < tempMin) tempMin = voltageBuffer;
voltageSum -= (u32)tempMax;
voltageSum -= (u32)tempMin;
voltageSum >>= FILTER_SHIFT;
return (u16)voltageSum ;
}