Microcontroller ADC: Top Ten C Language Filtering Algorithms

1. Clipping Filter Method

1. Method:

    • Determine the maximum allowable deviation between two samples based on experience (denote as A)
    • When a new value is detected, determine:

a. If the difference between the current value and the previous value <= A, then the current value is validb. If the difference between the current value and the previous value > A, then the current value is invalid, discard the current value and use the previous value instead2. Advantages:

    • Can effectively overcome pulse interference caused by random factors

3. Disadvantages:

    • Cannot suppress periodic interference
    • Poor smoothness
/* A value is adjusted based on actual conditions, Value is the valid value, new_Value is the current sample value, the program returns the valid actual value */#define A 10char Value;char filter(){    char new_Value;    new_Value = get_ad(); // Get sample value    if( abs(new_Value - Value) > A)           return Value;     // abs() function to get absolute value    return new_Value;}

2. Median Filter Method1. Method:

    • Continuously sample N times (N is an odd number), and arrange the N sampled values in order
    • Take the middle value as the valid value for this time

2. Advantages:

    • Can effectively overcome fluctuations caused by random factors
    • Has a good filtering effect on slowly changing parameters such as temperature and liquid level

3. Disadvantages:

    • Not suitable for rapidly changing parameters such as flow and speed
#define N 11char filter(){    char value_buf[N];    char count, i, j, temp;    for(count = 0; count < N; count ++) // Get sample values    {        value_buf[count] = get_ad();        delay();    }    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;            }        }    }    return value_buf[(N-1)/2];}

3. Arithmetic Mean Filter Method1. Method:

    • Continuously take N sample values for arithmetic averaging
    • When N is large: the signal smoothness is high, but sensitivity is low
    • When N is small: the signal smoothness is low, but sensitivity is high
    • Selection of N: generally for flow, N=12; for pressure, N=4

2. Advantages:

    • Suitable for filtering signals with random interference
    • Such signals have an average value, fluctuating around a certain value range

3. Disadvantages:

    • Not suitable for real-time control requiring fast data computation for slow measurement speeds
    • Relatively wasteful of RAM
#define N 12char filter(){    int sum = 0;    for(count = 0; count < N; count++)    {        sum += get_ad();    }     return (char)(sum/N);}

4. Recursive Average Filter Method

1. Method:

    • Treat the continuous N sample values as a queue
    • The length of the queue is fixed at N
    • Each time a new data sample is taken, it is added to the end of the queue, and the data at the front of the queue is discarded (FIFO principle)
    • Perform arithmetic averaging on the N data in the queue to obtain the new filtering result
    • Selection of N: for flow, N=12; for pressure, N=4; for liquid level, N=4 ~ 12; for temperature, N=1 ~ 4

2. Advantages:

    • Good suppression of periodic interference, high smoothness
    • Suitable for systems with high-frequency oscillations

3. Disadvantages:

    • Low sensitivity
    • Poor suppression of sporadic pulse interference
    • Difficult to eliminate sampling value deviations caused by pulse interference
    • Not suitable for situations with severe pulse interference
    • Relatively wasteful of RAM
/* A value is adjusted based on actual conditions, Value is the valid value, new_Value is the current sample value, the program returns the valid actual value */#define A 10char Value;char filter(){    char new_Value;    new_Value = get_ad(); // Get sample value    if( abs(new_Value - Value) > A)           return Value;     // abs() function to get absolute value    return new_Value;}

5. Median Average Filter Method

1. Method:

    • Equivalent to “Median Filter Method” + “Arithmetic Mean Filter Method”
    • Continuously sample N data, remove one maximum and one minimum value
    • Then calculate the arithmetic mean of the N-2 data
    • Selection of N: 3~14

2. Advantages:

    • Combines the advantages of both filtering methods
    • Can eliminate sampling value deviations caused by sporadic pulse interference

3. Disadvantages:

    • Measurement speed is slower, similar to the arithmetic mean filter method
    • Relatively wasteful of RAM
char filter(){    char count, i, j;    char Value_buf[N];    int sum = 0;    for(count = 0; count < N; count++)    {        Value_buf[count] = get_ad();    }     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;            }        }      }        for(count = 1; count < N-1; count ++)    {        sum += Value_buf[count];    }    return (char)(sum/(N-2));}

6. Clipping Average Filter Method

1. Method:

    • Equivalent to “Clipping Filter Method” + “Recursive Average Filter Method”
    • Each time a new data sample is taken, it is first subjected to clipping processing,
    • Then sent to the queue for recursive average filtering

2. Advantages:

    • Combines the advantages of both filtering methods
    • Can eliminate sampling value deviations caused by sporadic pulse interference

3. Disadvantages:

    • Relatively wasteful of RAM
#define A 10#define N 12char value, i = 0;char value_buf[N];char filter(){    char new_value, sum = 0;    new_value = get_ad();    if(Abs(new_value - value) < A)          value_buf[i++] = new_value;    if(i==N)          i=0;    for(count = 0; count < N; count++)    {        sum += value_buf[count];    }    return (char)(sum/N);}

7. First-Order Lag Filter Method

1. Method:

    • Take a=0~1
    • The filtering result = (1-a) current sample value + a previous filtering result

2. Advantages:

    • Good suppression of periodic interference
    • Suitable for situations with high fluctuation frequencies

3. Disadvantages:

    • Phase lag, low sensitivity
    • The degree of lag depends on the value of a
    • Cannot eliminate interference signals with frequencies higher than half the sampling frequency
/* To speed up program processing, take a=0~100 */#define a 30char value;char filter(){    char new_value;    new_value = get_ad();    return ((100-a)*value + a*new_value);}

8. Weighted Recursive Average Filter Method

1. Method:

    • This is an improvement on the recursive average filter method, where data at different times is given different weights
    • Typically, the closer the data is to the current time, the greater the weight.
    • The greater the weight coefficient given to the new sample value, the higher the sensitivity, but the lower the signal smoothness

2. Advantages:

    • Suitable for objects with large pure lag time constants
    • And systems with short sampling periods

3. Disadvantages:

    • For signals with small pure lag time constants and long sampling periods, changes are slow
    • Cannot quickly respond to the severity of interference currently affecting the trading system, resulting in poor filtering effects
/* coe array is the weighting coefficient table */#define N 12char code coe[N] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};char code sum_coe = {1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12};char filter(){    char count;    char value_buf[N];    int sum = 0;    for(count = 0; count < N; count++)    {        value_buf[count] = get_ad();    }    for(count = 0; count < N; count++)    {        sum += value_buf[count] * coe[count];    }     return (char)(sum/sum_coe);}

9. Debounce Filter Method

1. Method:

    • Set a filtering counter
    • Compare each sampled value with the current valid value:
    • If the sampled value = current valid value, reset the counter
    • If the sampled value > or < current valid value, increment the counter and check if the counter >= upper limit N (overflow)
    • If the counter overflows, replace the current valid value with the current value and reset the counter

2. Advantages:

    • Has a good filtering effect on slowly changing measured parameters,
    • Can avoid the repeated on/off bouncing of the controller or the jitter of values on the display near the critical value

3. Disadvantages:

    • Not suitable for rapidly changing parameters
    • If the value sampled during the counter overflow happens to be an interference value, it will be treated as a valid value in the trading system
#define N 12char filter(){    char count = 0, new_value;    new_value = get_ad();    while(value != new_value)    {        count++;        if(count >= N)             return new_value;        new_value = get_ad();    }    return value;}

10. Clipping Debounce Filter Method

1. Method:

    • Equivalent to “Clipping Filter Method” + “Debounce Filter Method”
    • First clip, then debounce

2. Advantages:

    • Inherits the advantages of both “clipping” and “debounce”
    • Improves certain defects in the “debounce filter method”, avoiding the introduction of interference values into the system

3. Disadvantages:

    • Not suitable for rapidly changing parameters
#define A 10#define N 12char value;char filter(){    char new_value, count = 0;    new_value = get_ad();    while(value != new_value)    {        if(Abs(value - new_value) < A)        {            count++;            if(count >= N)                 return new_value;            new_value = get_ad();        }        return value;    }}

Leave a Comment