In 10 kinds of classical software filtering algorithms, we can see that many algorithms are the average filter algorithm variant, in fact, the most commonly used is also the average filter algorithm. But the traditional average filter algorithm is the memory, each operation is required to accumulate and, then averaged, resulting in inefficient operation.
Today introduced a super-concise ultra-efficient average filter algorithm, this algorithm is used to make a single-chip machine a teacher created (single-chip memory is simply the amount of land in the gold), only with three variables, the average filter is completed the calculation. Just beginning to see this algorithm is only feel very admire, later with a variety of algorithms, only to feel this algorithm almost to the point of craftsmanship (do not think after reading the thought too simple nothing big, it is because too simple to highlight its great, The first thing you can think of is to simplify a complex algorithm to the point where it is not normal to be able to do it.
On this basis, we have developed a practical algorithm of queue averaging with dead zone and limited amplitude control.
Sample Value C, accumulator S, average A, number of samples N
The traditional average filter algorithm:
S = C (1) + C (2) + ... + C (N)
A = S/n
It takes a loop to calculate the sum and it is time consuming, C (1~n) is the cache, and the amount of memory increases with the number of samples N
Advance to the queue average algorithm:
S = C (1) + C (2) + ... + C (N) (first time)
C (x) = C (x + 1) (Queue forward)
C (N) = C
S = s-c (1) + C (N)
A = S/n
Improvements in computation (maintaining circular queues with pointers, not physically moving data), memory-intensive issues
Craftsmanship algorithm:
Initialize: a= initial value, S=a*n
S = s-a + C
A = S/n
As simple as that, three variables (n can be constants), as long as the range of S is sufficient, n can be arbitrarily adjusted.
As can be seen, this algorithm evolved from the queue average algorithm, because there is no queue, each time the calculation is not aware of the oldest one of the sample value is discarded, here is an alternative to discard the last calculated average.
The cache maintenance is eliminated, the memory space is saved, the computation is compressed to the minimum, and the execution efficiency is very high. Easy to modify the number of samples when debugging.
Optimization algorithm:
The core idea of this algorithm is the average filter, although it improves the computation and memory consumption, but also inherits the characteristics of good smoothness and slow response when the mean value filter is large.
Therefore, the algorithm introduces the concept of dead-zone in the filter program of S7-200 system: when the sampling value deviation is within the dead zone, the filter is calculated, and the sampling value deviation is used directly outside the dead zone to achieve the fast response effect.
Re-fusion limiting filter method to remove the accidental interference pulse: The sampling value deviation within the limit range, the filter calculation, the sampling value deviation in the limit range of the direct discard, using the last filter output value. Obviously, the limit value should be greater than the dead-zone value.
This algorithm is written as two sub-functions (also can be made into a library)
① Main Filter Program Avefilter
Entry parameters:
EN: Call Enable bit
Btype: Sample value type, ' W ' = integer, ' F ' = float, ' D ' (or other) = Long integer, parameter type: Byte
WHi: Sample Value high word (sample value is integer, argument must be 0), parameter type, 2 bytes
Wlo: Sample Value low word, parameter type, 2 bytes
Rdie: Filter dead-Zone, parameter type: floating-point number
Rmaxerr: Maximum allowable deviation, parameter type: floating point number
Rlen: Filter Queue Length, parameter type: floating point number
Out/Entry parameters:
Rsum: Additive and, Parameter type: floating-point number
RAve: Filter Output average, parameter type: floating point
Command line: Call Avefilter, ' W ', 0, SMW28, 640.0, 32000.0, 4.0, VD0, VD4
Note: The sample value of this program is adaptive to the parameter type, with the combination of whi/wlo to adapt to integral type, long integer type, floating point type of parameter type input, avoid using multiple identical subroutines to adapt to different types of input parameters. The input parameter type is specified by Btype.
② Filter Initialization Program Initfilter
Entry parameters:
EN: Call Enable bit
Rinit: Initial value (typically 0), parameter type: floating-point number
Rlen: Filter Queue Length, parameter type: floating point number
Out/Entry parameters:
Rsum: Additive and, Parameter type: floating-point number
RAve: Filter Output average, parameter type: floating point
Command line: Call Initfilter, 0.0, 4.0, VD0, VD4
Algorithm of average filtering of "lower computer Software"