Skip to content

Instantly share code, notes, and snippets.

@estysdesu
Last active July 21, 2019 05:51
Show Gist options
  • Save estysdesu/bf6670ac4529081c01b7a763c6a0f647 to your computer and use it in GitHub Desktop.
Save estysdesu/bf6670ac4529081c01b7a763c6a0f647 to your computer and use it in GitHub Desktop.
[Python: median filter (1d)] #median #filter #medfilt #window #python
#!/usr/bin/env python3
import window # https://gist.github.com/estysdesu/af7301fa17817c0d0ac7b5f8952c9bfe
def median_filt_1d(data, w_size):
""" Median filter for 1d data. Edges are handled with a window shift. """
windowed_data = window(data, w_size)
w_half_size = w_size//2
for i, w_data in enumerate(windowed_data):
w_data_sort = sorted(w_data)
data[i] = w_data_sort[w_half_size] # zero indexing handles shift to middle
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment