public
Last active

1D median filter using numpy

  • Download Gist
medfilt.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#!/usr/bin/env python
 
import numpy as np
 
 
def medfilt (x, k):
"""Apply a length-k median filter to a 1D array x.
Boundaries are extended by repeating endpoints.
"""
assert k % 2 == 1, "Median filter length must be odd."
assert x.ndim == 1, "Input must be one-dimensional."
k2 = (k - 1) // 2
y = np.zeros ((len (x), k), dtype=x.dtype)
y[:,k2] = x
for i in range (k2):
j = k2 - i
y[j:,i] = x[:-j]
y[:j,i] = x[0]
y[:-j,-(i+1)] = x[j:]
y[-j:,-(i+1)] = x[-1]
return np.median (y, axis=1)
 
 
def test ():
import pylab as p
x = np.linspace (0, 1, 101)
x[3::10] = 1.5
p.plot (x)
p.plot (medfilt (x,3))
p.plot (medfilt (x,5))
p.show ()
 
 
if __name__ == '__main__':
test ()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.