Common filtering algorithms for single chip microcomputers

Publisher:sumigLatest update time:2019-04-28 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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;countsum += get_ad();

delay();

}

return (char)(sum/N);

}

4. Recursive average filtering method

(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;countsum+=value_buff[count];

return (char)(sum/N);

}

5. Median average filtering method

(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;countsum += value[count];

return (char)(sum/(N-2));

}

6. Limiting average filtering method

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. 

7. First-order lag filtering method

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;

}

 

8. Weighted recursive average filtering method

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,countsum += value_buf[count]*coe[count];

return (char)(sum/sum_coe);

}

 

9. Anti-jitter filtering method

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;

}

10. Limiting and de-jittering filtering method

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, 9


Keywords:MCU Reference address:Common filtering algorithms for single chip microcomputers

Previous article:6 common MCU digital filtering algorithms
Next article:Digital Filter Design of Single Chip Microcomputer

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号