CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

question for "PCM Programmer" and your median_filt

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
valemike
Guest







question for "PCM Programmer" and your median_filt
PostPosted: Thu May 26, 2005 11:26 am     Reply with quote

Hi PCMProg,

A few weeks ago, i decided to use your median_filter() code that you put on this forum in the past. Basically, i'm reading my A/D continuously in my main()'s while(1) loop. There are occasions though where i have a sub-loop that may run for a 1-2 seconds, and will starve the median_filter() from running. The next time i do call the median_filter() function, I then have a few stale values in my array.

Is there a way to "flush" this array?

I figure maybe i can just invoke the function 7 times to flush out any stale data. Or should I just re-initialize the static variables inbuf_index and num_elements to zero?

Thanks,
Mike

Your original code below...
Code:

int16 median_filter(int16 latest_element)
{
    static int16 input_buffer[MEDIAN_FILTER_WIDTH];
    static char inbuf_index = 0;
    static char num_elements = 0;
    int16 sorted_data[MEDIAN_FILTER_WIDTH];
    int16 median;

    // Insert incoming data element into circular input buffer.
    input_buffer[inbuf_index] = latest_element;
    inbuf_index++;
    if(inbuf_index >= MEDIAN_FILTER_WIDTH)  // If index went past buffer end
       inbuf_index = 0;       // then reset it to start of buffer

    if(num_elements < MEDIAN_FILTER_WIDTH)
       num_elements++;

    // THIS LINE MAY NOT BE NEEDED IF SORTED DATA IS STATIC.
    memset(sorted_data, 0, MEDIAN_FILTER_WIDTH * 2);

    // Copy input data buffer to the (to be) sorted data array.
    memcpy(sorted_data, input_buffer, num_elements * 2);   // memcpy works on bytes

    // Then sort the data.
    Insertion_Sort_16(sorted_data, MEDIAN_FILTER_WIDTH);

   // During the first few calls to this function, we have fewer
   // elements in the sorted data array than the filter width.
   // So to compensate for that, we pick the median from the number
   // of elements that are available.  ie, if we have 3 elements,
   // we pick the middle one of those as the median.
   // Also, because the sort function sorts the data from low to high,
   // we have to calculate the index from the high end of the array.
   median = sorted_data[MEDIAN_FILTER_WIDTH - 1 - num_elements/2];

   return(median);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 26, 2005 3:10 pm     Reply with quote

You could add some code near the start of the routine,
and add a 2nd parameter to force an init if it's = TRUE,
and then just exit.
Code:
int16 median_filter(int16 latest_element, int8 init)
{
    static int16 input_buffer[MEDIAN_FILTER_WIDTH];
    static char inbuf_index = 0;
    static char num_elements = 0;
    int16 sorted_data[MEDIAN_FILTER_WIDTH];
    int16 median;

    if(init == TRUE)
      {
       inbuf_index = 0;
       num_elements = 0;
       return(0);
      }


Call it with the 2nd parameter = TRUE to do the init.
Code:
median_filter(0, TRUE);


Call it normally like this:
Code:
median_filter(value, FALSE);


Or, you can do it whatever way you want.
valemike
Guest







PostPosted: Thu May 26, 2005 3:20 pm     Reply with quote

ok thanks. i think i'll do that.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group