Advertisement
Schnuk

Untitled

Feb 20th, 2021
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.53 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. namespace yield
  4. {
  5. public static class MovingAverageTask
  6. {
  7. public static IEnumerable<DataPoint> MovingAverage(this IEnumerable<DataPoint> data, int windowWidth)
  8. {
  9. double sum = 0;
  10. var queue = new Queue<double>();
  11. foreach (var e in data)
  12. {
  13. if (queue.Count == windowWidth)
  14. sum -= queue.Dequeue();
  15. queue.Enqueue(e.OriginalY);
  16. sum += e.OriginalY;
  17. yield return new DataPoint(e.X, e.OriginalY).WithAvgSmoothedY(sum / queue.Count);
  18. }
  19. }
  20. }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement