Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4605222
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:24:10+00:00 2026-05-22T00:24:10+00:00

Does anyone know if there is a clean implementation of the Turlach rolling median

  • 0

Does anyone know if there is a clean implementation of the Turlach rolling median algorithm in C? I’m having trouble porting the R version to a clean C version. See here for more details on the algorithm.

EDIT:
As darkcminor pointed out, matlab has a function medfilt2 which calls ordf which is a c implementation of a rolling order statistic algorithm. I believe the algorithm is faster than O(n^2), but it is not open source and I do not want to purchase the image processing toolbox.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T00:24:11+00:00Added an answer on May 22, 2026 at 12:24 am

    I’ve implemented a rolling median calculator in C here (Gist). It uses a max-median-min heap structure: The median is at heap[0] (which is at the center of a K-item array). There is a minheap starting at heap[ 1], and a maxheap (using negative indexing) at heap[-1].
    It’s not exactly the same as the Turlach implementation from the R source: This one supports values being inserted on-the-fly, while the R version acts on a whole buffer at once. But I believe the time complexity is the same. And it could easily be used to implement a whole buffer version (possibly with with the addition of some code to handle R’s “endrules”).

    Interface:

    //Customize for your data Item type
    typedef int Item;
    #define ItemLess(a,b)  ((a)<(b))
    #define ItemMean(a,b)  (((a)+(b))/2)
    
    typedef struct Mediator_t Mediator;
    
    //creates new Mediator: to calculate `nItems` running median. 
    //mallocs single block of memory, caller must free.
    Mediator* MediatorNew(int nItems);
    
    //returns median item (or average of 2 when item count is even)
    Item MediatorMedian(Mediator* m);
    
    //Inserts item, maintains median in O(lg nItems)
    void MediatorInsert(Mediator* m, Item v)
    {
       int isNew = (m->ct < m->N);
       int p = m->pos[m->idx];
       Item old = m->data[m->idx];
       m->data[m->idx] = v;
       m->idx = (m->idx+1) % m->N;
       m->ct += isNew;
       if (p > 0)         //new item is in minHeap
       {  if (!isNew && ItemLess(old, v)) { minSortDown(m, p*2);  }
          else if (minSortUp(m, p)) { maxSortDown(m,-1); }
       }
       else if (p < 0)   //new item is in maxheap
       {  if (!isNew && ItemLess(v, old)) { maxSortDown(m, p*2); }
          else if (maxSortUp(m, p)) { minSortDown(m, 1); }
       }
       else            //new item is at median
       {  if (maxCt(m)) { maxSortDown(m,-1); }
          if (minCt(m)) { minSortDown(m, 1); }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know if there exists a stable version of the Scala plugin for
Does anyone know there have any other way that (by not using json_encode and
Does anyone know how to read a x.properties file in Maven. I know there
Does anyone know if there are new binaries for the castle logging facility using
Does anyone know if there is some type of tool, preferably a Firefox add
Does anyone know if there are any (ideally free) alternatives to SilkTest for automated
Does anyone know if there's a Flash (ActionScript) library for providing back button support?
does anyone know if there is a Twitter API library for the .Net Compact
Does anyone know if there is a limit to the number of applications I
Does anyone know is there a way to implement Windows Live ID authentication into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.