Last active
September 15, 2015 15:24
-
-
Save devth/ddea1a4cc768a186ff43 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compute center-weighted moving average of equally spaced values, some of which may be None | |
// period should be odd | |
def centerWeightedAverage (values: Iterable[Option[Double]], period: Int): Seq[Option[Double]] = { | |
// prepend and append period/2 Nones | |
val valuesWithBuffers = List.fill(period / 2)(None) ++ values ++ List.fill(period / 2)(None) | |
valuesWithBuffers | |
.sliding(period) | |
.map(_.flatten) | |
.map(x => if (x.size > 0) Some(x.sum.toDouble / x.size) else None) | |
.take(values.size) | |
.toList | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment