2

I've been using the following for creating a moving average list to plot in matplotlib. However, I'm needing to change to using a moving median to avoid unseemly spikes in data.

y_av = movingaverage(yclipped, avgStep)

def movingaverage(interval, window_size):
    window= np.ones(int(window_size))/float(window_size)
    return np.convolve(interval, window, 'same')

Do you all have suggestions/examples on how to change this definition to do a moving average? FYI, my avgStep/window_size is usually between 5 and 8.

Thanks!

Adam
  • 41
  • 1
  • 1
    In contrast to the mean, the median is a non-linear operation. So the moving median cannot be expressed as a simple convolution. A solution is provided in the dup-link. Alternatively, have a look at [`scipy.signal.medfilt`](https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.signal.medfilt.html). – MB-F Jan 30 '18 at 09:18

0 Answers0