当前位置:网站首页>Common software numerical filtering methods (I) have been applied

Common software numerical filtering methods (I) have been applied

2022-06-25 12:37:00 haoming Hu

MCU acquisition adc data , It is inevitable that the data will have great jitter at some time , At this time, a suitable numerical filtering algorithm is very good solution!

Bubble sort

Let's review the bubble sort first , Because many algorithms sort the data before processing , Here we won't explain what bubble sorting is , direct copy The following code is OK .

  for (j = 0; j <11; j++) // The array length is 11
        {
     
                for (i = 0; i < 11-j-1; i++) 
                {
     
                        if ( value_buf[i] > value_buf[i + 1] ) 
                        {
     
                                temp = value_buf[i]; 
                                value_buf[i] = value_buf[i + 1];  
                                value_buf[i + 1] = temp; 
                        } 
                } 
        } 

Median filtering

Continuous sampling N Time (N Take an odd number ) hold N The secondary sampling values are arranged according to the size, and the middle value is taken as the current effective value , It can effectively overcome the fluctuation interference caused by accidental factors 、 The measured parameters with slow change of liquid level have good filtering effect , But it's not absolute , Depending on your interval . The drawback is that it is not practical for high-speed signals , The comparison is to get the data first , Reprocessing .
Code : Here are all for the length 11 An array of .

float filter(float value_buf[]) 
{
     
	int ii;
	float x  ,temp; 
	int i,j;   
        for (j = 0; j <11; j++) 
        {
     
                for (i = 0; i < 11-j-1; i++) 
                {
     
                        if ( value_buf[i] > value_buf[i + 1] ) 
                        {
     
                                temp = value_buf[i]; 
                                value_buf[i] = value_buf[i + 1];  
                                value_buf[i + 1] = temp; 
                        } 
                } 
        } 
      return value_buf[5]; 
} 

Arithmetic average filtering

This is actually mean filtering , Continuous access N Sample values are arithmetic averaged .N When it's worth more : High signal smoothness , But the sensitivity is low ,N It's worth less : The signal smoothness is low , But the sensitivity is high , It is suitable for filtering signals with random interference
The characteristic of such a signal is that it has an average value , The signal fluctuates up and down near a certain range of values , However, it is not applicable to the real-time control with slow measurement speed or fast data calculation speed , More wasteful RAM.
To calculate the average value, there is no code posted here , This is too easy , The reason for this is that it will be used later .

Median average filtering method ( It is also called the average filtering method of anti pulse interference )

amount to “ Median filtering ”+“ Arithmetic average filtering ” Continuous sampling N Data , Take out a maximum and a minimum , And then calculate N-2 The arithmetic mean of data . For occasional impulsive interference , It can eliminate the sampling value deviation caused by pulse interference , The disadvantage is that the measurement speed is slow , It's the same as arithmetic average filtering , More wasteful RAM.
Code :

float filter2(float value_buf[])
{
    
	int ii;
	float x  ,temp; 
	int i,j;   
	float sum=0;

        for (j = 0; j <11; j++) 
        {
     
                for (i = 0; i < 11-j-1; i++) 
                {
     
                        if ( value_buf[i] > value_buf[i + 1] ) 
                        {
     
                                temp = value_buf[i]; 
                                value_buf[i] = value_buf[i + 1];  
                                value_buf[i + 1] = temp; 
                        } 
                } 
        } 
		
      for(ii=1;ii<10;ii++){
    

			sum = sum+value_buf[ii];
		}
		return (sum/9);
		 
}

In general , If there is enough time , A two-layer filter is recommended , That is to take 11 Secondary median value , Form an array , Then the array is filtered by the mean of the middle finger , The resulting data is basically very smooth !

In addition, there are many filtering methods to understand :

  • Limiting filtering ( It is also called program judgment filtering method )
  • Recursive average filtering ( It is also called moving average filtering method )*********** It is necessary to study
  • Limiting average filtering method
  • First order lag filtering
  • Weighted recursive average filtering method
  • Dithering filtering method
  • The method of limiting amplitude and eliminating dithering
原网站

版权声明
本文为[haoming Hu]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200528576371.html

随机推荐