Median Averaging


Recommended Posts


Is there anyway you could write a sequence/function for doing a median average??

I have a signal from a mass flow meter that I'm "mean" averaging currently, but I still get horrific fluctuations. On wikipedia I saw about using Median Averaging instead ( stating:

From a statistical point of view, the moving average, when used to estimate the underlying trend in a time series, is susceptible to rare events such as rapid shocks or other anomalies. A more robust estimate of the trend is the simple moving median over n time points:

	SMM = Median( p_M, p_{M-1}, ..., p_{M-n+1} )

where the median is found by, for example, sorting the values inside the brackets and finding the value in the middle.

Statistically, the moving average is optimal for recovering the underlying trend of the time series when the fluctuations about the trend are normally distributed. However, the normal distribution does not place high probability on very large deviations from the trend which explains why such deviations will have a disproportionately large effect on the trend estimate. It can be shown that if the fluctuations are instead assumed to be Laplace distributed, then the moving median is statistically optimal[12]. For a given variance, the Laplace distribution places higher probability on rare events than does the normal, which explains why the moving median tolerates shocks better than the moving mean.

When the simple moving median above is central, the smoothing is identical to the median filter which has applications in, for example, image signal processing.

Anyway, I noticed there isn't an pre-programmed Median function, like the Mean function that is present.

Any chance you could write on?

I wouldn't have a clue how to incorporate it.

Thanks in advance,


Link to comment
Share on other sites

  • 4 years later...

A quick followup: here's how to do it using existing functions:


You need several lines of script.  Let's say you have an array "X" that you want to find the median for:

x.time = x
global theMedian = sortTime(x)[numrows(x)/2]
This is using a trick of putting the actual values as time and then sorting on time.
Link to comment
Share on other sites


This topic is now archived and is closed to further replies.