efficient running median

Hendrik van Rooyen hendrik at microcorp.co.za
Wed Oct 14 02:48:51 EDT 2009


On Tuesday, 13 October 2009 17:22:55 Janto Dreijer wrote:
> I'm looking for code that will calculate the running median of a
> sequence, efficiently. (I'm trying to subtract the running median from
> a signal to correct for gradual drift).
>
> My naive attempt (taking the median of a sliding window) is
> unfortunately too slow as my sliding windows are quite large (~1k) and
> so are my sequences (~50k). On my PC it takes about 18 seconds per
> sequence. 17 of those seconds is spent in sorting the sliding windows.
>
> I've googled around and it looks like there are some recent journal
> articles on it, but no code. Any suggestions?
>
Can you not use the mean instead?  It is easier to calculate, as you do not 
have to work through all the values.

- Hendrik





More information about the Python-list mailing list