C Language Algorithm for Digital Filter Based on moving average method against pulse interference and Its Implementation

Source: Internet
Author: User
In many data collection systems, there are many on-site strong electrical equipment, which is inevitable.
The ground will produce sharp pulse interference, which generally lasts for a short period of time and has a large peak value.
When performing digital filtering, only arithmetic mean or moving average filtering is used.
Although 1/n is processed for pulse interference, the remaining value is still large.
The best strategy for this scenario is to remove the signal data that is considered to be disturbed.
It is the principle of the average value filtering method for anti-pulse interference.

The average filtering method for anti-pulse interference is to sort n consecutive data,
Remove the two largest and smallest data, and display the average value of the remaining data.

Generally, in the application of 8051 single-chip microcomputer, n can be set to 6 to speed up data processing.
For a processor with a high speed, the n value can be larger. But it is best
N = 2 ^ k + 2, k is an integer, because when the average = SUM/(n-2) = SUM/2 ^ k,
You can write it as average = SUM> k, and use the shift method to speed up processing.

The above algorithm obviously has a disadvantage, that is, each data collection needs to be sorted once,
This will occupy a lot of valuable time of the system. This allows you to store the maximum and minimum values of the current data.
To improve. The specific method is:
The system uses two variables to store the maximum and minimum values of the current n data in this array.
Offset (that is, the array subscript, which stores the array subscript and directly does not store the data itself because
In the system, n does not exceed the unsigned short integer representation range, so it can be stored with a char variable.
If the data itself is directly stored, int variables or even longer types are used in many cases ). This way
Only when the data to be overwritten by the input is the current maximum or minimum value
You can search for the maximum or minimum values in a group. In other cases, you only need to compare the input data with the maximum and minimum values.
You can modify the maximum and minimum values without sorting data.

This algorithm is very simple. below is the corresponding C language code implementation, which can be easily applied to the specific 51 single-chip microcomputer,
Or on other processors, only a small number of modifications are required.

# I nclude "stdio. h"
# Define dtype unsigned int // data type of the collected data
# Define uint8 char

# Define LEN 6 // Number of moving Arithmetic Averages + 2 = SHIFT <2 + 2
# Define SHIFT 2 // 2 ^ SHIFT

Uint8 pdata; // move the pointer
Uint8 pmax and pmin; // records the positions of the maximum and minimum values in the data table,
// In a general data collection system, the data length is greater than or equal to 8,
// Therefore, Use Pointer records instead of directly recording the maximum and minimum values
Dtype datas [Len];

Dtype szlb (dtype _ data)
{
/****************************/
/* This subprogram must be called before */
/* Pdata, datas [] array ,*/
/* Initialize Pmax and pmin */
/****************************/

Uint8 I;
Dtype average = 0; // reset to calculate the average value
Pdata = (pdata + 1) % Len; // pointer subscript slides from 0 to LEN-1
Datas [pdata] = _ data; // The sampled data is stored in the data table.
For (I = 0; I <Len; I ++)
Average + = datas [I]; // calculate the sum of all data

/******* Remove data that is considered as pulses ******/
If (_ DATA> datas [Pmax])
Pmax = pdata; // obtain the maximum pointer.
Else if (_ DATA <datas [pmin])
Pmin = pdata; // get the pointer to the minimum value
If (pdata = pmax) // if the current input value is saved to the position of the current maximum value
{// The above method will not work, and the extreme values must be searched from other locations
For (I = 0; I <LEN; I ++)
If (datas [I]> datas [pmax])
Pmax = I;
}
Else if (pdata = pmin) // when the current input value is saved to the position of the current maximum value
{// The above method will not work, and the extreme values must be searched from other locations
For (I = 0; I <LEN; I ++)
If (datas [I] <datas [pmin])
Pmin = I;
}
Average = average-datas [pmax]-datas [pmin]; // subtract the pulse

Return (average> SHIFT); // calculates the arithmetic average value.
}

/****** The following is the test program running in the VC ++ 6.0 environment **/

/***** Manually input to simulate the data collection process ****/

Void main ()
{
Uint8 I;
Dtype _ data;
Pdata = 0;
Pmax = 0;
Pmin = 0;
For (I = 0; I <LEN; I ++)
Datas [I] = 0;
Printf ("data: Max/MIN/n ");
While (1)
{
Scanf ("% u", & _ data );
Szlb (_ data );
For (I = 0; I <LEN; I ++)
Printf ("%-3u", datas [I]);
Printf ("%-3u %-3u", datas [Pmax], datas [pmin]);
Printf ("/N ");
}
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.