Filtering algorithm (II) - median filter algorithm

1, the algorithm description

        Implementation median filter algorithm is to collect data of N cycles, to remove the N cycles the maximum and minimum data, the remaining data are averaged. Median filtering algorithm particularly suitable system will occasional outliers. Median filtering algorithm used widely, such as for some of the score of the game, is often to remove a maximum points removing a minimum points, the other scores are averaged as the final score of the player.

Advantages: compared to the mean value filter algorithm, median filtering algorithm can effectively filter out occasional glitches.

Disadvantages: the same as the average filtering algorithm, median filtering algorithm has slow response, hysteresis problems.

2, implementation code

        The following code is an example of code median filtering.

float data[10];

float middleFilter(float in_data)
{
	float sum = 0;
	float temp[10];
	float change;
	int i,j;
	//记录数据
	for(i=0; i<9; i++)
	{
		data[i]=data[i+1];
	}
	data[9] = in_data;
	//复制数据
	for(i=0; i<10; i++)
		temp[i] = data[i];
	//冒泡法排序
	for(i=1; i<10; i++)
		for(j=0; j<10-i; j++)
		{
			if(temp[i] > temp[i+1])
			{
				change = temp[i];
				temp[i] = temp[i+1];
				temp[i+1] = change;
			}
		}
	//求和
	for(i=1; i<9; i++)
		sum = sum + temp[i];
	//返回平均值
	return(sum/8);

}

        In the above code, it is divided into several steps:

Step 1: reading the new data, and updates the data array;

Step 2: Copy data to a temporary array, in order to maintain unchanged the original data sequence;

Step 3: sorted temporary array;

Step 4: Calculate the median average.

3, an example of

        Let us to appreciate the role of median filtering through one example, the vehicle speed signal to be filtered, the filtering effect as shown in FIG. FIG horizontal axis represents time, unit: second, the vertical axis represents speed in km / h. Wherein, the blue data before filtering, after filtering red data. There can be seen in FIG., There are two outliers original data, may be caused by abnormality cause interference during data acquisition or data processing. Using median filtering algorithm which can effectively filter the influence caused by outliers.

Median filtering algorithm with respect to the average filtering algorithm can not solve this problem, as shown below, is a filtering algorithm using the average effect of treatment on the same raw data, the average value of the filter can not be seen outlier filter, and the impact of outliers time longer.

 

Published 100 original articles · won praise 7 · views 10000 +

Guess you like

Origin blog.csdn.net/bhniunan/article/details/104591123