35463 views|32 replies

693

Posts

7

Resources
The OP
 

11 classic software filtering algorithms and their waveform effects (with C language program) [Copy link]

 
 
Classic software data filtering algorithm (C language program attached on the back page) Note: (the red lines in the image are all filtered) 1. Limiting filtering method (also known as program judgment filtering method) A. Method: Based on experience, determine the maximum allowable deviation between two samples (set to A) Every time a new value is detected, it is judged:
If the difference between this value and the previous value is <= A, then this value is valid
If the difference between this value and the previous value is > A, then this value is invalid, abandon this value, and use the previous value to replace this value
B. Advantages:
Can effectively overcome the pulse interference caused by accidental factors
C. Disadvantages
Cannot suppress the periodic interference smoothness difference
2 , Median filtering method
A. Method:
Continuous sampling N times (N 14pt]3. Arrange the N sampling values in order of size and take the middle value as the effective value of this time. B. Advantages: It can effectively overcome the fluctuation interference caused by accidental factors. It has a good filtering effect on the measured parameters with slow changes in temperature and liquid level. C. Disadvantages: It is not suitable for parameters with fast changes such as flow rate and speed. 370516 3. Arithmetic average filtering method A. Method: Continuously take N The arithmetic average operation is performed on the sampling values. When the value of N is large: the signal smoothness is higher, but the sensitivity is lower. When the value of N 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 general random interference. The characteristic of such signals is that there is an average value, and the signal fluctuates around a certain numerical range. C. Disadvantages: It is not suitable for real-time control with slow measurement speed or fast data calculation speed, which is wasteful. RAM3705264, Recursive average filtering (also known as sliding average filtering)A. Method: Consider the N consecutive sample values as a queue. The length of the queue is fixed to 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 average calculation on the N data in the queue.Then you can get a new filtering result. Selection of N value: flow, N=12; pressure: N=4; liquid level, N=4~12; temperature, N=1~4. B. Advantages: Good suppression of periodic interference, high smoothness, suitable for high-frequency oscillation systems. C. Disadvantages: Low sensitivity. Poor suppression of occasional pulse interference. Difficult to eliminate the sampling value deviation caused by pulse interference. Not suitable for occasions with serious pulse interference. Relatively wasteful. RAM. 370527. 5. Median average filtering method (also known as anti-pulse interference average filtering method) A. Method: Equivalent to "median filtering method" + "arithmetic average filtering method" Continuously sample N data, remove a maximum value and a minimum value, and then calculate the arithmetic mean of N-2 data. Selection of value: 3~14 B. Advantages: It combines the advantages of the two filtering methods. For occasional pulse interference, it can eliminate the sampling value deviation caused by pulse interference. C. Disadvantages: The measurement speed is slow, and it is wasteful of RAM like the arithmetic average filtering method.
6, Limited Average Filtering Method A. Method: It is equivalent to "Limited Filtering Method" + "Recursive Average Filtering Method" Each time the new data is sampled, it is first limited. Then send it to the queue for recursive average filtering
B. Advantages:
Combines the advantages of the two filtering methods
For occasional pulse interference, it can eliminate the sampling value deviation caused by pulse interference
C. Disadvantages:
Relatively wasteful RAM
7、First-order lag filtering method
A. Method:
Take a=0~1
The result of this filtering = (1-a) * this sampling value + a * last filtering result
B. Advantages:
It has a good suppression effect on periodic interference and is suitable for occasions with high fluctuation frequency
C. Disadvantages:
Phase lag and low sensitivity
The degree of lag depends on the size of the a value
It cannot eliminate the interference signal whose filtering frequency is higher than 1/2 of the sampling frequency
8、Weighted recursive average filtering method
A、Method:
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 sampling value, the higher the sensitivity, but the lower the signal smoothness
B、Advantages:
It is suitable for objects with large pure lag time constant and systems with short sampling period
C、Disadvantages:
For signals with small pure lag time constant, long sampling period and slow change, it cannot quickly reflect the severity of the current interference of the system, and the filtering effect is poor
9. Anti-jitter filtering method A. Method: 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 added by 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 valid value and the counter will be cleared. B. Advantages: It has a good filtering effect for the slowly changing measured parameters, and can avoid the repeated on/off jump of the controller or the jitter of the value on the display near the critical value. C. Disadvantages: It is not suitable for fast changing parameters. If the value sampled at the time when the counter overflows happens to be an interference value,The interference value will be imported into the system as a valid value.
10、Amplitude limiting and de-jittering filtering method
A. Method:
Equivalent to "amplitude limiting filtering method" + "de-jittering filtering method". Limit first, then de-jittering
B. Advantages:
Inherits the advantages of "amplitude limiting" and "de-jittering"
Improves some defects in the "de-jittering filtering method" to avoid importing interference values into the system
C. Disadvantages:
For fast changing parameters, it is not suitable
11IIR Digital filter
A. Method:
Determine the signal bandwidth, filter it.
Y(n) = a1*Y(n-1) + a2*Y(n-2) + ... + ak*Y(nk) + b0*X(n) + b1*X(n-1) + b2*X(n-2) + ... + bk*X(nk) B. Advantages: high pass, low pass, band pass, band stop are arbitrary. Simple design (using matlab) C. Disadvantages: large amount of calculation. C Program Sample for Software Filtering Example programs for 11 software filtering methods Assume that data is read from an 8-bit AD (if it is a higher-bit AD, the data type can be defined as int), the subroutine is get_ad(); 1, limited sub-filter /* The value of A can be adjusted according to actual conditions. value is a valid value, new_value is the current sampling value. The filtering program 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;
else return new_value;
}
2, median filtering method
/* N The value can be adjusted according to the actual situation. The sorting method uses the 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;jMethod: Determine the signal bandwidth, and filter it. Y(n) = a1*Y(n-1) + a2*Y(n-2) + ... + ak*Y(nk) + b0*X(n) + b1*X(n-1) + b2*X(n-2) + ... + bk*X(nk) B. Advantages: High pass, low pass, band pass, band stop are arbitrary. Simple design (using matlab) C. Disadvantages: large amount of calculation. C Program Sample for Software Filtering Example programs for 11 software filtering methods Assume that data is read from an 8-bit AD (if it is a higher-bit AD, the data type can be defined as int), the subroutine is get_ad(); 1, limited sub-filter /* The value of A can be adjusted according to actual conditions. value is a valid value, new_value is the current sampling value. The filtering program 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;
else return new_value;
}
2, median filtering method
/* The N value can be adjusted according to the actual situation, and the sorting method uses the bubble method*/
[ align=left]#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;jMethod: Determine the signal bandwidth, and filter it. Y(n) = a1*Y(n-1) + a2*Y(n-2) + ... + ak*Y(nk) + b0*X(n) + b1*X(n-1) + b2*X(n-2) + ... + bk*X(nk) B. Advantages: High pass, low pass, band pass, band stop are arbitrary. Simple design (using matlab) C. Disadvantages: large amount of calculation. C Program Sample for Software Filtering Example programs for 11 software filtering methods Assume that data is read from an 8-bit AD (if it is a higher-bit AD, the data type can be defined as int), the subroutine is get_ad(); 1, limited sub-filter /* The value of A can be adjusted according to actual conditions. value is a valid value, new_value is the current sampling value. The filtering program 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;
else return new_value;
}
2, median filtering method
/* N The value can be adjusted according to the actual situation. The sorting method uses the 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;jtemp;
for ( count=0;count { value_buf[count] = get_ad(); delay();
}
[align=left ]for (j=0;jtemp;
for ( count=0;count { value_buf[count] = get_ad(); delay();
}
[align=left ]for (j=0;j{
if" align][align="left]{
for" 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
/*
*/
#define N 12 char filter()
{
int sum = 0;
for ( count=0;count{
if" (j="0;j<N-1;j++)" )="" +="get_ad();" 12[="" =="" [="" [align="left]int" align]="" align][align="left]{
sum" align]4、递推平均滤波法(又称滑动平均滤波法)
/*
*/
#define' char="" count="0;count<N;count++)" count,i,j;[="" count;="" delay();[="" filter()[="" get_ad();="" i="0;
" if="" int="" n="" n);[="" return="" sum="0;
value_buf[i++]" value_buf[count]="get_ad();" value_buf="" value_buf[n];="" value_buf[n];[="" {="" 冒泡法[="">value_buf[i+1] )
{
temp = value_buf; value_buf = value_buf[i+1]; value_buf[i+1 ] = temp;
}
}
}
for ( count=0;count{
count++;
if" #define="" (="" (100-a)*value="" (char)(sum="" (count="0,count" (n-2));[="" (value="" *="" +="" 100,a="0~100" 12="" 12[="" 1、3[="" 50[="" =="" [="" [align="left]char" 参考子程序" a="" a*new_value;[="" align][align="left]}
" align]6、限幅平均滤波法
/*
*/
' char="" code="" coe="" coe[n]="{1,2,3,4,5,6,7,8,9,10,11,12};
" count="0;count<N;count++)" count;[="" delay();[="" filter()[="" get_ad();="" int="" n="" new_value="get_ad();
return" new_value;="" new_value;[="" return="" sum="value_buf[count];" sum_coe="1+2+3+4+5+6+7+8+9+10+11+12;
" sum_coe);[="" value;[="" value_buf[count]="get_ad();" value_buf[n];="" while="" {="" 数组为加权系数表,存在程序存储区。*="">=N) return new_value; delay();new_value = get_ad();}return value;}10、Amplitude limiting and de-jittering filtering method/***/omitted Reference subroutine 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;
}

图片1.png (49.83 KB, downloads: 1)

a

a

b.png (64.24 KB, downloads: 1)

b

b

c.png (56.08 KB, downloads: 2)

c.png
This post is from Innovation Lab

Latest reply

It looks so cool. When can I learn this technique?   Details Published on 2024-9-3 10:40

赞赏

2

查看全部赞赏

 
 

1368

Posts

6

Resources
2
 
Good stuff, thanks for sharing, I saved it first ^_^
This post is from Innovation Lab
Personal signature专注智能产品的研究与开发,专注于电子电路的生产与制造……QQ:2912615383,电子爱好者群: void
 
 
 

693

Posts

7

Resources
3
 
This post is from Innovation Lab
 
 
 

693

Posts

7

Resources
4
 
This post is from Innovation Lab
 
 
 

10

Posts

1

Resources
5
 
Good stuff, thanks for sharing, saved it first^_
This post is from Innovation Lab
 
 
 

682

Posts

1

Resources
6
 
Great stuff, thanks for sharing!!!
This post is from Innovation Lab
 
 
 

693

Posts

7

Resources
7
 
For your convenience, I share a PDF file 11种经典软件滤波算法及其波形效果图(内附C语言源代码).pdf (826.43 KB, downloads: 159)
This post is from Innovation Lab

Comments

Really well organized information  Details Published on 2019-3-9 20:13
 
 
 

122

Posts

2

Resources
8
 
Remove the highest score and the lowest score, and then average the scores... I usually use this algorithm, and today I learned that it is called the median average filtering method.
This post is from Innovation Lab

Comments

Hehe, this algorithm is better. You have been using the right algorithm.  Details Published on 2018-8-22 20:13
 
 
 

693

Posts

7

Resources
9
 
lcn1992 posted on 2018-8-22 17:30 Remove the highest score and the lowest score, and then average the score. . . I usually use this algorithm, and today I learned that it is called the median average filter method.
Hehe, this algorithm is better, you have been using the right algorithm
This post is from Innovation Lab
 
 
 

321

Posts

1

Resources
10
 
The one I use most is 4. Recursive average filtering (also known as sliding average filtering), which is actually the so-called integral operation, the I of the PID algorithm. When I use it, in order to shorten the code execution time, I usually define sum as a static variable. When initializing, the queue and sum values are cleared to zero. Every time a new value is collected, the old value to be replaced is first subtracted from the sum, and then the old value in the queue is replaced with the new value, and the sum is added to the new value. In this way, after collecting a new value, only three steps of subtraction, assignment, and addition are required, which saves the queue loop summation and greatly shortens the code execution time.
This post is from Innovation Lab

Comments

The above method has a hidden danger. If an error occurs during the sum update process and a number that has not been added to the sum is subtracted (that is, the queue data is accidentally destroyed), then the sum value of each subsequent calculation will be an incorrect value and will not be corrected even after all the data in the queue is updated. If this method is used every time  Details Published on 2018-8-24 11:03
Personal signature模电临时工
 
 
 

321

Posts

1

Resources
11
 
shipeng posted on 2018-8-24 08:42 The one I use most is 4. Recursive average filtering (also known as sliding average filtering), which is actually the so-called integral operation, the I of PID algorithm. I am using...
The above method has a hidden danger. If an error occurs during the sum update process and a number that has not been added to the sum is subtracted (that is, the queue data is accidentally destroyed), then each subsequent calculation sum value will be an incorrect value and will not be corrected even after all the data in the queue is updated. However, if the queue loop sum is used each time, there will be no such problem.
This post is from Innovation Lab
Personal signature模电临时工
 
 
 

33

Posts

3

Resources
12
 
Very good, already collected
This post is from Innovation Lab
 
 
 

47

Posts

0

Resources
13
 
Good information, saved it!
This post is from Innovation Lab
 
 
 

2

Posts

0

Resources
14
 
IIR filtering and the like should not be used by us. I also saw some Kalman filtering and data fusion a few days ago. They seemed very complicated. I wonder if they are useful.
This post is from Innovation Lab
 
 
 

693

Posts

7

Resources
15
 
This post was last edited by bqgup on 2018-10-22 16:31 数据处理上位机.rar (2.12 MB, downloads: 324) 数据处理.rar (121.22 KB, downloads: 75) I would like to share some more useful information with you.

数据处理.rar

121.22 KB, downloads: 43

This post is from Innovation Lab

赞赏

1

查看全部赞赏

 
 
 

869

Posts

0

Resources
16
 
For your convenience, here is a PDF file of 11 classic software filtering algorithms and their waveform effects.
This post is from Innovation Lab
 
 
 

650

Posts

8

Resources
17
 
Thanks for sharing, it is very useful
This post is from Innovation Lab
 
 
 

1903

Posts

0

Resources
18
 
Classic enough
This post is from Innovation Lab
 
 
 

19

Posts

0

Resources
19
 
Thanks for sharing,,,,,I use it a lot,,,,,very useful. . . . .
This post is from Innovation Lab
Personal signature好好学习,天天向上
 
 
 

33

Posts

0

Resources
20
 
Not bad, not bad, I'll save it and look at it after the exam.
This post is from Innovation Lab
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list