# 1, Limiting filtering

1. Method

• Judge the maximum allowable deviation A of two samples according to experience
• Judgment during each new value acquisition: if the difference between the current value and the last value is < = a, it is valid this time; If the difference between the current value and the last value is > A, this time is invalid, and the last value is used instead of this time.

• Overcome pulse interference, unable to suppress periodic interference and poor smoothness.

3. Procedure

```/* A The value is adjusted according to the actual value. The valid value is new_Value is the current sampling value, and the program returns a valid actual value */
#define A 10
char Value;
char filter()
{
char new_Value;
new_Value = get_ad();                                        //Get sample value
if( abs(new_Value - Value) > A)   return Value;             //abs()Take absolute value function
return new_Value;
}```

# 2, Median filtering

1. Method

• Continuous sampling N times, arranged by size
• Take the intermediate value as the effective value this time

• It overcomes the fluctuation interference and has a good filtering effect on the measured parameters with slow change such as temperature, but not on the parameters with rapid change such as speed.

3. Code

```#define N 11
char filter()
{
char value_buf[N];
char count,i,j,temp;
for(count = 0;count < N;count++)                                //Get sample value
{
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 average filtering

1. Method

• Continuous sampling N times, average
• When N is large, the smoothness is high and the sensitivity is low
• When N is small, the smoothness is low and the sensitivity is high
• General N=12

• It is applicable to the system with random interference, occupying more RAM and slow speed.

3. Code

```#define N 12
char filter()
{
int sum = 0;
for(count = 0;count<N;count++)
return (char)(sum/N);
}```

# 4, Recursive average filtering

1. Method

• Take N sampling values to form a queue, first in first out
• Take the mean
• General N=4~12
• The queue is used as the measurement data memory, and the length of the queue is set as n. for each measurement, the measurement result is placed at the end of the queue, and the original data at the head of the queue is discarded, so that there are always n "latest" data in the queue. When calculating the average value, as long as N data in the queue are arithmetically averaged, a new arithmetical average value can be obtained. In this way, a new arithmetic mean can be obtained for each measurement.

• Good suppression of periodic interference and high smoothness
• Suitable for high frequency vibration system
• Low sensitivity, large RAM occupation and serious pulse interference

3. Code

```/*Define filter data type*/
typedef int filter_type;
// Function declaration
filter_type filter(filter_type value_buf[], filter_type new_value, int num);

/* Recursive average filter function */
filter_type filter(filter_type value_buf[], filter_type new_value, int num)
{
static int i;
int count;
filter_type sum = 0;

value_buf[i++] = new_value;

if (i == num)
i = 0;
for (count=0; count<num; count++)
sum += value_buf[count];

return (filter_type)(sum/num);
}```

5, Median average filtering

1. Method

• Sample N values and remove the maximum and minimum
• Calculate the average of N-2
• N= 3~14

• It combines the advantages of median value and average value
• Eliminate pulse interference
• Slow computing speed and large RAM occupation

3. Code

```char filter()
{
char count,i,j;
char Value_buf[N];
int sum=0;
for(count=0;count<N;count++)
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, Amplitude limiting average filtering

1. Method

• Each sampling data is limited first and then sent to the queue
• Take the average value

• Integrate the advantages of amplitude limiting, mean and queue
• Eliminate pulse interference and occupy more RAM

3. Code

```#define A 10
#define N 12
char value,i=0;
char value_buf[N];
char filter()
{
char new_value,sum=0;
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 filtering

1. Method

• Take a=0~1
• This filtering result = (1-a) * this sampling + a * last result

• Good suppression of periodic interference, suitable for occasions with high fluctuation frequency
• Low sensitivity and phase lag

3. Code

```/*In order to speed up program processing, take a=0~100*/
#define a 30
char value;
char filter()
{
char new_value;
return ((100-a)*value + a*new_value);
}```

# 8, Weighted recursive average filtering

1. Method

• For the improvement of recursive average filtering, different weights are given to the data at different times. Generally, the newer the data, the greater the weight. In this way, the sensitivity is high, but the smoothness is low.

• It is suitable for systems with large lag time constant and short sampling period. The signals with small lag time constant, long sampling period and slow change can not quickly respond to their interference.

3. Code

```/* coe The array is a table of weighting coefficients */
#define N 12
char 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++)
{
}
for(count=0;count<N;count++)
sum+=value_buf[count]*coe[count];
return (char)(sum/sum_coe);
}```

# 9, Dithering filtering

1. Method

• Set a filter counter
• Compare the sample value with the currently valid value
• If the sampling value = the current valid value, the counter is cleared to 0
• If the sampling value is not equal to the current valid value, the counter + 1
• If the counter overflows, the sampling value replaces the current valid value, and the counter is cleared to 0

• The filtering effect is good for signals with slow change, but bad for signals with fast change
• Avoid jumping near the critical value. When the counter overflows, if the interference value is collected, it cannot be filtered

3. Code

```#define N 12
char filter()
{
char count=0,new_value;
while(value!=new_value)
{
count++;
if(count>=N) return new_value;
}
return value;
}```

# 10, Amplitude limiting anti chattering filter

1. Method

• First limit amplitude and then eliminate chattering

• It combines the advantages of amplitude limiting and chattering elimination
• Avoid introducing interference value, and it is not suitable for rapidly changing signals

3. Code

```#define A 10
#define N 12
char value;
char filter()
{
char new_value,count=0;
while(value!=new_value)
{
if(Abs(value-new_value)<A)
{
count++;
if(count>=N) return new_value;