Rolling median algorithm in C

前端 未结 13 1738
臣服心动
臣服心动 2020-11-27 09:24

I am currently working on an algorithm to implement a rolling median filter (analogous to a rolling mean filter) in C. From my search of the literature, there appear to be t

13条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-27 09:48

    Here is one that can be used when exact output is not important (for display purposes etc.) You need totalcount and lastmedian, plus the newvalue.

    {
    totalcount++;
    newmedian=lastmedian+(newvalue>lastmedian?1:-1)*(lastmedian==0?newvalue: lastmedian/totalcount*2);
    }
    

    Produces quite exact results for things like page_display_time.

    Rules: the input stream needs to be smooth on the order of page display time, big in count (>30 etc), and have a non zero median.

    Example: page load time, 800 items, 10ms...3000ms, average 90ms, real median:11ms

    After 30 inputs, medians error is generally <=20% (9ms..12ms), and gets less and less. After 800 inputs, the error is +-2%.

    Another thinker with a similar solution is here: Median Filter Super efficient implementation

提交回复
热议问题