-
Determine the maximum allowable deviation between two samples based on experience (set as A)
-
When a new value is detected, judge:
a. If the difference between this value and the last value <= A, then this value is valid
2. Advantages:
-
Can effectively overcome pulse interference caused by random factors
3. Disadvantages
-
Cannot suppress periodic interference
-
Poor smoothness
/* A value is adjusted according to the actual situation, Value is the valid value, new_Value is the current sampled value, the program returns the 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() function to get absolute value
return new_Value;
}
-
Continuously sample N times (N is an odd number), arrange the N sampled values in order of size
-
Take the middle value as the valid value this time
2. Advantages:
-
Can effectively overcome fluctuations caused by random factors
-
Has a good filtering effect on slowly changing measured parameters such as temperature and liquid level
3. Disadvantages:
-
Not suitable for rapidly changing parameters such as flow and speed
#define N 11
char 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];
}
-
Continuously take N sampled 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 up and down around a certain numerical range
3. Disadvantages:
-
Not suitable for real-time control where data calculation speed is required to be fast
-
Relatively wasteful of RAM
#define N 12
char filter()
{
int sum = 0;
for(count = 0; count < N; count++)
{
sum += get_ad();
}
return (char)(sum/N);
}
-
Treat the continuously taken N sampled 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, discarding the original data at the front of the queue (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 oscillation
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
#define N 10
u16 value_buf[N];
u16 sum=0;
u16 curNum=0;
u16 moveAverageFilter()
{
if(curNum < N)
{
value_buf[curNum] = getValue();
sum += value_buf[curNum];
curNum++;
return sum/curNum;
}
else
{
sum -= sum/N;
sum += getValue();
return sum/N;
}
}
1. Method:
-
Equivalent to “Median Filtering Method” + “Arithmetic Mean Filtering Method”
-
Continuously sample N data, discard one maximum and one minimum value
-
Then calculate the arithmetic average 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:
-
Slow measurement speed, similar to arithmetic mean filtering 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));
}
1. Method:
-
Equivalent to “Limit Filtering Method” + “Recursive Average Filtering Method”
-
Each new data sample is first limited,
-
Then sent into 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 12
char 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);
}
1. Method:
-
Take a=0~1
-
The filtering result this time = (1-a) * this sampled value + a * last filtering result
2. Advantages:
-
Has 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 size 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 30
char value;
char filter()
{
char new_value;
new_value = get_ad();
return ((100-a)*value + a*new_value);
}
1. Method:
-
Improvement of the recursive average filtering method, where different weights are given to data at different times
-
Generally, the closer the data is to the current time, the greater the weight taken.
-
The greater the weight coefficient given to the new sampled 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 shorter sampling periods
3. Disadvantages:
-
For signals with small pure lag time constants and longer sampling periods, changes are slow
-
Cannot quickly respond to the severity of interference currently affecting the trading system, poor filtering effect
/* coe array is the weighted coefficient table */
#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++)
{
value_buf[count] = get_ad();
}
for(count = 0; count < N; count++)
{
sum += value_buf[count] * coe[count];
}
return (char)(sum/sum_coe);
}
1. Method:
-
Set a filtering counter
-
Compare each sampled value with the current valid value:
-
If the sampled value = the current valid value, reset the counter
-
If the sampled value > or < the current valid value, increment the counter +1, and check if the counter >= upper limit N (overflow)
-
If the counter overflows, replace the current valid value with this 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 near the critical value or the jitter of the value on the display
3. Disadvantages:
-
Not suitable for rapidly changing parameters
-
If the value sampled at the time of counter overflow happens to be an interference value, it will be treated as a valid value in the trading system
#define N 12
char 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;
}
1. Method:
-
Equivalent to “Limit Filtering Method” + “Debounce Filtering Method”
-
First limit, then debounce
2. Advantages:
-
Inherits the advantages of both “Limit” and “Debounce”
-
Improves certain defects in the “Debounce Filtering Method”, avoiding introducing interference values into the system
3. Disadvantages:
-
Not suitable for rapidly changing parameters
#define A 10
#define N 12
char 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;
}
}
More Exciting:
20th Anniversary Series || My Story with “Computer Education” Magazine Call for Papers
Call for Papers
Nanjing University Professor Chen Dao Xu | Change and Invariance: The Dialectics in the Learning Process
Yan Shi | Thoughts and Suggestions on the “Dilemma” of Young Teachers in Universities
XU Xiaofei et al. | Metaverse Education and Its Service Ecosystem
[Directory] “Computer Education” 2023 Issue 7
[Directory] “Computer Education” 2023 Issue 6
[Directory] “Computer Education” 2023 Issue 5
[Directory] “Computer Education” 2023 Issue 4
[Directory] “Computer Education” 2023 Issue 3
[Directory] “Computer Education” 2023 Issue 2
[Directory] “Computer Education” 2023 Issue 1
[Editorial Message] Peking University Professor Li Xiaoming: Reflections from the “Year of Classroom Teaching Improvement”…
Nanjing University Professor Chen Dao Xu: Which is more important, teaching students to ask questions or teaching students to answer questions?
[Yan Shi Series]: Trends in Computer Science Development and Its Impact on Computer Education
Peking University Professor Li Xiaoming: From Interesting Mathematics to Interesting Algorithms to Interesting Programming – A Path for Non-specialist Learners to Experience Computational Thinking?
Reflections on Several Issues in First-class Computer Discipline Construction
New Engineering and Big Data Major Construction
Learning from Others’ Stones to Attack Your Own Jade – Compilation of Research Articles on Computer Education at Home and Abroad

