The first 10 would be tossed but the second 10 would not because now the pairs are 10,10,11...
The first 10 would be tossed but the second 10 would not because now the pairs are 10,10,11...
Dave
Always wear safety glasses while programming.
Aah...ok, gotcha.
Actually my example 'data series' wasn't a good one, as the data values would actually be in the hundreds (vs units which I used), which allows percentages to be used.
So, assuming a series of data coming into a variable called 'sample'...
would something like the above work?#Code:' compare a pair called value & value2 from the incoming 'sample' value = sample ' upper_window = value2+(value2*4/100) 'create a window 4% above previous sample lower_window = value2-(value2*4/100) 'create a value 4% below previous sample if (value>= lower_window) AND (value <= upper_window) then incoming 'sample is within 'allowable error window'? gosub average 'goto averaging routine endif value2 = value1 etc
Edit: Just seen your post/link scalerobotics - off to have reader's digestion! (& yes, I did a search but I guess my keywords weren't erhm key)
Last edited by HankMcSpank; - 29th August 2010 at 20:26.
ok, so scalerobotic's link to Melanie's 'sorting numbers' article, is exactly what I need, so I'm trying to get the thing to work.
i've basically got a small array that a prepopulate with some random numbers.
here's my screen output (I average the middle two numbers Rawdata[2] & RawData[3] )....Code:@ __CONFIG _FCMEN_OFF & _HS_OSC & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOR_OFF & _PWRTE_OFF DEFINE OSC 20 ' set Oscillator at 20Mhz. DEFINE NO_CLRWDT 1 ' PBP doesn't clear WDT automatically DEFINE HSER_CLROERR 1 DEFINE HSER_TXSTA 24h ' Enable transmit DEFINE HSER_SPBRG 129 ' set USART to 9600 baud @20Mhz CounterA var Byte DataA var Byte RawData var Byte [6] Averaged var byte hserout [27,91,72] 'home cursor hserout [27,91,50,74] 'clear screen RawData[0] = 25 RawData[1] = 27 RawData[2] = 32 RawData[3] = 30 RawData[4] = 34 RawData[5] = 33 hserout ["before....", 13, 10] hserout [dec rawdata[0],13,10] hserout [dec rawdata[1],13,10] hserout [dec rawdata[2],13,10] hserout [dec rawdata[3],13,10] hserout [dec rawdata[4],13,10] hserout [dec rawdata[5],13,10] hserout [13,10] hserout [13,10] CounterA =0 gosub SortArray gosub Average_data hserout ["after....", 13, 10] hserout [dec rawdata[0],13,10] hserout [dec rawdata[1],13,10] hserout [dec rawdata[2],13,10] hserout [dec rawdata[3],13,10] hserout [dec rawdata[4],13,10] hserout [dec rawdata[5],13,10] hserout ["averaged = ", dec averaged,13,10] goto end1 Average_data: hserout [dec rawData[2]," + ", dec rawData[3], 13,10] Averaged = (rawData[2]+rawData[3])/2 ' average the middle two of 6 ongoing samples return SortArray: SortLoop: If RawData(CounterA+1) < RawData(CounterA) then DataA=RawData(CounterA) RawData(CounterA)=RawData(CounterA+1) RawData(CounterA+1+0)=DataA If CounterA > 0 then CounterA=CounterA-2 endif CounterA=CounterA+1 If CounterA < 5 then goto SortLoop Return end1: end
It all works (ie sorts & averages correctly) - but where on earth has the number 6 in the sorted numbers come from? (there was no number six in the original array - also, whatever number I place in that array position RawData[4] comes out sorted as 6!?!!)Code:before.... 25 27 32 30 34 33 after.... 6 25 27 30 32 33 averaged = 31
Edit: Ok, think I've stumbled on the issue - it seems to be related to my array declaration ....because when I use RawData var Byte [8] ...everything works.
So why can't I declare an array 5 bytes 'deep'? ie RawData var byte [5] ???
Last edited by HankMcSpank; - 23rd September 2010 at 22:50.
Check out the sorting routine because it doesn't work. The first number should be 25, while the last number should be 34 not 33.It all works (ie sorts & averages correctly) - but where on earth has the number 6 in the sorted numbers come from?
Also your average is not correct, since 27 + 30 / 2 = 28.5 (28 as integer) not 31
Very likely the error comes from the wrong sorting.
Cheers
Al.
All progress began with an idea
I suppose that you corrected the array definition from[6] to [5].
And then the check in the program:
to this:Code:If CounterA < 5 then goto SortLoop
IoannisCode:If CounterA < 4 then goto SortLoop
Guy - what can I say.....I initially used random 1-6 for my numbers in the array & it sorted them & averaged them fine! (ie 1,2,3,4,5,6 was the screen output!)
I'm puzzled why my screen output posted above doesn't look like it's working (for either the sorting or the averaging!! lol) - it was late ...that's my lame excuse for not picking up on that! The 'sorting' part of the code was a direct lift from Melanie's article.
I'm at work now (don't tell my boss!) , so will revisit this tonight.
Ok, the thread title really ought to be called stripping out erroneous samples & then averaging the leftovers (vs averaging data which includes erroneous samples), I've revisited my earlier problem
And it works (basically, the code throws away all but the middle two samples of six & averages those two - ideal if your input data is all over the place.,...eg comparator counts etc!)...Code:@ __CONFIG _FCMEN_OFF & _HS_OSC & _WDT_OFF & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOR_OFF & _PWRTE_OFF DEFINE OSC 20 ' set Oscillator at 20Mhz. DEFINE NO_CLRWDT 1 ' PBP doesn't clear WDT automatically DEFINE HSER_CLROERR 1 DEFINE HSER_TXSTA 24h ' Enable transmit DEFINE HSER_SPBRG 129 ' set USART to 9600 baud @20Mhz ANSEL = 0 ANSELH = 0 CounterA var Byte 'A counter for sorting out the erroneous data DataA var word 'A Variable used in sorting the numbers in the array RawData var word [6] 'This is the RawData gojng into the array Averaged var word 'This is used to get the average. top: RawData[0] = 300 RawData[1] = 100 RawData[2] = 200 RawData[3] = 600 RawData[4] = 400 RawData[5] = 500 pause 250 hserout [27,91,72] 'home cursor hserout [27,91,50,74] 'clear screen next1: hserout [13,10] hserout [13,10] pause 300 hserout ["before....", 13, 10] hserout [13,10] hserout [dec rawdata[0],13,10] hserout [dec rawdata[1],13,10] hserout [dec rawdata[2],13,10] hserout [dec rawdata[3],13,10] hserout [dec rawdata[4],13,10] hserout [dec rawdata[5],13,10] hserout [13,10] hserout [13,10] pause 2000 CounterA =0 gosub SortArray gosub Average_data hserout ["after....", 13, 10] hserout [13,10] hserout [dec rawdata[0],13,10] hserout [dec rawdata[1],13,10] hserout [dec rawdata[2],13,10] hserout [dec rawdata[3],13,10] hserout [dec rawdata[4],13,10] hserout [dec rawdata[5],13,10] hserout [13,10] hserout ["therefore, middle two samples are ", dec rawData[2]," + ", dec rawData[3], 13,10] pause 500 hserout [13,10] hserout ["averaged middle two samples = ", dec averaged,13,10] pause 500 pause 3000 goto top Average_data: Averaged = (rawData[2]+rawData[3])/2 ' average the middle two of 6 samples return SortArray: SortLoop: If RawData(CounterA+1) < RawData(CounterA) then DataA=RawData(CounterA) RawData(CounterA)=RawData(CounterA+1) RawData(CounterA+1+0)=DataA If CounterA > 0 then CounterA=CounterA-2 endif CounterA=CounterA+1 If CounterA < 5 then goto SortLoop Return end
Or a more real world example (eg a comparator interrupt count jittering in or around the 200 count level but with one spurious/erroneous count of 400 in the 'sample of six' to get rid of)...Code:before.... 300 100 200 600 400 500 after.... 100 200 300 400 500 600 therefore, middle two samples are 300 + 400 averaged middle two samples = 350
Not entirely sure what the problem was (I changed a few things at once & didn't retrace my steps!)Code:before.... 199 400 200 200 199 200 after.... 199 199 200 200 200 400 therefore, middle two samples are 200 + 200 averaged middle two samples = 200
Last edited by HankMcSpank; - 25th September 2010 at 11:03.
Bookmarks