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
Author Message
SteveS

Joined: 27 Oct 2003
Posts: 126

Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

Re: Filters (long)
Posted: Thu Jun 03, 2004 9:37 am

 SteveS wrote: A median filter takes (generally) an odd number of inputs (5 - 7 is a good starting point), sorts them by value and takes the median (middle) value as the new input. - SteveS

Make that "and takes the median value as the new OUTPUT.
SteveS

Joined: 27 Oct 2003
Posts: 126

 Posted: Thu Jun 03, 2004 1:12 pm corrected -thanks!
SimpleAsPossible

Joined: 19 Jun 2004
Posts: 21

 Minor correction Posted: Sat Jun 19, 2004 6:13 pm Out0 = (In0 + In1 + In2 + In3) << 2; and Out0 = (In0 + Out1 + Out1 + Out1) << 2; should be Out0 = (In0 + In1 + In2 + In3) >> 2; and Out0 = (In0 + Out1 + Out1 + Out1) >> 2; to divide the sums by 4 rather than multiply. Or, if you prefer a slightly clearer (to the non-C minded) version: Out0 = (In0 + Out1 + Out1 + Out1) / 4;
SteveS

Joined: 27 Oct 2003
Posts: 126

 Posted: Thu Jul 01, 2004 9:44 am DOH! I do that all the time. I also do this: x>>2; thinking x= x>>2; Fixed.
Neutone

Joined: 08 Sep 2003
Posts: 839
Location: Houston

Variations on a theme
Posted: Mon Mar 14, 2005 10:55 am

In attempting to filter a 10 bit ADC and produce a 12 bit output signal I found I could get a good result using a different version of the "Low Pass" filter SteveS mentioned. With the elements of time and oversampeling the signal output really is more than 10 bits for a steady input. Even with oversampeling the output is not going to get much more than 2 added bits of resolution. Using a variable voltage input you can actually adjust the voltage to produce almost every 12-bit value. For the values of zero and full scale input the filter produces an error of 2 bits.

A simple version that runs very fast is this.

A version that takes more time to process but gives better filtering. The 2-bit error at end of scale is removed.

Storic

Joined: 03 Dec 2005
Posts: 182
Location: Australia SA

Re: Variations on a theme
Posted: Sun Mar 19, 2006 6:10 am

Neutone wrote:
In attempting to filter a 10 bit ADC and produce a 12 bit output signal I found I could get a good result using a different version of the "Low Pass" filter SteveS mentioned. With the elements of time and oversampeling the signal output really is more than 10 bits for a steady input. Even with oversampeling the output is not going to get much more than 2 added bits of resolution. Using a variable voltage input you can actually adjust the voltage to produce almost every 12-bit value. For the values of zero and full scale input the filter produces an error of 2 bits.

A simple version that runs very fast is this.

A version that takes more time to process but gives better filtering. The 2-bit error at end of scale is removed.

I would like to thank Neutone for the above analog filter and SteveS who started this thread, I was stuck for days tring to stop the Analog from hunting, (I was measuring a 10K thermistor and getting a variance of up to 1'C ie 25'C would range from 24'C to 26'C, now with the filter

This is my final little piece of code for a 10K thermister, in order to slow down any rapid change, I read the value 10 times and take an average, I then used 2 x old results to average out reads. before I would get the peaks of up to 1deg, now with the filter + average 10 reads and further average of 2 reads from Old_result1 and Old_result2, I see any peaks of .1 deg

 Code: /*********************************************************** *    Read Tempature 10K thermister filtered code ***********************************************************/ void Set_Temp ( ) { int16 Ref_10K_25deg = 715;   // 20=262(748) 30=237(677) int16 Intermediate_Result = 0; signed int16 Final_Result = 0; int16 min=65535; int16 max=0; int8 i; set_adc_channel(0); delay_ms(100); for(i=0; i<=10; ++i) { delay_ms(100);    Intermediate_Result=Intermediate_Result-(Intermediate_Result>>6)+read_adc();    if(Intermediate_Resultmax)       max=Intermediate_Result; }    //convert Average read to value, deduct from ref to reverse the negative action    Final_Result=(((min + max)/2)>>2)-Ref_10K_25deg;    //convert to readable temp(Final_Result + 25'C), add to old convert, round to 0.1 value    Temp_Result =(((final_Result*-13)+2500)+old_result1)/20*10;    old_result1 = Temp_Result; // get 1st result value    //convert new Temp result from Old result 1 and 2 and round to 0.1 value    Temp_Result =(old_result1 + old_result2)/20*10;    old_result2 = Temp_Result; // get 2st result value        ......// aditional error check for open/short ciruits }

Andrew
_________________
What has been learnt if you make the same mistake?
Ken Johnson

Joined: 23 Mar 2006
Posts: 197
Location: Lewisburg, WV

 Posted: Wed Sep 20, 2006 7:03 am Can't take credit - I read this somewhere - but I use this digital exponential filter a lot: Value += Filter * (newValue - Value); All variables are floats (calculation is still pretty fast). You can adapt this to "int" types if time is of the essence. Filter <= 1.0 (smaller Filter gives more filtering). Works great! Ken
mthornton

Joined: 22 Jan 2008
Posts: 4
Location: Canal Flats BC

 integer version of above averaging method Posted: Thu Feb 21, 2008 5:26 pm Value += FilterK * (newValue - Value); where FilterK <1 this works great, but requires floating point variables & math --------------------------------------------------------------------- so do this instead multiply FilterK by 10 (I often use .9, so make =9) Value = (10*Value+(9*(newValue - Value)))/10; works fine on a 10 bit value using unsigned int16 variables Also... Try playing with multipliers > 10 (works good x50). Not much higher or you overflow 16 bit unsigned values when working with 10 bit raw a/d newValue
alan69

Joined: 09 Jun 2010
Posts: 1
Location: London

 Example Median Filter Posted: Wed Jun 09, 2010 2:20 am Great thread thanks. Here's an code example for a median filter: http://www.electronic-products-development.com/embedded-programming/filters/median-filter_________________Alan www.electronic-products-development.com
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First
 All times are GMT - 6 Hours Page 1 of 1

 Jump to: Select a forum Software----------------General CCS C DiscussionCode LibraryEZ App LynxBest OfKnown Issues Hardware----------------CCS ICD / Mach X / Load-n-Go
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