Advertisement
Guest User

Untitled

a guest
Feb 26th, 2017
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using System.Collections.Generic;
  2.  
  3. namespace yield
  4. {
  5.     public class Averager
  6.     {
  7.         DataPoint point;
  8.         Queue<double> queue;
  9.         double sum;
  10.         int windowWidth;
  11.         public Averager(DataPoint point, int windowWidth)
  12.         {
  13.             this.windowWidth = windowWidth;
  14.             this.point = point;
  15.             queue = new Queue<double>();
  16.         }
  17.  
  18.         public double Measure()
  19.         {
  20.             var value = point.OriginalY;
  21.             queue.Enqueue(value);
  22.             sum += value;
  23.  
  24.             if (queue.Count > windowWidth)
  25.                 sum -= queue.Dequeue();
  26.  
  27.             return sum / queue.Count;
  28.         }
  29.     }
  30.     public static class MovingAverageTask
  31.     {
  32.         public static IEnumerable<DataPoint> MovingAverage(this IEnumerable<DataPoint> data, int windowWidth)
  33.         {
  34.             var counter = data.GetEnumerator();
  35.             var t = 0;
  36.  
  37.             while (counter.MoveNext())
  38.             {
  39.                 var point = counter.Current;
  40.                 if (t > 0) // здесь всё плохо. Как мне получить значение новое у точки?
  41.                 {
  42.                     var averager = new Averager(point, windowWidth);
  43.                     point.OriginalY = point.AvgSmoothedY;
  44.                 }
  45.                 else // а тут вроде норм
  46.                 {
  47.                     point.AvgSmoothedY = point.OriginalY;
  48.                     t++;
  49.                 }
  50.                 yield return point;
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement