"Turn" 11 kinds of filtering algorithms

Source: Internet
Author: User
Tags arithmetic

1, the Limiting filter method (also known as the Program Judgment Filter method)
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
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
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 average filter method (also known as the sliding Average filter method)
A, Method:
Consider a sequential fetch of n sampled values as a queue
The queue length is fixed to n
Each time a new data is sampled, it is placed at the end of the queue and the first data of the original team is discarded. (Advanced first Out principle)
A new filtering result can be obtained by arithmetic averaging of n data in the queue.
N-Value selection: flow, n=12; pressure: n=4; liquid level, n=4~12; temperature, n=1~4
B, Advantages:
Good inhibition of periodic interference, high smoothness
Systems for high-frequency oscillation
C, Disadvantages:
Low sensitivity
Poor inhibition of occasional impulsive disturbances
It is not easy to eliminate the sampling value deviation caused by impulse interference
Not suitable for situations where pulse interference is more severe
Compare wasted Ram

5, median value averaging filter method (also known as anti-pulse interference averaging filter method)
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
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
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
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 method
A, Method:
Set up a filter counter
Compare each sample value to the current valid value:
If the sample value = Current valid value, the counter is zeroed
Counter +1 if the sample value <> current valid value, and determine if the counter is >= upper limit n (overflow)
If the counter overflows, replace this value with the current valid value and clear the counter
B, Advantages:
The measured parameters with slow change have better filtering effect,
Avoids repeated on/off runout of the controller near the critical value or numeric jitter on the monitor
C, Disadvantages:
For rapidly changing parameters, it is not advisable
If the value sampled at the time of the counter overflow happens to be a disturbance value, the interference value is imported as a valid value into the system

10. Amplitude-limiting anti-jitter filtering method
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


11th Method: IIR Digital Filter

A. Method:
Determine the signal bandwidth, 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, band-pass, with resistance arbitrary. Simple design (with MATLAB)
C. Cons: Large computational capacity.


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

Sample C program for software filtering

Sample program for 10 software filtering methods

Assume that the data is read from 8-bit AD (if it is a higher ad definable data type of int), the subroutine is Get_ad ();

1. Filter with limited pair
/* A value can be adjusted according to the actual situation
Value is a valid value, New_value is the current sampled value
The filter returns a valid actual value */
#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 value filter method
/* N value can be adjusted according to the actual situation
Sort by bubbling 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 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);
}

4, recursive average filter method (also known as the sliding Average filter method)
/*
*/
#define N 12

Char Value_buf[n];
Char i=0;

Char filter ()
{
char count;
int sum=0;
value_buf[i++] = Get_ad (); According to the algorithm to explain, should be first-out, here are the questions online a lot of it is this way of writing, the array is full of directly from the beginning to write, and here the buffer data source is debatable

if (i = = N) i = 0; //The first data should be discarded, all other data moved forward one bit, the new data is placed at the end of the array, and then the average needs to be modified

for (count=0;count<n,count++)
Sum + = Value_buf[count];
Return (char) (sum/n);
}

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>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, the limit amplitude average filter method
/*
*/
Refer to sub-programs 1, 3

7, first-order hysteresis filter method
/* for Expedited program processing the assumption 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
/* The COE array is a weighted table, and there is a program store. */

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

9, anti-shake filter 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 anti-jitter filtering method
/*
*/
Refer to sub-programs 1, 9

11. IIR Filter Example

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

Two. 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 source image
unsigned char* lpsrc;

Pointer to the region to copy
unsigned char* lpdst;

Pointer to copy image
LPSTR lpnewdibbits;
Hlocal hnewdibbits;

Pointer to array of filters
unsigned char * avalue;
Hlocal Harray;

Loop variable
LONG i;
LONG J;
LONG K;
LONG l;

Number of bytes per line of image
LONG llinebytes;

Calculate the number of bytes per row of an image
Llinebytes = Widthbytes (Lwidth * 8);

Temporarily allocate memory to save the new image
Hnewdibbits = LocalAlloc (lhnd, llinebytes * lheight);

Determine if memory allocation failed
if (hnewdibbits = = NULL)
{
Failed to allocate memory
return FALSE;
}

Lock 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 if memory allocation failed
if (Harray = = NULL)
{
Freeing memory
LocalUnlock (hnewdibbits);
LocalFree (hnewdibbits);

Failed to allocate memory
return FALSE;
}

Lock memory
Avalue = (unsigned char *) locallock (Harray);

Start Median filter
Row (remove edges a few lines)
for (i = ifiltermy;   I < Lheight-ifilterh + ifiltermy + 1; i++)
{
column (excluding several columns of edges)
for (j = ifiltermx;   J < Lwidth-ifilterw + ifiltermx + 1; J + +)
{
Pointer to the first line of the new Dib, J-Pixel
LPDST = (unsigned char*) lpnewdibbits + llinebytes * (LHeight-1-i) + j;

Read Filter array
for (k = 0;   K < Ifilterh; k++)
{
for (l = 0;   L < Ifilterw; l++)
{
Pointer to Dib I-ifiltermy + K line, section j-ifiltermx + L pixels
LPSRC = (unsigned char*) lpdibbits + llinebytes * (lHeight-1-i + ifiltermy-k) + j -Ifiltermx + L;

Save pixel values
Avalue[k * ifilterw + l] = *lpsrc;
}
}

Get Medium value
* Lpdst = Getmediannum (avalue, Ifilterh * ifilterw);
}
}

Copy the transformed image
memcpy (Lpdibbits, Lpnewdibbits, llinebytes * lheight);

Freeing memory
LocalUnlock (hnewdibbits);
LocalFree (hnewdibbits);
LocalUnlock (Harray);
LocalFree (Harray);

Return
return TRUE;
}

Three. An implementation of RC filter.

Rcdigital (Double & X, double & Y)
{
static int midflag;
static double yn_1,xn_1;
Double mygetx=0,mygety=0;
Double Alfa;
alfa=0.7;
If (x==0| | y==0)
{
   midflag=0;
   xn_1=0;
   yn_1=0;
   mygetx=0;
   mygety=0;
}
if (x>0&&y>0)
{
   if (midflag==1)
   {
    Mygety = (1-ALFA) * Y + Alfa * yn_1;
            mygetx = (1-ALFA) * X + Alfa * xn_1;
            xn_1 = mygetx;
    yn_1 = mygety;
  }
   Else
   {
    midflag=1;
    mygetx = X;
    mygety = Y;
    xn_1 = X;
    yn_1 = Y;
  }
}
X = Mygetx;
Y = mygety;
}

"Go" 11 filtering algorithms

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.