Advertisement
Guest User

Untitled

a guest
Mar 13th, 2012
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. /**
  2. * ewma_init() - Initialize EWMA parameters
  3. * @avg: Average structure
  4. * @factor: Factor to use for the scaled up internal value. The maximum value
  5. * of averages can be ULONG_MAX/(factor*weight).
  6. * @weight: Exponential weight, or decay rate. This defines how fast the
  7. * influence of older values decreases. Has to be bigger than 1.
  8. *
  9. * Initialize the EWMA parameters for a given struct ewma @avg.
  10. */
  11. void ewma_init(struct ewma *avg, unsigned long factor, unsigned long weight)
  12. {
  13. WARN_ON(weight <= 1 || factor == 0);
  14. avg->internal = 0;
  15. avg->weight = weight;
  16. avg->factor = factor;
  17. }
  18. EXPORT_SYMBOL(ewma_init);
  19.  
  20. /**
  21. * ewma_add() - Exponentially weighted moving average (EWMA)
  22. * @avg: Average structure
  23. * @val: Current value
  24. *
  25. * Add a sample to the average.
  26. */
  27. struct ewma *ewma_add(struct ewma *avg, unsigned long val)
  28. {
  29. avg->internal = avg->internal ?
  30. (((avg->internal * (avg->weight - 1)) +
  31. (val * avg->factor)) / avg->weight) :
  32. (val * avg->factor);
  33. return avg;
  34. }
  35. EXPORT_SYMBOL(ewma_add);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement