MCU ADC sampling algorithm ---- first-order low-pass filtering

Publisher:Lihua521Latest update time:2020-04-01 Source: eefocusKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Regarding low-pass filtering, let’s first look at the definition on Baidu Encyclopedia.

The formula for algorithm implementation is as follows:


y(n) = q*x(n) + (1-q)*y(n-1)    

Where Y(n) is the output, x(n) is the input, y(n-1) is the last output value, and q is the filter coefficient. The value range is 0--1.


That is to say, if q=0.5, this formula means taking 50% of the current sampling value plus 50% of the previous sampling value as the current sampling result. In other words, each sampling result is related to the previous sampling result.


Take a look at the implementation of C code in the microcontroller


//Parameter: com is the original value of the sample

//Return value: iData sample value after first-order filtering

unsigned int lowV( unsigned int com )

{

    static unsigned int iLastData; //Last value

    unsigned int iData; //this calculation value

    float dPower = 0.1; //Filter coefficient

    iData = (com * dPower) + (1 - dPower) * iLastData; //Calculation

    iLastData = iData; //Store this data

    return iData; //Return data

}

 

//Main function

void main( void )

{

  while( 1 )

    {

        val1 = ReadVol_CH3() ; // Read AD sampling value   

        val3 = lowV( val1 ); // The sampled value passes through the first-order filtering algorithm

        printf("A%drn",val1); // print sampling value

        printf("B%drn",val3); // Print the sample value after filtering algorithm

    }

}


The input voltage is sampled through the ADC, and then the sampled value is filtered through a first-order filter. The sampled value and the value after the first-order filter are printed out through the serial port.


Through the serial port waveform display software, you can see the sampling results are:

When the filter coefficient q=0.1, the current sampling data accounts for 10% of the sampling result, and the previous sampling data accounts for 90% of the sampling result. That is to say, when the sampling data changes suddenly, it has little impact on the sampling result, and the sampled waveform is relatively smooth.


It can also be seen from the waveforms above that the blue waveform is the original data waveform, which has a relatively large fluctuation range, and the orange waveform is the waveform after the first-order filtering algorithm, which is relatively stable.


When the q value is changed to 0.5, take a look at the sampling situation.

It can be seen that when the filter coefficient increases to 0.5, the influence of the current sampling data and the previous sampling data on the result is 50% respectively. From the waveform, it can be seen that the waveform after filtering also fluctuates, but the fluctuation range is a little smaller than that of the original waveform.


Continue to increase the q value to 0.9 and look at the sampling situation.

It can be seen from the waveform that after the sampling coefficient is increased, the sampling data this time accounts for 90% of the influence on the sampling result. The waveform after the first-order filtering is basically synchronized with the original waveform, and the real-time performance is better, but the stability is slightly worse.


By comparing different filter coefficients, we found that:


The smaller the filter coefficient, the more stable the filtering result, but the lower the sensitivity;


The larger the filter coefficient, the higher the sensitivity, but the more unstable the filtering result.


In practical applications, appropriate filter coefficients are selected according to different needs to meet system requirements.

Keywords:MCU Reference address:MCU ADC sampling algorithm ---- first-order low-pass filtering

Previous article:MCU ADC sampling algorithm ---- Kalman filter
Next article:MCU ADC sampling algorithm ---- effective value sampling method

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号