Skip to content

Instantly share code, notes, and snippets.

@higumachan
Last active August 29, 2015 14:17
Show Gist options
  • Save higumachan/1a7dda35417f27681998 to your computer and use it in GitHub Desktop.
Save higumachan/1a7dda35417f27681998 to your computer and use it in GitHub Desktop.
IObservable<double> MovingAvg(IObservable<double> targetStream, int windowSize)
{
return targetStream.Zip(
targetStream.Skip(windowSize)
.StartWith(Enumerable.Repeat(0.0, windowSize)),
(l, r) => Tuple.Create(r, l))
.Scan(0.0, (ago, next) => { return ago - next.Item1 + next.Item2; })
.Skip(windowSize)
.Select(x => x / windowSize)
;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment