The Median filter is a non-linear digital filter that serves to suppress pulsed (non-stationary random process) interference by discarding all suspicious measurements. There are several input data and the filter calculates the median output value.
Example 1, there are measurements: [123, 407, -108, 115, 80, 150], then the median filter will return the value (115+123)/2 = 119
Or another example 2, we have values [523, 609, 453, -1, 471], then the output of the median filter is gotten 471
Median filter algorithm description:
Input data is sorted in ascending order (or descending order):
Example 1: [-108, 80, 115, 123, 150, 407]
Example 2: [-1, 453, 471, 523, 609]
Let there be N input measurements
If N is an even number then the filter calculates an arithmetic mean between the two central elements of the sorted array:
If N is an odd number then the filter takes a central element of the sorted array:
Example 2: N = 5, MedianFilter = a( (5+1)/2 ) = a(3) = 471
Notes
The Median filter should sort the input data. It is important to use a reasonable sorting algorithm. I want to refer you to the article “Overview of Sorts with C Source Codes”.
If the number of input data does not exceed 9 then the application of the Insertion Sort Method is optimal. In my C-code I will use the method. If you are working with a large amount of data, you can use, for example, Shell Sort.
2. One Modification of the Median Filter
There are still various modifications of the median filters. Here I want to describe one of the possible variant. We start with the description of the model. Let the measured data be affected by two different interferences:
Pulse noise (non-stationary random process): η(t). The source of interference can be located near the measuring device, for example, a microprocessor address or/and data bus. The median filter should eliminate measurements with large deviations from the median value
Stationary-ergodic noise (random ergodic process): ξ(t) with zero mean value E(ξ(t)) = 0. For example, it can be a fluctuating thermo noise. In this case we should calculate the arithmetic mean value using the measured data (3) and thus eliminate the error caused by the ξ(t) noise
It is important to know variance of the random process:
The algorithm will be used the parameter
Now let’s go to the description of the algorithm:
First calculate the Classic Median Filter. See previous chapter. Let the filter output be A
Next, we check all the input data of the filter for the condition:
For all values that meet this requirement, we calculate the arithmetic mean:
where K – the measurements number which satisfy the condition (7)
Suppose that the process ξ (t) has
then
Example 1: [123, 407, -108, 115, 80, 150]. ClassicMedianFilter = 119 Condition (7) is satisfied by the values: 115, 123, 150
Example 2: [523, 609, 453, -1, 471] ClassicMedianFilter = 471 Condition (7) is satisfied by the values: 453, 471
Note
The parameters ξ(t) of the random process can be changed depending on temperature, humidity and etc. In this case can be written a program which will estimate the current σ and update the Δ
3. Download the median_filter.c/h
C-source code median_filter.c/h
Functions: sint16 ClassicMedianFilter(sint16* ptrArray, uint16 arraySize); – classic median filter sint16 AdvanceMedianFilter(sint16* ptrArray, uint16 arraySize); – modification of the median filter where ptrArray – pointer on the input data arraySize – input data length return value is filter output
Support the AdvanceMedianFilter() function: #define ADVANCE_MEDIAN_DELTA 36 – definition of the Δ=3σ