|
|
View previous topic :: View next topic |
Author |
Message |
valemike Guest
|
question for "PCM Programmer" and your median_filt |
Posted: Thu May 26, 2005 11:26 am |
|
|
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
|
|
Posted: Thu May 26, 2005 3:10 pm |
|
|
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
|
|
Posted: Thu May 26, 2005 3:20 pm |
|
|
ok thanks. i think i'll do that. |
|
|
|
|
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
|