How to realize digital filtering with single chip microcomputer

Filter 18.432M

When the MCU performs data acquisition, it will encounter random errors of data. Random errors are caused by random interference. The characteristic is that when the same quantity is measured under the same conditions, its size and symbol will change irregularly and cannot be predicted. However, the results of multiple measurements are in line with statistical laws. In order to overcome the error caused by random interference, the filtering technology can be adopted on the hardware, and the software can be used to implement digital filtering on the software. The filtering algorithm is often an important part of the system measurement and control algorithm, and the real-time performance is very strong.

Using digital filtering algorithms to overcome the error of random interference has the following advantages:

1, digital filtering does not require other hardware costs, only one calculation process, high reliability, no impedance matching problem. In particular, digital filtering can filter signals with very low frequencies, which is not possible with analog filters.

2. Digital filtering is implemented using software algorithms. Multiple input channels can share a single filter program to reduce system overhead.

3. As long as the filter program or operation of the filter is appropriately changed, the filter characteristics can be easily changed, which has a great effect on filtering out low frequency interference and random signals.

4. Filtering algorithms commonly used in single-chip systems are finite-amplitude filtering, median filtering, arithmetic average filtering, weighted average filtering, and moving average filtering.

(1) Limiting filter algorithm

During the operation, two adjacent samples are subtracted, the increment is obtained, 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 condition of the object to be tested. If it is less than or equal to the maximum allowable difference, the current sampling is valid; otherwise, the last sampled value is taken as the sample of the current data.

The program code of the algorithm is as follows:

#defineA //Maximum allowed difference

Chardata; //last data

Char filter()

{

Chardatanew; //new data variable

Datanew=get_data(); //Get the new data variable

If((datanew-data)>A||(data-datanew>A))

Return data;

Else

Returndatanew;

}

Note: The clipping filter method is mainly used to process data with slower changes, such as temperature and position of objects. When using, the key is to choose the appropriate door limit A. Usually this can be obtained from empirical data and can be obtained experimentally if necessary.

(2) Median filtering algorithm

The operation process is to continuously sample a parameter for N times (N is generally an odd number), then arrange the values ​​of N samples from small to large, and then take the intermediate value as the current sample value. The whole process is actually a sequence. The process of sorting.

The program code of the algorithm is as follows:

#define N11 //Define the number of data obtained

Char filter()

{

Charvalue_buff[N]; //Define an array of stored data

Char count,i,j,temp;

For(count=0;count

{

Value_buf[count]=get_data();

Delay(); //If the data is collected slowly, then it needs to be delayed or interrupted.

}

For(j=0;j

{

For(value_buff[i]>value_buff[i+1]

{

Temp=value_buff[i];

Value_buff[i]=value_buff[i+1];

Value_buff[i+1]=temp;

}

}

Returnvalue_buff[(N-1)/2];

}

Note: Median filtering is more suitable for removing pulsation caused by accidental factors and sampler instability. If the measured value changes slowly, the median filtering method will be better, but if the data changes faster, this method should not be used.

(3) Arithmetic average filtering algorithm

The basic principle of the algorithm is very simple, that is, the arithmetic average is performed after taking N samples consecutively.

The program code of the algorithm is as follows:

Char filter()

{

Int sum=0;

For(count=0;count

{

Sum+=get_data();

Delay():

}

Return (char)(sum/N);

}

Description: The arithmetic average filtering algorithm is suitable for filtering signals with random interference. This signal is characterized by an average value that fluctuates around a certain value. The average smoothness of the signal is completely dependent on the value of N. When N is large, the smoothness is high and the sensitivity is low; when N is small, the smoothness is low, but the sensitivity is high. In order to facilitate the averaging, N generally takes an integer power of 2 such as 4, 8, 16, 32, etc., in order to replace the division with a shift operation in the program.

(4) Weighted average filtering algorithm

Since the "arithmetic average filtering algorithm" mentioned above has a contradiction between smoothness and sensitivity. To coordinate the relationship between smoothness and sensitivity, weighted average filtering can be employed. Its principle is to multiply the successive N times of sampling values ​​by different weighting coefficients and then add them up. The weighting coefficients are generally small and large, to highlight the effect of several subsequent samples, and strengthen the system's understanding of the trend of parameter changes. Each weighting coefficient is less than a fraction of 1 and satisfies an end condition where the sum is equal to one. The accumulated sum after such a weighting operation is the effective sample value. The mathematical model of weighted average digital filtering is:

Where: D is the weighted average of N sample values: XN-i is the Nith sample value; N is the number of samples; Ci is the weighting coefficient. The weighting coefficient Ci embodies the proportion of various sample values ​​in the average. Generally, the lower the number of samples, the larger the ratio is taken, which increases the proportion of new samples in the average. The weighted average filtering method highlights a portion of the signal against another portion of the signal to increase the sensitivity of the sampled value change.

The sample program code is as follows:

Char codejq[N]={1,2,3,4,5,6,7,8,9,10,11,12}; //code array is a weighting coefficient table, there is program storage area

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

{

Value_buff[count]=get_data();

Delay();

}

For(count=0;count

Sum+=value_buff[count]*jq[count];

Return(char)(sum/sum_jq);

}

(5) Moving average filtering algorithm

The above introduction has a common feature with various average filtering algorithms, that is, each acquisition of an effective sampling value must be performed several times in succession. When the mining speed is slow, the real-time of the system cannot be guaranteed. The sliding average filtering algorithm introduced here only samples once, and averages one sampling value together with several past sampling values, and the obtained effective sampling value can be put into use. If N samples are averaged, a temporary storage area of ​​N data must be opened in the storage area. Each new data is stored in the temporary storage area, and at the same time, the oldest data is removed, and the saved N data is always the latest updated data. This type of data storage can be conveniently implemented using a ring queue structure.

The program code is as follows:

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

Sum=value_buff[count];

Return (char)(sum/N);

}

(6) low pass filtering

The differential equation of the ordinary hardware RC low-pass filter is expressed by the difference equation. The software algorithm can be used to simulate the function of hardware filtering. The low-pass filtering algorithm is derived as follows:

Yn=a* Xn+(1-a) *Yn-1

Where Xn - this sampled value

Yn-1 - the last filtered output value;

, a - the filter coefficient, its value is usually much less than 1;

Yn - the output value of this filter.

It can be seen from the above formula that the output value of this filtering mainly depends on the output value of the last filtering (note that it is not the last sampling value, which is fundamentally different from the weighted average filtering). This sampling value is filtered output. The contribution is relatively small, but somewhat modified, this algorithm simulates the low-pass filter function with a specific inertia. The cutoff frequency of the filtering algorithm can be calculated by the following formula:

fL=a/2Pit pi is a pi of 3.14...

Where a - the filter coefficient;

, t - sampling interval time;

For example: when t = 0.5s (ie 2 times per second), a = 1/32;

fL=(1/32)/(2*3.14*0.5)=0.01Hz

This is very effective when the target parameter is a physically variable that changes very slowly. On the other hand, it cannot filter out the dry-stirring signal higher than 1/2 sampling frequency. In this example, the sampling frequency is 2 Hz, so the dry-stirring signal above 1 Hz should be filtered by other means.

The low-pass filtering algorithm program is similar to weighted average filtering, but the weighting coefficients are only two: a and 1-a. For the convenience of calculation, a takes an integer, 1-a is replaced by 256-a, and the result is rounded off to the lowest byte, because only two, a and 1-a, are programmed in immediate form. There is no separate form. Although the sample value is a unit byte (8-bit A/D). In order to ensure the accuracy of the operation, the filtered output value is represented by two bytes, one byte of the integer, one byte of the decimal, otherwise it is possible that the output will not change each time the mantissa is rounded off.

Let Yn-1 be stored in two units of 30H (integer) and 31H (decimal), and Yn be stored in 32H (integer) and 33H (decimal). The filtering procedure is as follows: Sub-table 6.

Test4

Once upon a time, there was a little girl named Lily. She lived in a small village with her parents and siblings. Lily was a curious and adventurous girl who loved exploring the world around her
One day, while playing in the woods, Lily stumbled upon a hidden cave. She was both scared and excited at the same time. Her curiosity got the best of her, and she decided to explore the cave.
As she entered the cave, she noticed that it was dark and damp. She could hear the sound of water dripping from the ceiling. The cave was so big that she could barely see the other end of it.


e-mail Growth Quality

Guangdong ganzhou , https://www.cn-gangdao.com