This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Need to know what code to use to take an average

Hello, I am trying to find out how to write a program which will take the average over the day. I dont want it to just take the average over the last 24 hours, I know how to do that, rather I want to find the average from 12 midnight to 12 midnight the next day, and to have this average displayed throughout the day and changing as the day progresses. This is to be written in C code

Parents
  • I'm sorry, in my example the variable

    signed int Avg_1;                         // previous value of running average
    
    is redundant, so the essential functions should be as follows:
    void InitAvgCalc(signed char init_value, unsigned int sample_count)
    {
      reset_time = 0;                           // reset time init
      Rem = 0;                                  // remainder init
      Avg = init_value;                         // "AVG(i-1)" init
      current_minus_N = &Ring_Buffer[0];        // pointer to (current-N) sample
      memset(&Ring_Buffer[0],init_value,sample_count);  // fills ring buffer
    }                                                   // with initial value
    
    void CalculateAverageValue(void)            // running average calculation
    {
      signed int sample = GetCurrentSample();   // x(i)
      signed int diff;
    
      diff = sample - *current_minus_N + Rem;   // [x(i)-x(i-N)] corr. (remainder)
      *current_minus_N = sample;                // saves x(i-N)
      Avg = Avg + diff / SAMPLE_COUNT;          // calculation itself
      Rem = diff % SAMPLE_COUNT;
    
      if (++current_minus_N == &Ring_Buffer[SAMPLE_COUNT]) {
        current_minus_N = &Ring_Buffer[0];      // pointer correction
      }                                         // @ the end of ring buffer
    }
    
    Eric

Reply
  • I'm sorry, in my example the variable

    signed int Avg_1;                         // previous value of running average
    
    is redundant, so the essential functions should be as follows:
    void InitAvgCalc(signed char init_value, unsigned int sample_count)
    {
      reset_time = 0;                           // reset time init
      Rem = 0;                                  // remainder init
      Avg = init_value;                         // "AVG(i-1)" init
      current_minus_N = &Ring_Buffer[0];        // pointer to (current-N) sample
      memset(&Ring_Buffer[0],init_value,sample_count);  // fills ring buffer
    }                                                   // with initial value
    
    void CalculateAverageValue(void)            // running average calculation
    {
      signed int sample = GetCurrentSample();   // x(i)
      signed int diff;
    
      diff = sample - *current_minus_N + Rem;   // [x(i)-x(i-N)] corr. (remainder)
      *current_minus_N = sample;                // saves x(i-N)
      Avg = Avg + diff / SAMPLE_COUNT;          // calculation itself
      Rem = diff % SAMPLE_COUNT;
    
      if (++current_minus_N == &Ring_Buffer[SAMPLE_COUNT]) {
        current_minus_N = &Ring_Buffer[0];      // pointer correction
      }                                         // @ the end of ring buffer
    }
    
    Eric

Children
No data