From 1baaee0fd50a3e071fbb35f2417aec959dd3049f Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 25 Apr 2015 22:19:44 +0200 Subject: [PATCH] Statistics: MovingAverage --- src/Numerics/Statistics/Statistics.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Numerics/Statistics/Statistics.cs b/src/Numerics/Statistics/Statistics.cs index 3fe30641..9a92fafe 100644 --- a/src/Numerics/Statistics/Statistics.cs +++ b/src/Numerics/Statistics/Statistics.cs @@ -931,5 +931,21 @@ namespace MathNet.Numerics.Statistics { return StreamingStatistics.Entropy(data.Where(d => d.HasValue).Select(d => d.Value)); } + + /// + /// Evaluates the sample mean over a moving window, for each samples. + /// Returns NaN if no data is empty or if any entry is NaN. + /// + /// The sample stream to calculate the mean of. + /// The number of last samples to consider. + public static IEnumerable MovingAverage(this IEnumerable samples, int windowSize) + { + var movingStatistics = new MovingStatistics(windowSize); + return samples.Select(sample => + { + movingStatistics.Push(sample); + return movingStatistics.Mean; + }); + } } }