Detailed analysis of the algorithm of single chip microcomputer digital filtering

Publisher:沈阳阿荣Latest update time:2017-07-09 Source: 21icKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 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 single-chip microcomputers to realize digital filtering.

When the single-chip microcomputer collects data, random errors will be encountered. Random errors are caused by random interference. Their 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 to implement digital filtering in software. Filtering algorithms are often an important part of the system measurement and control algorithm, and they are very real-time.

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

l Digital filtering does not require additional hardware costs, only uses one calculation process, has high reliability, and does not have impedance matching problems. In particular, digital filtering can filter very low frequency signals, which is impossible for analog filters.

l Digital filtering is implemented using software algorithms. Multiple input channels can share one filtering program, reducing system costs.

l 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.

l The commonly used filtering algorithms in the single-chip microcomputer system include limited amplitude filtering, median filtering, arithmetic mean filtering, weighted average filtering, sliding average filtering, etc.

(1) Limiting filter algorithm

During 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 this data.

The program code of the algorithm is as follows:

#define A // 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

return datanew;

}

Note: The limiting filter method is mainly used to process data that changes slowly, such as temperature, the position of an object, etc. When using it, the key is to select a suitable gate limit A. Usually this can be obtained from empirical data, and can be obtained through experiments when necessary.

(2) Median filter algorithm

The operation process is to sample a certain parameter N times continuously (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 sampling value this time. The whole process is actually a sequence sorting process.

The program code of the algorithm is as follows:

#define N 11 //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《N;count++) //Get data

{

value_buf[count]=get_data();

delay(); //If the data collection is slow, then a delay or interrupt is required

}

for(j=0;j《N-1;j++) // Sort the data using the bubble method, of course it is better to use other sorting methods

{

for(value_buff[i]》value_buff[i+1]

{

temp=value_buff[i];

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

value_buff[i+1]=temp;

}

}

return value_buff[(N-1)/2];

}

Note: 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 be more effective, but if the data changes quickly, this method is not suitable.

(3) Arithmetic mean 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:

char filter()

{

int sum=0;

for (count=0;count《N;count++)

{

sum+=get_data();

delay():

}

return (char)(sum/N);

}

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 N value. 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 filtering 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 accumulate them. The weighting coefficient is generally small at first and then large to highlight the effect of the following samplings 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. In this way, the cumulative sum after 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 number, the greater the proportion, which can increase the proportion of new samples 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 code jq[N]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; //code array is the weighted coefficient table, stored in the program storage area

char code sum_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);

}

(5) Sliding average filtering algorithm

The above introduction and various average filtering algorithms have one thing in common, that is, each valid sample value must be sampled several times continuously. When the sampling speed is slow, the real-time performance of the system cannot be guaranteed. The sliding average filtering algorithm introduced here only samples once, averages the sample value once and the past sample values ​​together, and the valid sample value obtained can be put into use. If N sample values ​​are averaged, 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. The N data saved are always the latest updated data. The use of a ring queue structure can easily implement this data storage method.

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《N;count++)

sum=value_buff[count];

return (char)(sum/N);

}

I will stop here today, because there are many other methods for digital filtering algorithms, such as first-order lag low-pass filter (inertial filtering method), time-limited filtering, fault-tolerant redundant two-out-of-three filtering method, etc. However, due to personal ability and time constraints, I have not been able to list them one by one. In the future, I will continue to find information to improve them.


Keywords:MCU Reference address:Detailed analysis of the algorithm of single chip microcomputer digital filtering

Previous article:Zhongying dual-core microcontroller (DSP and MCU) SH99F100 based on industrial control applications
Next article:Detailed Analysis of Electromagnetic Compatibility Design of Single Chip Microcomputer System

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号