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

Median Filter Code Problem

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



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

Median Filter Code Problem
PostPosted: Mon Mar 14, 2005 4:55 pm     Reply with quote

I'm trying to implement the median filter recommended by SherpaDoug and I'm running into an odd problem. I was hoping a second opinion might catch something I'm not.

Here is the code I'm using to toss out wild balls coming back from the PNI ASIC:
Code:

signed int16 median_filter(signed int16 value_in) {
   static signed int16 holding_array[3];
   int i = 0;
   static int counter;

   signed int16 low_val  =  32767;
   signed int16 high_val = -32768;

   int high_loc = 0;                     //location in array of high value
   int low_loc  = 0;                     //location in array of low value

   signed int16 median_val;

   holding_array[counter] = value_in;   //put incoming val in array

   if(counter < 2) {                    //increment array
      counter++;
   } else {
      counter = 0;
   }

   for(i = 0; i < 3; i++) {             //find and mark highs and lows
      if(holding_array[i] > high_val) {
         high_val = holding_array[i];
         high_loc = i;
      }
      if(holding_array[i] < low_val) {
         low_val = holding_array[i];
         low_loc = i;
      }
   }

   median_val = holding_array[3 - (high_loc + low_loc)];

   printf("1 %ld 2 %ld 3 %ld\n\r", holding_array[0], holding_array[1], holding_array[2]);
   printf(Median %ld\n\r", median_val);

   return(median_val);
   
}


Here is the output from a problem. It appears when the ASIC returns three consecutive readings that are identical.

Output:
Code:

1 -44 2 -44 3 -39
Median -44
1 -44 2 -44 3 -44
Median 32000         <------------------
1 -48 2 -44 3 -44
Median -44
1 -48 2 -42 3 -44
Median -44
1 -48 2 -42 3 -38
Median -42


I'll try to figure it out in the mean time. Thanks again for all the great help,

John
jecottrell



Joined: 16 Jan 2005
Posts: 559
Location: Tucson, AZ

View user's profile Send private message

Solved.....
PostPosted: Mon Mar 14, 2005 5:17 pm     Reply with quote

Not 10 seconds after hitting the submit button I realized the sorting routine won't assign different locations to the same values. That then goons up the median picking routine......

Sorting:
Code:

   for(i = 0; i < 3; i++) {             //find and mark highs and lows
      if(holding_array[i] > high_val) {
         high_val = holding_array[i];
         high_loc = i;
      }
      if(holding_array[i] < low_val) {
         low_val = holding_array[i];
         low_loc = i;
      }
   }



Picking: (relies on a different value, 0-2, being assigned to high_loc and low_loc)
Code:

   median_val = holding_array[3 - (high_loc + low_loc)];


All I needed to do was to change:
Code:

if(holding_array[i] > high_val)

to:
Code:

if(holding_array[i] >= high_val)

so the marker would sequence with each subsequent equal value.....



I proved to my self that the median filter worked (sometimes I'm amazed by the simplest successes.)
Code:

1 -45 2 -42 3 -41
Median -42
1 -44 2 -42 3 -41
Median -42
1 -44 2 -44 3 -41
Median -44
1 -44 2 -44 3 6275
Median -44
1 -52 2 -44 3 6275
Median -44
1 -52 2 -39 3 6275
Median -39
1 -52 2 -39 3 -41
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