Shop OBEX P1 Docs P2 Docs Learn Events
Variations of average data between Spin and Microsoft Excel — Parallax Forums

Variations of average data between Spin and Microsoft Excel

RussMRussM Posts: 27
edited 2013-05-12 18:43 in Propeller 1
I have a program that collects data from two rotary encoders and outputs the data to excel. My program then sorts the data using bubble sort then takes the average of the 30 highest values. The problem I am facing is that the average calculated in my program is always considerably lower than the average given to me in Excel. For example, if I have 30 data points ranging in values from approximately 2.559 to 2.6069 Excel calculates the average as 2.6019. When I calculate the average of those same points in my program it calculates the average as 2.5153. I have attached my average algorithm, bubble sort algorithm and the program as a whole. Does anyone know why I am getting this data discrepancy? Thanks for any help!

Bubble Sort
c := 0
d := 0
swap := 0.0


'==Sort values using bubble sory
repeat while (c < (i - 1))
    d := 0
    repeat while (d < (i-c-1) )
        if (1 == f32_orig.FCmp(float_highs[d],float_highs[d+1]))
          swap := float_highs[d]
          float_highs[d] := float_highs[d+1]
          float_highs[d+1] := swap
     d := d + 1
 c := c + 1

Average, note that "i" is equal to the number of elements in my data array.
xbar := 0.0
h := 0
an_average := 0.0
repeat while (h < 30)
  an_average := f32_orig.FAdd(an_average, float_highs[i-h])
  h := h + 1
PDAQ.DataText(string("Highs Average"))
PDAQ.CR  
xbar :=  f32_orig.FDiv(an_average, 30.0)
PDAQ.DataText(fs.FloatToString(xbar))
PDAQ.CR

Comments

  • kuronekokuroneko Posts: 3,623
    edited 2013-05-12 17:03
    Looks like you fill your array from 0..i-1 but sum up the average from 1..i. Make that (i-29)..i.
  • RussMRussM Posts: 27
    edited 2013-05-12 17:19
    kuroneko wrote: »
    Looks like you fill your array from 0..i-1 but sum up the average from 1..i.

    Where do you see that? I sum up i-0,i-1,i-2,...i-29 which gives me the sum of the 30 highest values.
  • kuronekokuroneko Posts: 3,623
    edited 2013-05-12 17:27
    RussM wrote: »
    Where do you see that? I sum up i-0,i-1,i-2,...i-29 which gives me the sum of the 30 highest values.
    Yes, and with i being N you'll get indices N..M (M < N). Assuming the values are still stored with a 0-based index that's off by one (the highest index being N-1).

    In that respect my first post was slightly incorrect. However you do include index i which is invalid.
  • RussMRussM Posts: 27
    edited 2013-05-12 18:43
    kuroneko wrote: »
    Yes, and with i being N you'll get indices N..M (M < N). Assuming the values are still stored with a 0-based index that's off by one (the highest index being N-1).

    In that respect my first post was slightly incorrect. However you do include index i which is invalid.

    You're absolutely right. That fixed the problem thank you!
Sign In or Register to comment.