[Solved] How to calculate a decaying average? [closed]


Here’s a simplistic implementation of what you asking.

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>

int normalize_weights(double *weights, size_t num_weights)
{
  double sum = 0;

  for (size_t i = 0; i < num_weights; ++i)
  {
    if (weights[i] < 0)
      return -1;

    sum += weights[i];
  }

  if (sum == 0)
    return -1;

  for (size_t i = 0; i < num_weights; ++i)
    weights[i] /= sum;

  return 0;
}

int weighted_avg(double *avg_ptr, const double *samples, size_t num_samples, const double *weights, size_t num_weights)
{
  if (num_samples != num_weights)
    return -1;

  double avg = 0;

  for (size_t i = 0; i < num_weights; ++i)
    avg += samples[i] * weights[i];

  *avg_ptr = avg;

  return 0;
}

int main(int argc, char **argv)
{
  double weights[] = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; 
  double samples[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  double avg;

  if (normalize_weights(weights, sizeof(weights) / sizeof(weights[0])))
    abort();

  for (size_t i = 0; i < sizeof(weights) / sizeof(weights[0]); ++i)
    printf("Normalized weight[%u] = %lf\n", (unsigned) i, weights[i]);

  if (weighted_avg(&avg, samples, sizeof(samples) / sizeof(samples[0]), weights, sizeof(weights) / sizeof(weights[0])))
    abort();

  printf("\nWeighted average is %lf\n", avg);

  return 0;
}

solved How to calculate a decaying average? [closed]