efficient running median

Daniel Stutzbach daniel at stutzbachenterprises.com
Tue Oct 13 15:22:45 EDT 2009


On Tue, Oct 13, 2009 at 10:22 AM, Janto Dreijer <jantod at gmail.com> 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).
>

In the past, I've used the following algorithm which approximates the
running median.  I got it from an article in a peer-reviewed journal, but I
can't locate the reference at the moment.

my_median = some_reasonable_default
step_size = some_value_depending_on_your_application

def adjust_median(value):
    global my_median
    if my_median < value:
        my_median += step_size
    else:
        my_median -= step_size

Let me know if you have any questions about it.

--
Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC <http://stutzbachenterprises.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20091013/f958f154/attachment-0001.html>


More information about the Python-list mailing list