2821 views|1 replies

6555

Posts

0

Resources
The OP
 

Detailed explanation of digital filtering using single chip microcomputer [Copy link]

The main function of a single-chip microcomputer is to control peripheral devices and realize certain communications and data processing. However, in certain specific occasions, mathematical operations are inevitably used, although single-chip microcomputers are not good at implementing algorithms and performing complex operations. The following mainly introduces how to use a single-chip microcomputer to implement digital filtering. When a single-chip microcomputer collects data, it will encounter random errors in the data. Random errors are caused by random interference. Its characteristics are that when measuring the same quantity under the same conditions, its size and sign will change irregularly and cannot be predicted, but the results of multiple measurements conform to statistical laws. In order to overcome the errors caused by random interference, filtering technology can be used in hardware, and software algorithms can be used in software to implement digital filtering. Filtering algorithms are often an important part of the system measurement and control algorithm, and they are very real-time. The use of digital filtering algorithms to overcome random interference errors has the following advantages: 1. Digital filtering does not require additional hardware costs, only one calculation process, high reliability, and no impedance matching problems. In particular, digital filtering can filter signals with very low frequencies, which analog filters cannot do. 2. Digital filtering is implemented using software algorithms, and multiple input channels can share one filtering program, reducing system expenses. 3. As long as the filter's filtering procedure or operation is appropriately changed, its filtering characteristics can be easily changed, which will have a greater effect on filtering out low-frequency interference and random signals. 4. Commonly used filtering algorithms in single-chip microcomputer systems include limited amplitude filtering, median filtering, arithmetic mean filtering, weighted average filtering, and sliding average filtering. (1) Limiting filter algorithm In this 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 between the two samples. The size of A depends on the specific situation of the object being measured. If it is less than or equal to the maximum difference allowed, the current sampling is valid; otherwise, the last sampling value is taken as the sample of the current data. The program code of the algorithm is as follows: #defineA //The maximum allowed difference char data; //Last data char filter() { char datanew; //New data variable datanew=get_data(); //Get new data variable if((datanew-data)>A||(data-datanew>A)) return data; else[/backcolor ] return datanew; } Description: The limit filter method is mainly used to process data that changes slowly, such as temperature, object position, etc. When using it, the key is to select a suitable threshold limit A. Usually this can be obtained from empirical data, and can be obtained through experiments when necessary. (2) Median filter algorithm The process of this operation is to sample a certain parameter N times (N is generally an odd number), then arrange the values of the N samples from small to large, and then take the middle value as the current sampling value. The whole process is actually a sequence sorting process. The program code of the algorithm is as follows: #define N11 //Define the number of data obtained char filter() { char value_buff[N]; //Define the array to store data char count,i,j,temp; for(count=0;count { value_buf[count]=get_data(); delay(); //If the data collection is slow, then delay or interruption is required } for(j=0;j { for(value_buff>value_buff[i+1][/ color] { temp=value_buff; value_buff=value_buff[i+1];[/back color] value_buff[i+1]=temp; } } [size=4 ]return value_buff[(N-1)/2]; } Description: Median filtering is more suitable for removing fluctuations caused by accidental factors and pulsation interference caused by sampler instability. If the measured value changes slowly, the median filtering method will have a better effect, but if the data changes quickly, this method is not suitable. (3) Arithmetic average filtering algorithm The basic principle of this algorithm is very simple, which is to take N consecutive sampling values and then perform arithmetic averaging. The program code of the algorithm is as follows: charbackcolor filter()[/color ] { int sum=0; for(count=0;count { sum+=get_data(); delay(): [size= 4]} return (char)(sum/N); } [color= #000000] Description: The arithmetic mean filter algorithm is suitable for filtering signals with random interference. The characteristic of this signal is that it has an average value, and the signal fluctuates around a certain value. The average smoothness of the signal depends entirely 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 average value, N is generally taken as an integer power of 2 such as 4, 8, 16, 32, so that shift operations can be used instead of division in the program. (4) Weighted average filter algorithm Because the "arithmetic mean filter algorithm" mentioned above has a contradiction between smoothness and sensitivity. In order to coordinate the relationship between smoothness and sensitivity, weighted average filtering can be used. Its principle is to multiply the N consecutive sampling values by different weighting coefficients and then calculate the accumulation. The weighting coefficient is generally small at first and then large to highlight the effect of the following sampling and strengthen the system's understanding of the parameter change trend. Each weighting coefficient is a decimal less than 1 and meets the end condition that the sum is equal to 1. The cumulative sum after the weighted operation is the effective sampling value. The mathematical model of weighted average digital filtering is: Where: D is the weighted average of N sampling values: XN-i is the Ni-th sampling value; N is the number of samplings; Ci is the weighting coefficient. The weighting coefficient Ci reflects the proportion of various sampling values in the average value. Generally speaking, the later the sampling times are, the larger the proportion is, which can increase the proportion of the new sampling in the average value. The weighted average filtering method can highlight one part of the signal and resist another part of the signal to improve the sensitivity of the sampling 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数组为加权系数表,存在程序存储区
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) Sliding average filtering algorithm The above introduction and various average filtering algorithms have one thing in common, that is, each time a valid sample value is obtained, several samples must be continuously taken. When the sampling speed is slow, the real-time system cannot be guaranteed. The sliding average filtering algorithm introduced here only samples once, and averages the sample value once and several past sample values together, and the valid sample value obtained can be put into use. If N sample values are taken for averaging, N data temporary storage areas must be opened in the storage area. Each new data collected is stored in the temporary storage area, and the oldest data is removed at the same time, so that the N data stored are always the latest updated data. This data storage method can be easily implemented by 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 as a difference equation, and the software algorithm can be used to simulate the function of the hardware filter. After deduction, the low-pass filtering algorithm is as follows: Yn=a* Xn+(1-a) *Yn-1 In the formula, Xn is the sampling value of this time Yn-1 is the output value of the last filter; , a is the filter coefficient, its value is usually much smaller than 1; Yn is the output value of this filter. From the above formula, we can see 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 essentially different from the weighted average filtering). The contribution of this sampling value to the filtering output is relatively small, but it has some correction effect. This algorithm simulates the function of a low-pass filter with large inertia. The cutoff frequency of the filtering algorithm can be calculated by the following formula: fL=a/2Pit pi is the ratio of pi to 3.14… where a is the filtering coefficient; t is the sampling interval. For example: when t=0.5s (i.e. 2 times per second), a=1/32; fL=(1/32)/(2*3.14*0.5)=0.01HzWhen the target parameter is a physical quantity that changes very slowly, this is very effective. On the other hand, it cannot filter out the dry signal that is higher than 1/2 of the sampling frequency. In this example, the sampling frequency is 2Hz, so other methods should be used to filter out the dry signal above 1Hz. The low-pass filter algorithm is similar to the weighted average filter, but there are only two weighting coefficients: a and 1-a. For the convenience of calculation, a is an integer, and 1-a is replaced by 256-a. The calculation result discards the lowest byte, because there are only two items, a and 1-a, which are both programmed into the program in the form of immediate numbers, and no additional table is set. Although the sampling value is a unit byte (8-bit A/D). To ensure the accuracy of calculation, the filter output value is represented by two bytes, one byte for integer and one byte for decimal. Otherwise, the output may not change because the mantissa is discarded each time. Suppose Yn-1 is stored in 30H (integer) and 31H (decimal), and Yn is stored in 32H (integer) and 33H (decimal). That's all, because there are many methods for digital filtering algorithms, such as first-order lag low-pass filter (inertial filter method), time-limited filtering, fault-tolerant redundant three-out-of-two filtering method, etc.

This post is from Microcontroller MCU

Latest reply

Nice summary of methods, good stuff.  Details Published on 2019-2-11 14:55
 

2618

Posts

0

Resources
2
 
Nice summary of methods, good stuff.
This post is from Microcontroller MCU
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

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