Common filtering algorithm of single chip microcomputer

1. Sliding filter

1. Definition

Moving average filtering is to regard the continuously obtained N sampling values ​​as a queue , and the length of the queue is fixed at N. Each time a new data is sampled and placed at the end of the queue, and the original data at the head of the queue is discarded, the N in the queue is A new filtering result can be obtained by averaging the data. After sliding filtering, the waveform becomes smooth as a whole.

2, official

y ( n ) = 1 2 m + 1 ∑ n − m n + m x ( n − k ) y(n)=\frac{1}{2m+1} \sum_{n-m}^{n+m}x(n-k) and ( n )=2 m+11nmn+mx(nk)

In the formula: m is a positive integer, L=2m+1 is the filter order, which is equal to the width of the sliding window.

3. Code

 char value_buff[N];
 
 char i=0;
 
 char filter()
 
 {
    
    
 
    char count;
    int sum=0;
    value_buff[i++]=get_data();
    if(i==N)
        i=0;
    for(count=0;count<N;count++)
        sum=value_buff[count];
    return (char)(sum/N);
}

Where get_data() refers to the data value you collected.

4. Advantages and disadvantages

Advantages: simple algorithm, good suppression effect on periodic disturbance, high smoothness, suitable for high-frequency vibration system.

Disadvantages: The suppression effect on abnormal signals is poor, and the influence of pulse interference cannot be eliminated.

2. Arithmetic mean filter

1. Principle

Arithmetic average is carried out after taking N times of sampling values ​​continuously.

The arithmetic mean filtering algorithm is suitable for filtering signals with random interference.

2, official

y = 1 n + 1 ∑ 0 n x ( n ) y=\frac{1}{n+1} \sum_{0}^{n}x(n) y=n+110nx(n)

3. Code

char filter()
{
    
    
    int sum=0;
    for(count=0;count<N;count++)
    {
    
    
        sum+=get_data();
        delay():
    }
    return (char)(sum/N);
}

3. Weighted average filtering algorithm

1. Principle

A weighting coefficient is added on the basis of the arithmetic mean.

Part of the signal can be emphasized and another part of the signal can be resisted to improve the sensitivity to changes in sampled values.

2, official

y = ∑ i = 0 n x i C i y=\sum_{i=0}^{n}x_{i}C _{i} y=i=0nxiCi

y is the weighted average of n sampling values: Xi is the i-th sampling value; N is the number of sampling times; Ci is the weighting coefficient.

The weighting coefficient Ci reflects the proportion of various sampling values ​​in the average value.

3. Code

 char codejq[N]={
    
    1,2,3,4,5,6,7,8,9,10,11,12}; //code数组为加权系数表,存在程序存储区
 
 char codesum_jq=1+2+3+4+5+6+7+8+9+10+11+12;
 
 char filter()
 
 {
    
    
    char count;
    char value_buff[N];
    int sum=0;
    for(count=0;count<N;count++)
    {
    
    
        value_buff[count]=get_data();
        delay();
    }
    for(count=0;count<N;count++)
        sum+=value_buff[count]*jq[count];
    return (char)(sum/sum_jq);
}

4. Limiting filter

1. Principle

During the operation, two adjacent samples are subtracted to find their increment, and then the absolute value of the increment is compared with the maximum difference A allowed by the two samples. The size of A depends on the specific situation of the measured object.

If it is less than or equal to the maximum difference allowed, this sampling is valid; otherwise, the last sampling value is taken as the sample of this data.

2. Code

#define A //允许的最大差值
char data; //上一次的数据
char filter()
{
    
    
    char datanew; //新数据变量
    datanew=get_data(); //获得新数据变量
    if((datanew-data)>A||(data-datanew>A))
        return data;
    else
        return datanew;
}

3. Applicable occasions

The limiting filtering method is mainly used to process data that changes slowly, such as temperature and the position of objects. When using, the key is to choose the appropriate door limit A. Usually this can be obtained from empirical data and, if necessary, by experiment.

おすすめ

転載: blog.csdn.net/a919964703/article/details/124312749