On Sun, 28 Apr 2002, Andy Shaw wrote: > Hi Scott, > > From: "Scott Dattalo" > > > > I think this may be what you're looking for: > > > > http://www.dattalo.com/technical/software/pic/medfilt.asm > > > > Thanks for this it looks interesting. However I don't think the code you > gave a pointer to does what you just described (it seems to do a lot more!). > The code you have given a pointer to seems to maintain a secondary set of > pointers to 16 bit data (not 12 bit) that are used to maintain a sorted > version of the circular buffer. Then when you want to obtain the actual > output of the filter you can drop an arbitary number of entries from the low > and high end of the sorted data (as defined by high index and low index) the > remaining middle data points are then summed and used to provide an average > of these points. I may have got this wrong but I think this is what the code > does (and it seems to do it very well). This would appear to be a superset > of your described algorithm did you by any chance refine one into the other? Yeah, you're right. I forgot that the nibble manipulation stuff was to deal with 4-bit pointers and not 12-bit data. > Anyway thanks a lot for the input. > > Refering to your originnally described algorithm I have been thinking along > similar lines. In psudoe code I was thinking of doing the following > > add_data_point(val) > /* maintain simple circular buffer of ARRAY_SIZE data items */ > data[index] = val > index = index + 1 > if (index >= ARRAY_SIZE) index = 0 > > > get_filter_value() > /* get hold of the average of the data points dropping the highest and > lowest sort of comined median and average filter */ > low = MAX_VALUE > high = MIN_VALUE > sum = 0; > for(i=0; i<=ARRAY_SIZE;i++) > sum = data[i] + sum > if (data[i] > high) high = data[i]; > if (data[i] < low) low = data[i]; > sum = sum - high - low; > return sum/(ARRAY_SIZE - 2) > > I think that the above is equivalent to the mechanism you have described > except that it does more work when the data is needed rather than when it is > inserted. Is this correct? Or have I got things totally wrong? I guess it depends on what you want to do with the data. In my case, I needed to make decisions on the data fairly rapidly. If you wait until N samples are accumulated then you have to do the sort followed by the average. However, if you're throwing away only the high low values, then the C-snippet you write above would be ideal. Here's a slight optimization to it: low = min(data[0],data[1]); high = max(data[0],data[1]); sum = 0; for(i=2; i high) { sum += high; high = data[i]; } else if(data[i] < low) { sum += low; low = data[i]; } else { sum += data[i]; } } return sum/(ARRAY_SIZE - 2) Scott -- http://www.piclist.com hint: The PICList is archived three different ways. See http://www.piclist.com/#archives for details.