1st methods limit filtering (also known as program judgment filtering) method
Based on experience, determine the maximum deviation allowed by the two samples (set to)
When a new value is detected:
If the difference between the current value and the previous value is <= A, the current value is valid.
If the difference between the current value and the previous value is> A, the current value is invalid. Discard this value and replace this value with the previous value.
B advantages
Can effectively overcome impulsive interference caused by accidental factors
C disadvantages
It cannot suppress periodic interference.
Poor Smoothness
/* The a value can be adjusted according to the actual situation.
Value is a valid value, and new_value is the current sample value.
The filter returns valid actual values */
# Define a 10
Char value;
Char filter ()
{
Char new_value;
New_value = get_ad ();
If (new_value-value> A) | (value-new_value>)
Return value;
Else return new_value;
}
2nd Methods center value filtering method
Continuous Sampling n times (N is an odd number)
Sort n sample values by size
The median value is the valid value.
B advantages
It can effectively overcome the fluctuation interference caused by accidental factors.
Good filtering effect on tested parameters with slow changes in temperature and Liquid Level
C disadvantages
Inefficient at traffic, speed, and other rapidly changing parameters
/* The N value can be adjusted according to the actual situation.
Sort by Bubble Method */
# Define N 11
Char filter ()
{
Char value_buf [N];
Char count, I, j, temp;
For (COUNT = 0; count {
Value_buf [count] = get_ad ();
Delay ();
}
For (j = 0; j <N-1; j ++) // Bubble Method
{
For (I = 0; I <N-1-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];
}
3rd Methods arithmetic mean filtering method
N consecutive sample values for arithmetic mean Operations
Large N value: high signal smoothness, but low sensitivity
N value is relatively small: the signal smoothness is low, but the sensitivity is high.
N Value Selection: general traffic, n = 12; pressure: N = 4
B advantages
Suitable for filtering signals with random interference.
In this way, the signal has an average value, and the signal fluctuates up and down near a certain value range.
C disadvantages
It is not applicable to real-time control of slow measurement speed or fast data computing speed.
Resource Access Management (RAM) is a waste of resources.
/*
*/
# Define N 12
Char filter ()
{
Int sum = 0;
For (COUNT = 0; count <n; count ++)
{
Sum + = get_ad ();
Delay ();
}
Return (char) (sum/N );
}
4th Methods recursive mean filtering (also known as moving average filtering) method
Regard n consecutive sample values as a queue
The queue length is fixed to n.
Each time a new data is sampled and put at the end of the team, the first data of the original team is discarded. (first-in-first-out principle)
Perform arithmetic mean operations on N data in the queue to obtain new filtering results.
N Value Selection: traffic, n = 12; pressure: N = 4; liquid level, n = 4 ~ 12; temperature, n = 1 ~ 4
B advantages
Excellent anti-Periodic Interference and High smoothness
Suitable for high-frequency oscillating systems
C disadvantages
Low sensitivity
Ineffective suppression of occasional impulsive interference
It is difficult to eliminate the sample value Deviation Caused by pulse interference.
It is not applicable to scenarios with severe pulse interference.
Resource Access Management (RAM) is a waste of resources.
/*
*/
# 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 );
}
Method A of the average median value filtering method (also known as the average filtering method for anti-pulse interference) of the 5th Methods
It is equivalent to "median value filtering method" + "arithmetic mean filtering method"
Continuous Sampling of n data, removing a maximum and then calculating the arithmetic mean of N-2 data
N Value Selection: 3 ~ 14
B advantages
Integrates the advantages of the two filter methods
The sample value Deviation Caused by impulse interference can be eliminated.
C disadvantages
Slow measurement speed, same as the arithmetic mean Filtering Method
Resource Access Management (RAM) is a waste of resources.
/*
*/
# 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 <N-1; j ++) // Bubble Method
{
For (I = 0; I <N-1-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 = 0; count <n; count ++) sum = value_buf [count];
Return (char) (sum/(N-2 ));
}
6th methods limit average filtering method
It is equivalent to "limit filtering" + "recursive average filtering"
The new data sampled at each time is first limited.
Send it to the queue for Recursive average filtering
B advantages
Integrates the advantages of the two filter methods
The sample value Deviation Caused by impulse interference can be eliminated.
C disadvantages
Resource Access Management (RAM) is a waste of resources.
7th methods first-order delayed filtering method
Obtain a = 0 ~ 1
Filter result = (1-A) * sample value + A * last filter result
B advantages
It can effectively suppress periodic interference.
Suitable for scenarios with high volatility
C disadvantages
Phase lagging, low sensitivity
The degree of lag depends on the size of value.
The interference signal with a filtering frequency greater than the sampling frequency of 1/2 cannot be eliminated.
/* To accelerate the processing speed of the program, assume that the base number 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;
}
8th Methods weighted recursive mean filtering method
Is an improvement of the recursive average filtering method, that is, different weights are applied to data at different time points.
Generally, the closer the information is to the current time, the larger the right is obtained.
The greater the weight 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 constants
And systems with short sampling periods
C disadvantages
Signals with low pure lag time constants, long sampling periods, and slow changes
The system's current interference severity cannot be quickly reflected, and the filtering effect is poor.
/* The CoE array is a weighting coefficient table, which exists in the program storage area. */
# Define N 12
Char code COE [N] = {1, 2, 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 <n; count ++) sum = value_buf [count];
Return (char) (sum/sum_coe );
}
9th Methods deshake filtering method
Set a filter counter
Compare the value of each sample with the current valid value:
If the sample value = the current valid value, the counter is cleared.
If the sampling value is <> the current valid value, the counter is + 1 and determines whether the counter is greater than or equal to n (overflow)
If the counter overflows, replace the current valid value with this value and clear the counter.
B advantages
It provides better filtering effects for tested parameters with slow changes,
It can avoid repeated on/off beats or numerical jitter on the monitor near the critical value.
C disadvantages
Not suitable for rapidly changing parameters
If the sampled value that overflows from the counter happens to be the interference value, the interference value is imported into the system as a valid value.
# 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;
}
10th methods limit and deshake filtering method
It is equivalent to "limit filtering" + "deshake filtering"
First limit and then shake
B advantages
It inherits the advantages of "limiting" and "shake-free ".
Some defects in the "Shake-free filtering" method are improved to avoid importing interference values into the system.
C disadvantages
Not suitable for rapidly changing parameters
11th Methods: IIR Digital Filter
Method
Determine the signal bandwidth,
Filter.
Y (n) = A1 * Y (n-1) + A2 * Y (n-2) +... + AK * Y (n-k) + B0 * x (n) + B1 * X (n-1) + B2 * X (n-2) +... + BK * x (n-k)
B advantages
High pass, low pass, and band pass. Simple Design (using Matlab)
C disadvantages
Large computing capacity
Int bandpassfilter4 (INT inputad4)
{
Int returnvalue;
Int II;
Reslo = 0;
Reshi = 0;
Macs = * pdelin;
OP2 = 1068; // filtercoef F4 [4];
Macs = * (pdelin + 1 );
OP2 = 8; // filtercoef F4 [3];
Macs = * (pdelin + 2 );
OP2 =-2001; // filtercoeff4 [2];
Macs = * (pdelin + 3 );
OP2 = 8; // filtercoef F4 [1];
Macs = inputad4;
OP2 = 1068; // filtercoef F4 [0];
Macs = * pdelou;
OP2 =-7190; // filtercoeff4 [8];
Macs = * (pdelou + 1 );
OP2 =-1973; // filtercoef F4 [7];
Macs = * (pdelou + 2 );
OP2 =-19578; // filtercoeff4 [6];
Macs = * (pdelou + 3 );
OP2 =-3047; // filtercoef F4 [5];
* P = reslo;
* (P + 1) = reshi;
Mytestmul <= 2;
Returnvalue = * (p + 1 );
For (II = 0; II <3; II ++)
{
Delayinput [II] = delayinput [II + 1];
Delayoutput [II] = delayoutput [II + 1];
}
Delayinput [3] = inputad4;
Delayoutput [3] = returnvalue;
// If (returnvalue <0)
//{
// Returnvalue =-returnvalue;
//}
Return returnvalue;
}