/// <summary> /// 算术平均值过滤. 窗口长度内取平均值 /// </summary> /// <param name="values">原集合</param> /// <param name="windowLength">窗口长度</param> /// <returns>新的样本集</returns> public static double[] FilterWithWindowMean(this IIndexable <double> values, int windowLength) { windowLength.ShouldBeNotLessThanZero(); windowLength.ShouldNotLargerThan(values.Count()); var result = new double[values.Count]; int half = windowLength / 2; for (int i = half; i < values.Count - half; i++) { result[i] = values.TakePythonStyle(i - half, i + half + 1) .ToArray() .Sum((e) => e) / windowLength; } return(result); }
/// <summary> /// 中位值过滤. 窗口长度内取中位值 /// </summary> /// <param name="values">原集合</param> /// <param name="windowLength">窗口长度</param> /// <returns>新的样本集</returns> public static double[] FilterWithWindowMedian(this IIndexable <double> values, int windowLength) { windowLength.ShouldLargerThan(0); windowLength.ShouldBeOdd(); windowLength.ShouldNotLargerThan(values.Count()); var result = new double[values.Count]; int half = windowLength / 2; for (int i = half; i < values.Count - half; i++) { double[] window = values.TakePythonStyle(i - half, i + half + 1).OrderBy((e) => e).ToArray(); result[i] = window[half + 1]; } return(result); }