Eleven General Filter Algorithms

Source: Internet
Author: User

I. 11 General FiltersAlgorithm(Transfer)

1. Limit filtering (also knownProgramFiltering Method)
A. 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

2. Central value filtering method
A. 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

3. arithmetic mean Filtering
A. 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.

4. Recursive mean filtering (also known as moving average filtering)
A. 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.
A waste of RAM

5. Median average filtering method (also known as the average filtering method for anti-pulse interference)
A. Method:
It is equivalent to "median value filtering method" + "arithmetic mean filtering method"
Samples n data records consecutively, removing one maximum value and one minimum value.
Then calculate 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.

6. amplitude limiting average filtering method
A. 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.

7. First-order lag filtering method
A. Method:
obtain a = 0 ~ 1
filtering result = (1-A) * sampling value + A * Last filtering result
B. Advantages:
excellent restraining effect on Periodic Interference
suitable for scenarios with high volatility frequencies
C. Disadvantages:
phase lag, low sensitivity
the degree of lag depends on the value.
interference signals with a filtering frequency greater than 1/2 of the sampling frequency cannot be eliminated.
8. Weighted recursive average filtering method
BR> A. Method:
it is an improvement of the recursive average filtering method, that is, data with different weights at different time points.
generally, the closer the data is to the current time point, the larger the permission is obtained.
the higher the weight, the higher the sensitivity, but the lower the signal smoothness
B. Advantages:
suitable for systems with large pure lagging time constants
and short sampling periods
C. Disadvantages:
small pure lagging time constants, signals with long sampling period and slow changes
the current disturbance severity of the system cannot be quickly reflected, and the filtering effect is poor

9. deshake Filtering Method
A. 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.

10. amplitude limiting and jitter Filtering
A. Method:
It is equivalent to "limit filtering" + "deshake filtering"
First limit, then deshake
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

A. Method:
Determine the signal bandwidth and filter it.
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. disadvantage: large computing capacity.

//---------------------------------------------------------------------

Sample C program for Software Filtering

Sample programs for 10 software filtering methods

Assume that the data is read from the 8-bit AD (if it is a higher AD, the data type can be defined as INT), and the subroutine is get_ad ();

1. subfilter
/* 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;
Return new_value;

}

2. Central value filtering method
/* 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 <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
/*
*/

# Define N 12

Char filter ()
{
Int sum = 0;
For (COUNT = 0; count <n; count ++)
{
Sum + = get_ad ();
Delay ();
}
Return (char) (sum/N );
}

4. Recursive mean filtering (also known as moving average filtering)
/*
*/
# 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 );
}

5. Median average filtering method (also known as the average filtering method for anti-pulse interference)
/*
*/
# 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> value_buf [I + 1])
{
Temp = value_buf;
Value_buf = value_buf [I + 1];
Value_buf [I + 1] = temp;
}
}
}
For (COUNT = 1; count <N-1; count ++)
Sum + = value [count];
Return (char) (sum/(N-2 ));
}

6. amplitude limiting average filtering method
/*
*/
For details, refer to subprograms 1 and 3.

7. First-order lagging Filtering Method
/* 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;
}

8. Weighted recursive average filtering method
/* 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 <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 );
}

9. deshake Filtering Method

# 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. amplitude limiting and jitter Filtering
/*
*/
For details, refer to subprograms 1 and 9.

11. Examples of IIR filtering

Int bandpassfilter4 (INT inputad4)
{
Int returnvalue;
Int II;
Reslo = 0;
Reshi = 0;
Macs = * pdelin;
OP2 = 1068; // filtercoeff4 [4];
Macs = * (pdelin + 1 );
OP2 = 8; // filtercoeff4 [3];
Macs = * (pdelin + 2 );
OP2 =-2001; // filtercoeff4 [2];
Macs = * (pdelin + 3 );
OP2 = 8; // filtercoeff4 [1];
Macs = inputad4;
OP2 = 1068; // filtercoeff4 [0];
Macs = * pdelou;
OP2 =-7190; // filtercoeff4 [8];
Macs = * (pdelou + 1 );
OP2 =-1973; // filtercoeff4 [7];
Macs = * (pdelou + 2 );
OP2 =-19578; // filtercoeff4 [6];
Macs = * (pdelou + 3 );
OP2 =-3047; // filtercoeff4 [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;
}

2. Examples of filtering algorithms applied in image processing:

Bool winapi medianfilter (lpstr lpdibbits, long lwidth, long lheight,
Int ifilterh, int ifilterw,
Int ifiltermx, int ifiltermy)
{

// Pointer to the source Image
Unsigned char * lpsrc;

// Pointer to the region to be copied
Unsigned char * lpdst;

// Pointer to the copied Image
Lpstr lpnewdibbits;
Hlocal hnewdibbits;

// Pointer to the filter array
Unsigned char * avalue;
Hlocal harray;

// Cyclic variable
Long I;
Long J;
Long K;
Long L;

// Number of bytes per line of the image
Long llinebytes;

// Calculate the number of bytes per line of the image
Llinebytes = widthbytes (lwidth * 8 );

// Temporarily allocate memory to save new images
Hnewdibbits = localalloc (lhnd, llinebytes * lheight );

// Determine whether memory allocation has failed
If (hnewdibbits = NULL)
{
// Memory allocation failed
Return false;
}

// Lock the memory
Lpnewdibbits = (char *) locallock (hnewdibbits );

// Initialize the image as the original image
Memcpy (lpnewdibbits, lpdibbits, llinebytes * lheight );

// Temporarily allocate memory to save the Filter Array
Harray = localalloc (lhnd, ifilterh * ifilterw );

// Determine whether memory allocation has failed
If (harray = NULL)
{
// Release the memory
Localunlock (hnewdibbits );
Localfree (hnewdibbits );

// Memory allocation failed
Return false;
}

// Lock the memory
Avalue = (unsigned char *) locallock (harray );

// Start Median Filtering
// Rows (except the number of rows on the edge)
For (I = ifiltermy; I <lheight-ifilterh + ifiltermy + 1; I ++)
{
// Column (excluding the edge columns)
For (j = ifiltermx; j <lwidth-ifilterw + ifiltermx + 1; j ++)
{
// Pointer to row I and pixel J of the new Dib
Lpdst = (unsigned char *) lpnewdibbits + llinebytes * (lheight-1-I) + J;

// Read the Filter Array
For (k = 0; k <ifilterh; k ++)
{
For (L = 0; L <ifilterw; l ++)
{
// Pointer to DiB's I-ifiltermy + k row and J-ifiltermx + L Pixel
Lpsrc = (unsigned char *) lpdibbits + llinebytes * (lheight-1-I + ifiltermy-k) + J-ifiltermx + L;

// Save the pixel value
Avalue [K * ifilterw + L] = * lpsrc;
}
}

// Obtain the median value
* Lpdst = getmediannum (avalue, ifilterh * ifilterw );
}
}

// Copy the transformed Image
Memcpy (lpdibbits, lpnewdibbits, llinebytes * lheight );

// Release the memory
Localunlock (hnewdibbits );
Localfree (hnewdibbits );
Localunlock (harray );
Localfree (harray );

// Return
Return true;
}

 

Iii. Implementation of RC Filtering.

rcdigital (double & X, double & Y)
{< br> static int midflag;
static double yn_1, xn_1;
double mygetx = 0, mygety = 0;
double alfa;
Alfa = 0.7;
If (x = 0 | Y = 0)
{< br> midflag = 0;
xn_1 = 0;
yn_1 = 0;
mygetx = 0;
mygety = 0;
}< br> If (x> 0 & Y> 0)
{< br> If (midflag = 1)
{< br> mygety = (1-Alfa) * Y + Alfa * yn_1;
mygetx = (1-Alfa) * x + Alfa * xn_1;
xn_1 = mygetx;
yn_1 = mygety;
}< br> else
{< br> midflag = 1;
mygetx = x;
mygety = y;
xn_1 = X;
yn_1 = y;
}< BR >}< br> X = mygetx;
Y = mygety;
}

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.