[single chip microcomputer digital filtering algorithm]

Posted by kjtocool on Fri, 24 Dec 2021 13:13:21 +0100

Single chip microcomputer digital filtering algorithm

Tip: you can add the directories of all articles in the series here. You need to add the directories manually
For example, the first chapter is the use of pandas, an introduction to Python machine learning

Tip: after writing the article, the directory can be generated automatically. For how to generate it, please refer to the help document on the right

preface

Using digital filtering algorithm to overcome the error of random interference has the following advantages:
1. Digital filtering needs no other hardware cost, only one calculation process, high reliability and no impedance matching problem. In particular, digital filtering can filter the signal with very low frequency, which can not be done by analog filter.
2. Digital filtering is realized by software algorithm, and multiple input channels can share one filtering program to reduce system expenditure.
3. As long as the filtering program or operation of the filter is properly changed, its filtering characteristics can be easily changed, which will have a great effect on filtering low-frequency interference and random signals.
4. The commonly used filtering algorithms in single chip microcomputer system include finite amplitude filtering, median filtering, arithmetic average filtering, weighted average filtering, moving average filtering and so on.

1, Limiting filtering

1. Method
Judge the maximum allowable deviation A of two samples according to experience
Judgment during each new value acquisition: if the difference between the current value and the last value is < = a, it is valid this time; If the difference between the current value and the last value is > A, this time is invalid, and the last value is used instead of this time.
Or (in the process of this operation, the two adjacent samples are subtracted to calculate the increment, and then the absolute value of the increment is compared with the maximum difference A allowed by the two samples. The size of A depends on the specific situation of the tested object. If it is less than or equal to the maximum difference allowed, this sampling is valid; otherwise, the last sampling value is taken as the sample of this data.)
2. Advantages and disadvantages
Overcome pulse interference, unable to suppress periodic interference and poor smoothness.
3. Code

/* A The value is adjusted according to the actual value. The valid value is new_Value is the current sampling value, and the program returns a valid actual value */
#define A 10
char Value;
char filter()
{
  char new_Value;
  new_Value = get_ad();                                        //Get sample value
  if( abs(new_Value - Value) > A)   return Value;if((datanew-data)>A||(data-datanew>A))          //abs() function taking absolute value
  return new_Value;
}

4. Application
Generally, the amplitude limiting filtering method is mainly used to process the data with slow change, such as temperature, object position and so on. When using, the key is to select the appropriate door limit A. Usually, this can be obtained from empirical data and experiments if necessary.

2, Median filtering

1. Method
Continuous sampling N times, arranged by size
Take the intermediate value as the effective value this time
2. Advantages and disadvantages
It overcomes the fluctuation interference and has a good filtering effect on the measured parameters with slow change such as temperature, but not on the parameters with rapid change such as speed.
3. Code

#define N 11
char filter()
{
 char value_buf[N];
 char count,i,j,temp;
 for(count = 0;count < N;count++)                                //Get sample value
 {
  value_buf[count] = get_ad();
  delay();
 }
  for(i = 0;i<n-1;i++)
  {
    for(j = i+1;j<n-1;j++)
    {
          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[(N-1)/2];
}

4. Application
Note: the median filter is more suitable for removing the fluctuation caused by accidental factors and the pulsation interference caused by the instability of the sampler. If the measured value changes slowly, the effect of using median filtering method will be better, but if the data changes quickly, this method should not be used.

3, Arithmetic average filtering

1. Method
Continuous sampling N times, average
When N is large, the smoothness is high and the sensitivity is low
When N is small, the smoothness is low and the sensitivity is high
General N=12
2. Advantages and disadvantages
It is applicable to the system with random interference, occupying more RAM and slow speed.
3. Code

#define N 12
char filter()
{
 int sum = 0;
 for(count = 0;count<N;count++)
  sum += get_ad();
 return (char)(sum/N);
}

4. Application
Arithmetic average filtering 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 up and down near 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 generally takes the integer power of 2 such as 4, 8, 16 and 32, so as to replace division with shift operation in the program.

4, Median average filtering

1. Method
Sample N values and remove the maximum and minimum
Calculate the average of N-2
N= 3~14
2. Advantages and disadvantages
It combines the advantages of median value and average value
Eliminate pulse interference
Slow computing speed and large RAM occupation
3. Code

char filter()
{
 char count,i,j;
 char Value_buf[N];
 int sum=0;
 for(count=0;count<N;count++)
  Value_buf[count]= get_ad();

  for(i=1;i<N;i++)
  {
  		for(j=n-1;j>=i;j--)
  		{
  		     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(count =1;count<N-1;count++)
    sum += Value_buf[count];
   return (char)(sum/(N-2));
}

5, Amplitude limiting average filtering

1. Method
Each sampling data is limited first and then sent to the queue
Take the average value
2. Advantages and disadvantages
Integrate the advantages of amplitude limiting, mean and queue
Eliminate pulse interference and occupy more RAM
3. Code

#define A 10
#define N 12
char value,i=0;
char value_buf[N];
char filter()
{
 char new_value,sum=0;
 new_value=get_ad();
 if(Abs(new_value-value)<A)
  value_buf[i++]=new_value;
 if(i==N)i=0;
 for(count =0 ;count<N;count++)
  sum+=value_buf[count];
 return (char)(sum/N);
}

6, First order lag filtering

1. Method
Take a=0~1
This filtering result = (1-a) * this sampling + a * last result
2. Advantages and disadvantages
Good periodic interference, suitable for occasions with high fluctuation frequency
Low sensitivity and phase lag
3. Code

/*In order to speed up program processing, take a=0~100*/
#define a 30
char value;
char filter()
{
 char new_value;
 new_value=get_ad();
 return ((100-a)*value + a*new_value);
}

7, Weighted recursive average filtering

1. Method
For the improvement of recursive average filtering, different weights are given to the data at different times. Generally, the newer the data, the greater the weight. In this way, the sensitivity is high, but the smoothness is low.
2. Advantages and disadvantages
It is suitable for systems with large lag time constant and short sampling period. The signals with small lag time constant, long sampling period and slow change can not quickly respond to their interference.
3. Code

/* coe The array is a table of weighting coefficients */
#define N 12
char code coe[N]={1,2,3,4,5,6,7,8,9,10,11,12};
char code sum_coe={1+2+3+4+5+6+7+8+9+10+11+12};
char filter()
{
 char count;
 char value_buf[N];
 int sum=0;
 for(count=0;count<N;count++)
 {
  value_buf[count]=get_ad();
 }
 for(count=0;count<N;count++)
  sum+=value_buf[count]*coe[count];
 return (char)(sum/sum_coe);
}

8, Dithering filtering

1. Method
Set a filter counter
Compare the sample value with the currently valid value
If the sampling value = the current valid value, the counter is cleared to 0
If the sampling value is not equal to the current valid value, the counter + 1
If the counter overflows, the sampling value replaces the current valid value, and the counter is cleared to 0
2. Advantages and disadvantages
The filtering effect is good for signals with slow change, but bad for signals with fast change
Avoid jumping near the critical value. When the counter overflows, if the interference value is collected, it cannot be filtered
3. Code

#define N 12
char filter()
{
 char count=0,new_value;
 new_value=get_ad();
 while(value!=new_value)
 {
  count++;
  if(count>=N) return new_value;
  new_value=get_ad();
 }
 return value;
}

9, Amplitude limiting anti chattering filter

1. Method
First limit amplitude and then eliminate chattering
2. Advantages and disadvantages
It combines the advantages of amplitude limiting and chattering elimination
Avoid introducing interference value, and it is not suitable for rapidly changing signals
3. Code

#define A 10
#define N 12
char value;
char filter()
{
 char new_value,count=0;
 new_value=get_ad();
 while(value!=new_value)
 {
  if(Abs(value-new_value)<A)
  {
  count++;
  if(count>=N) return new_value;
  new_value=get_ad();
  }
 return value;
 }
}

Topics: Algorithm Single-Chip Microcomputer