Sample program for 10 software filtering methods

Source: Internet
Author: User

1, the Limiting filter method (also known as the Program Judgment Filter method)

#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;

}

A, Method:

Based on experience, determine the maximum allowable deviation of two samples (set to a)

Each time a new value is detected:

This value is valid if the difference between this value and the last value is <=a

If the difference between this value and the last value is >a, this value is invalid, discard this value, replace the value with the last value

B, Advantages:

Can effectively overcome the impulse interference caused by accidental factors

C, shortcomings

Can't suppress that periodic disturbance.

Poor smoothing Degree

2, Median value filter 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[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];

}

A, Method:

Continuous sampling n times (n odd)

Arrange the N sampling values by size

Take the middle value as the valid value

B, Advantages:

Can effectively overcome the fluctuation disturbance caused by accidental factors

Good filtering effect on measured parameters with slow change of temperature and liquid level

C, Disadvantages:

The fast changing parameters such as flow rate and speed are not suitable

3, arithmetic average filter method

#define N 12

Char filter ()

{

int sum = 0;

for (count=0;count<n;count++)

{

Sum + = Get_ad ();

Delay ();

}

Return (char) (sum/n);

}

A, Method:

Arithmetic averaging operation by taking n sampled values continuously

Large n value: higher signal smoothness, but less sensitivity

When n values are small: signal smoothness is low, but sensitivity is high

N-Value selection: General flow, n=12; pressure: n=4

B, Advantages:

Suitable for filtering signals that generally have random disturbances

This signal is characterized by an average value, and the signal moves up and down near a range of values

C, Disadvantages:

Not suitable for real-time control with slow measurement or faster data calculation

Compare wasted Ram

4, recursive averaging filter (also known as sliding average filter)

#define N 12  

 

Char value_buf[n]; 

Char i=0; 

 

Char filter ()  

Char count; 

int sum=0; 

value_buf[i++] = Get_ad ();  

if (i = = N ) i = 0; 

for (count=0;count<n,count++)  

sum = value_buf[count]; 

Return (char) ( sum/n)  

}

A, method:  

Take N sampled values as a queue  

The length of the queue is fixed to n 

Each time a new data is sampled into the tail of the team and the first data of the original team is discarded. ( FIFO principle)  

The arithmetic average operation of n data in the queue can obtain the new filter result  

N Value Selection: flow, n=12; pressure: n=4; level, n=4~12; temperature, n=1~4 

B, advantages:  

The

has good inhibitory effect on the periodic disturbance, and the smoothness is high  

System   for high-frequency oscillation;

C, disadvantage:  

Low sensitivity  

The inhibition of occasional impulsive disturbances is poor  

The

is not easy to eliminate the sampling value deviation   caused by impulse interference;

The

is not suitable for occasions where impulse interference is more severe  

Compare wasted ram 

 

5, median value averaging filter method (also known as anti-pulse interference averaging filter method)

#define N 12

Char filter ()

{

Char count,i,j;

Char Value_buf[n];

int sum=0;

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[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[count];

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

}

A, Method:

Equivalent to "median filter" + "Arithmetic average filter"

Continuously samples n data, minus one maximum and one minimum value

Then calculate the arithmetic mean of the N-2 data

Selection of n values: 3~14

B, Advantages:

Combines the advantages of two filtering methods

For accidental impulse interference, the deviation of sampling value caused by impulse interference can be eliminated.

C, Disadvantages:

The measurement is slower, as is the arithmetic average filtering method

Compare wasted Ram

6, the limit amplitude average filter method

Refer to sub-programs 1, 3

A, Method:

Equivalent to "amplitude-limiting filtering" + "recursive averaging filter"

Each new data sampled is limited to the first

Re-feed the queue for recursive averaging filter processing

B, Advantages:

Combines the advantages of two filtering methods

For accidental impulse interference, the deviation of sampling value caused by impulse interference can be eliminated.

C, Disadvantages:

Compare wasted Ram

7, first-order hysteresis filter method

#define a 50

char value;

Char filter ()

{

Char New_value;

New_value = Get_ad ();

Return (100-a) *value + a*new_value;

}

A, Method:

Take a=0~1

This filter result = (1-a) * This sample value +a* the last filter result

B, Advantages:

Good inhibition of periodic disturbances

Suitable for occasions with high fluctuation frequency

C, Disadvantages:

Phase lag, low sensitivity

The degree of lag depends on the a value size

Interference signals with a filter frequency higher than 1/2 of the sampling frequency cannot be eliminated

8. Weighted recursive average filtering method

#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 ();

Delay ();

}

for (count=0,count<n;count++)

Sum + = Value_buf[count]*coe[count];

Return (char) (SUM/SUM_COE);

}

A, Method:

is the improvement of the recursive average filtering method, that is, the data of different moments have different weights

Usually, the closer the data is to the present moment, the greater the right to achieve.

The higher the weight coefficient of the new sampled value, the higher the sensitivity, but the lower the signal smoothness

B, Advantages:

For objects with large pure lag time constants

Systems with short sampling cycles

C, Disadvantages:

For low-latency constant, long sampling period, slow-changing signal

Cannot quickly react to the severity of the system's current interference, the filtering effect is poor

9, anti-shake filter

#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, method:  

Set a filter counter  

Compare each sample value to the current valid value:  

If the sample value = Current valid value, the counter is clear 0  

If the sample value <> current valid value, then counter +1, and determine whether the counter >= upper limit n (overflow) &NBSP

If the counter overflows, replace this value with the current valid value, and clear the counter  

B, advantages:  

Good filtering effect on measured parameters with slow change,  

Avoids the repeated on/off runout of the controller near the critical value or the value jitter   on the monitor;

C, disadvantage:  

For fast changing parameters should not  

If the value sampled at the counter overflow happens to be a disturbing value, The interference value is treated as a valid value  

into the system  

 

10. Amplitude-limiting anti-jitter filtering method

Refer to sub-programs 1, 9

A, Method:

Equivalent to "amplitude-limiting filtering" + "anti-jitter filtering"

First limit amplitude, rear shake

B, Advantages:

Inherited the advantages of "limit" and "shake-out"

Some defects in the "chattering filtering" method are improved to avoid the introduction of interference values into the system.

C, Disadvantages:

For rapidly changing parameters, it is not advisable

Sample program for 10 software filtering methods

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.