Thread: Help with Averaging values previous 5 seconds received from function generator.

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    5

    Help with Averaging values previous 5 seconds received from function generator.

    How do I average the values received from the function generator
    I'm using microcontroller 68HC11 and I would like to average the values received from it.

    Do I store it into array 1st and then loop it? How?

    For example
    function gen might output frequency,
    1.23hz
    2.13hz
    1.54hz
    3.53hz
    1.85hz
    1.81hz
    and so on so forth..


    I need to average it over the previous 5 seconds.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    So what's the problem?

    You could store it in an array or keep a running total.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    5
    The problem is I have no idea where to start. the frequency will generate every 33microsec, How do I add them all up, and then divide it by god knows how many times it count, to get the avg.

    And 1 more thing, I still need it to average over the recent 5 seconds, how do I do so it will forget the last second, and then add the new second.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    int data[5];
    int dataptr = 0;
    
    // data loop
    void AddData( int *data, int *dataptr, int newdata)
      { data[*dataptr] = newdata;
         dataptr = (dataptr + 1) % 5; }
    The array is now circular... that is it will start at 0 progress to 4 then loop back to 0 again... you will always have the last 5 entries in the array. If you are only looking for an average, just add 'em up and divide by 5.

    Trust me... 2.3hz isn't happening on microsecond intervals... 2.5hz is 400 milliseconds. With 1 digit frequencies you can capture data every 100 milliseconds and not miss anything.
    Last edited by CommonTater; 12-07-2011 at 07:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Convert seconds to hours, minutes and seconds
    By kkk in forum C Programming
    Replies: 2
    Last Post: 07-26-2011, 10:47 AM
  2. Replies: 6
    Last Post: 06-10-2011, 11:02 AM
  3. Average function is not averaging...
    By csharp100 in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2011, 05:59 PM
  4. Replies: 12
    Last Post: 10-05-2009, 12:19 AM
  5. Running a function for a period of seconds
    By Akkernight in forum C++ Programming
    Replies: 5
    Last Post: 02-24-2009, 12:41 PM