Note: Assume that data is read from 8-bit AD (if it is a higher-bit AD, the data type can be defined as int), the subroutine is get_ad();
1. Limiting filter method
(Also known as program judgment filtering method)
A. Method:
Based on experience, determine the maximum deviation allowed for two samples (set to A), and make a judgment each time a new value is detected. If the difference between this value and the previous value is
B. Advantages:
Can effectively overcome pulse interference caused by accidental factors
C. Disadvantages
①. Unable to suppress periodic interference
②. Poor smoothness
D. Sample code
#define A 10
char value;
char filter()
{
char new_value;
new_value = get_ad();
if ( ( new_value - value > A ) || ( value - new_value > A ) return value;
return new_value;
}
2. Median filter method
A. Methods:
Take samples N times continuously (N is an odd number), arrange the N sample values in order of size, and take the middle value as the effective value of this time.
B. Advantages:
①. Can effectively overcome fluctuation interference caused by accidental factors
②. It has good filtering effect on the measured parameters with slow changes in temperature and liquid level
C. Disadvantages:
It is not suitable for parameters that change rapidly, such as flow rate and speed.
E. Sample code
/* The N value can be adjusted according to actual conditions
Sorting uses the bubble method*/#define N 11
char filter()
{
char value_buf[N];
char count,i,j,temp;
for (count = 0; count < N; count++)
{
value_buf[count] = get_ad();
delay();
}
for (j = 0; j < N-1; j++)
{
for (i = 0; i < N - j; i++)
{
if ( value_buf > value_buf[i + 1] )
{
temp = value_buf;
value_buf = value_buf[i + 1];
value_buf[i + 1] = temp;
}
}
}
return value_buf[(N-1)/2];
}
3. Arithmetic mean filtering method
A. Methods:
Take N sampling values continuously for arithmetic averaging. When the N value is large, the signal smoothness is higher, but the sensitivity is lower; when the N value is small, the signal smoothness is lower, but the sensitivity is higher.
Selection of N value: general flow, N=12; pressure: N=4.
B. Advantages:
It is suitable for filtering signals with random interference. The characteristics of such signals are that they have an average value and the signal fluctuates around a certain value range.
C. Disadvantages:
①. Not applicable to real-time control with slow measurement speed or fast data calculation speed
②. Waste of RAM
D. Sample code
#define N 12
char filter()
{
int sum = 0;
for (count=0;count delay(); } return (char)(sum/N); } (Also known as sliding average filtering) A. Methods: Consider N consecutive sample values as a queue. The length of the queue is fixed at N. Each time a new data is sampled, it is put at the end of the queue and the data at the head of the queue is discarded (first-in-first-out principle). Perform arithmetic averaging on the N data in the queue to obtain a new filtering result. Selection of N value: flow rate, N=12; pressure: N=4; liquid level, N=4~12; temperature, N=1~4 B. Advantages: ①. It has good suppression effect on periodic interference and high smoothness; ②. Suitable for systems with high frequency oscillation. C. Disadvantages: ①. Low sensitivity; ②. The suppression effect on occasional pulse interference is poor; ③. It is not easy to eliminate the sampling value deviation caused by pulse interference; ④. Not suitable for occasions with severe pulse interference; ⑤. It wastes RAM. F. Sample Code char value_buff[N]; char i=0; char filter() { char count; int sum=0; value_buff[i++]=get_data(); if(i==N)i=0; for(count=0;count return (char)(sum/N); } (Also known as pulse interference prevention average filtering method) A. Methods: It is equivalent to "median filtering method" + "arithmetic mean filtering method", which continuously samples N data, removes a maximum value and a minimum value, and then calculates the arithmetic mean of N-2 data. Selection of N value: 3~14. B. Advantages: ①. Combines the advantages of two filtering methods; ②. For occasional pulse interference, the sampling value deviation caused by the pulse interference can be eliminated. C. Disadvantages: ①. The measurement speed is slow, the same as the arithmetic average filtering method; ②. It wastes RAM. D. Sample code #define N 12 char filter() { char count,i,j; char value_buf[N]; int sum=0; for (count=0;count value_buf[count] = get_ad(); delay(); } for (j=0;j for (i=0;i if ( value_buf>value_buf[i+1] ) { temp = value_buf; value_buf = value_buf[i+1]; value_buf[i+1] = temp; } } } for(count=1;count return (char)(sum/(N-2)); } A. Methods: It is equivalent to "limiting filter method" + "recursive averaging filter method". Each time the new data is sampled, it is first limited and then sent to the queue for recursive averaging filtering. B. Advantages: ①. Combining the advantages of two filtering methods ②. For occasional pulse interference, the sampling value deviation caused by pulse interference can be eliminated C. Disadvantages: Waste of RAM D. Sample code: Omitted. Refer to subroutines 1 and 3. A. Methods: Take a=0~1 The current filtering result = (1-a) * this sampling value + a * last filtering result B. Advantages: ①. It has good suppression effect on periodic interference; ②. Suitable for occasions with higher fluctuation frequency. C. Disadvantages: ①. Phase lag and low sensitivity; ②. The degree of hysteresis depends on the value of a; ③. Interference signals whose filtering frequency is higher than 1/2 of the sampling frequency cannot be eliminated. G. Sample code: /* To speed up the program processing, assume the base is 100, a=0~100*/ #define a 50 char value; char filter() { char new_value; new_value = get_ad(); return (100-a)*value + a*new_value; } A. Methods: It is an improvement on the recursive average filtering method, that is, different weights are given to data at different times. Usually, the closer the data is to the current time, the greater the weight is. The larger the weight coefficient given to the new sample value, the higher the sensitivity, but the lower the signal smoothness. B. Advantages: ①. Suitable for objects with large pure lag time constant; ②. And systems with shorter sampling periods. C. Disadvantages: ①. For signals with small pure lag time constant, long sampling period and slow changes; ②. It cannot quickly respond to the severity of the interference the system is currently experiencing, and the filtering effect is poor. D. Sample code: /* The coe array is a weighted coefficient table, stored in the program storage area. */ #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 value_buf[count] = get_ad(); delay(); } for (count=0,count return (char)(sum/sum_coe); } A. Methods: Set a filter counter and compare each sampling value with the current effective value: if the sampling value = the current effective value, the counter is cleared; if the sampling value <> the current effective value, the counter is +1, and it is determined whether the counter is >= the upper limit N (overflow); if the counter overflows, the current value will replace the current effective value and the counter will be cleared. B. Advantages: It has a good filtering effect on the slowly changing measured parameters, which can avoid the repeated on/off jump of the controller or the value jitter on the display near the critical value. C. Disadvantages: ①. It is not suitable for fast changing parameters ②. If the value sampled when the counter overflows happens to be an interference value, the interference value will be imported into the system as a valid value. H. Sample code: #define N 12 char filter() { char count=0; char new_value; new_value = get_ad(); while (value != new_value); { count++; if (count>=N) return new_value; delay(); new_value = get_ad(); } return value; } A. Methods: It is equivalent to "limiting filter method" + "anti-jitter filtering method", first limiting the amplitude, then eliminating jitter. B. Advantages: ①. Inherits the advantages of "limiting" and "de-jittering" ②. Improved some defects in the "anti-jitter filtering method" to avoid introducing interference values into the system C. Disadvantages: Not suitable for rapidly changing parameters D. Example References 1, 94. Recursive average filtering method
5. Median average filtering method
6. Limiting average filtering method
7. First-order lag filtering method
8. Weighted recursive average filtering method
9. Anti-jitter filtering method
10. Limiting and de-jittering filtering method
Previous article:6 common MCU digital filtering algorithms
Next article:Digital Filter Design of Single Chip Microcomputer
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- ST NUCLEO-H743ZI Review Summary
- Animation showing the working principle of capacitive sensors and capacitive headphones
- 【New Year's Festival Competition】+Flying Yingge Dance
- MSP430f5529 car source code
- 【EasyARM-RT1052 Review】+ Transplantation and use of Fatfs file system based on SD card
- If you don't work in the electronics industry, what else can you do?
- 【Goodbye 2021, Hello 2022】2021 calendar
- Theoretical basis of the movie "The Wandering Earth"
- Satellite Internet in One Picture
- cc254x cc2640 WeChat access airsync-broadcast data