Hello! Welcome to Embedic!
This website uses cookies. By using this site, you consent to the use of cookies. For more information, please take a look at our Privacy Policy.
Home > Embedded Events > 6 microcontroller digital filtering algorithms explained

6 microcontroller digital filtering algorithms explained

Date: 31-05-2021 ClickCount: 1494

The main role of the microcontroller is to control peripheral devices and to implement certain communication and data processing. However, in some specific occasions, it is inevitable to use mathematical operations, although the microcontroller is not good at implementing algorithms and performing complex operations. The following is mainly about how to implement digital filtering with a microcontroller.

In the microcontroller for data acquisition, will encounter the random error of data, random error is caused by random interference, its characteristics are in the same conditions when measuring the same amount, its size and sign will be irregular changes and can not be predicted, but the results of multiple measurements in line with the statistical law. To overcome the errors caused by random disturbances, filtering techniques can be used in hardware, and software algorithms can be used to implement digital filtering. The filtering algorithm is often an important part of the system measurement and control algorithm, real-time.

6 microcontroller digital filtering algorithms explained

The use of digital filtering algorithms to overcome errors from random disturbances has the following advantages.

  1. Digital filtering does not require additional hardware costs, uses only one calculation process, is highly reliable, and does not have impedance matching problems. In particular, digital filtering can filter signals at very low frequencies, which cannot be done by analog filters.
  2. Digital filtering is implemented using software algorithms, and multiple input channels can share a single filtering program, reducing system expenses.
  3. The filtering characteristics can be easily changed by appropriately changing the filtering procedure or operation of the filter, which will have a greater effect on filtering out low frequency interference and random signals.
  4. The filtering algorithms commonly used in monolithic systems are limited-amplitude filtering, median filtering, arithmetic average filtering, weighted average filtering, sliding average filtering, etc.

Limit filtering algorithm

The process of the operation will be two adjacent samples are subtracted to find the increment, and then the absolute value of the increment, and the maximum difference between the two samples allowed A. The size of A is determined by the specific situation of the measured object, if less than or equal to the maximum difference allowed, the sample is valid; otherwise, the last sample value is taken as the sample of the current data.

The program code of the algorithm is as follows.

#defineA // the maximum difference allowed

chardata; //the 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;

}

Description: The limit filtering method is mainly used to deal with data that changes more slowly, such as temperature, position of objects, etc. When used, it is crucial to select the appropriate gate limit A. Usually this can be obtained from empirical data, or experimentally if necessary.

Median filtering algorithm

The process of this operation is to sample a parameter N times continuously (N is generally an odd number), and then arrange the values of the N samples from smallest to largest, and then take the middle value as the current sample 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()

{

  charvalue_buff[N]; //define the array to store the data

  char count,i,j,temp;

  for(count=0;count

  {

    value_buf[count]=get_data();

    delay(); // if the data collection is slow, 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 fluctuations caused by chance factors and pulsating disturbances due to 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.

Arithmetic average filtering algorithm

The basic principle of this algorithm is simple, which is to take N consecutive samples of values and then perform arithmetic averaging.

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 disturbances. Such signals are characterized by an average value, and the signal fluctuates up and down around a certain value. The average degree of 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. For the convenience of averaging, N is generally taken as an integer power of 2 like 4, 8, 16, 32, etc. in order to replace the division with a shift operation in the program.

Weighted average filtering algorithm

Since there is a conflict between smoothness and sensitivity in the "arithmetic average filtering algorithm" described above, it is necessary to reconcile smoothness and sensitivity. In order to reconcile smoothness and sensitivity, weighted average filtering can be used. The principle is to multiply the N consecutive samples by different weighting coefficients and then add them up. The weighting coefficients are generally smaller and then larger to highlight the effect of the later samples and to enhance the system's understanding of the trend of the parameters. Each weighting coefficient is less than a decimal of 1 and satisfies the end condition that the sum is equal to 1. The cumulative sum after such weighting operation is the effective sampling value. Where the mathematical model of the weighted average digital filtering is

Where: D is the weighted average of N sampled values: XN-i is the N-i-th sampled value; N is the number of samples; Ci is the weighting coefficient. The weighting factor Ci reflects the proportion of various sampled values in the average value. Generally speaking, the later the number of samples, the larger the proportion taken, which can increase the proportion of new samples in the average value. The weighted average filtering method can highlight a part of the signal to resist another part of the signal to improve the sensitivity to changes in the sampled values.

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 table of weighted coefficients and exists in the program memory

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);

}

Sliding average filtering algorithm

The above and various averaging algorithms have one thing in common, that is, each valid sample value must be sampled several times in succession, when the picking speed is slow, the system's real-time is not guaranteed. The sliding average filtering algorithm introduced here only samples once, and averages one sample value with several past samples to obtain a valid sample value that can be put into use. If N samples are averaged, N data staging areas must be created in the storage area. Each new data is stored in the staging area, and the oldest data is removed, so that the N data 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);

}

Low-pass filtering algorithm

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

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

where

  • Xn-The current sample value
  • Yn-1 - the last filtered output value
  • a - filtering coefficient, its value is usually much less than 1
  • Yn-the output value of the current filtering

As can be seen from the above formula, the output value of the current filter depends mainly on the output value of the last filter (note that it is not the last sampling value, which is essentially different from the weighted average filter), the contribution of the current sampling value to the filter output is relatively small, but more or less some correction effect, this algorithm simulates the specific low-pass filter function with greater inertia. The cutoff frequency of the filtering algorithm can be calculated by the following equation.

fL=a/2Pit

pi is the circumference of the circle 3.14...

where

  • a-filter coefficient
  • t - sampling interval time

Example.

When t=0.5s (i.e. 2 times per second) and a=1/32, fL=(1/32)/(2*3.14*0.5)=0.01Hz

This is effective when the target parameter is a physical quantity that changes very slowly. On the other hand, it cannot filter out the dry churning signals higher than 1/2 sampling frequency, in this case the sampling frequency is 2Hz, so the dry churning signals above 1Hz should be filtered out in other ways.

The low-pass filtering algorithm is similar to the weighted average filtering, but there are only two weighting coefficients: a and 1-a. For calculation convenience, a is taken as an integer, 1-a is replaced by 256-a, and the calculation result is rounded off to the lowest byte, because there are only two, a and 1-a, which are programmed in the form of immediate numbers, without setting up a separate table. Although the sampling value is unit byte (8-bit A/D). To ensure the accuracy of the operation, the filtered output value is expressed in double bytes, where one byte integer and one byte decimal, otherwise there is a risk that the output will not change due to rounding off the trailing number each time.

  • STM32WB15 Released, New Products in STM32 Wireless Family
  • The 8 stages of learning embedded development, methods and considerations

Hot Products

  • ADSP-BF514KBCZ-4F4

    Manufacturer: Analog Devices

    IC DSP 16/32B 400MHZ 168CSBGA

    Product Categories: DSP

    Lifecycle:

    RoHS:

  • PIC18F13K50-I/SO

    Manufacturer: Microchip

    IC MCU 8BIT 8KB FLASH 20SOIC

    Product Categories: 8bit MCU

    Lifecycle:

    RoHS:

  • ADSP-BF518BBCZ-4F4

    Manufacturer: Analog Devices

    IC DSP 16/32B 400MHZ 168CSBGA

    Product Categories: DSP

    Lifecycle:

    RoHS:

  • 0W344-005-XTP

    Manufacturer: ON Semiconductor

    DSP BELASIGNA 200 AUDIO 52-NQFN

    Product Categories: DSP

    Lifecycle:

    RoHS:

Customer Comments

  • Looking forward to your comment

  • Comment

    Verification Code * 

Compare products

Compare Empty