4

我正在寻找一种在 C# 中实现移动平均过滤器的优雅方法。现在,这很容易,但是在边界处,平均窗口将环绕开始/结束。这让我的代码变得丑陋且不直观,我想知道是否有更聪明的方法可以使用 LINQ 来解决这个问题。

所以我目前拥有的是:

// input is a List<double> y, output is List<double> yfiltered
int yLength = y.Count;
for (int i = 0; i < yLength; i++)
{
    double sum = 0.0;
    for (int k = i - halfWindowWidth; k <= i + halfWindowWidth; k++)
    {
        if (k < 0)
        {
            // k is negative, wrap around
            sum += y[yLength - 1 + k];
        }
        else if (k >= yLength)
        {
            // k exceeds y length, wrap around
            sum += y[k - yLength];
        }
        else
        {
            // k within y.Count
            sum += y[k];
        }
    }
    yfiltered[i] = sum / (2 * halfWindowWidth + 1);
}
4

3 回答 3

5

这是一个完全不同的建议 -

我试图让它变得更好,而不是更具可读性。

您当前代码的问题在于,它在不需要时一次又一次地总结了许多数字。

在实现代码之后比较两种方法......

我只是第一次将一堆相加,然后一次又一次地减去尾部并添加头部:

double sum = 0;

// sum = Enumerable.Range(i - halfWindowWidth, halfWindowWidth * 2 + 1)
//    .Select(k => y[(k + yLength) % yLength]).Sum();

for (var i = -halfWindowWidth; i <= halfWindowWidth; i++)
{
    sum += y[(i + yLength) % yLength];
}

yFiltered[0] = sum / (2 * halfWindowWidth + 1);

for (var i = 1; i < yLength; i++)
{
    sum = sum -
          y[(i - halfWindowWidth - 1 + yLength) % yLength] +
          y[(i + halfWindowWidth) % yLength];

    yFiltered[i] = sum / (2 * halfWindowWidth + 1);
}

以下是速度测试,比较了完全重新计算的方法和这个:

private static double[] Foo1(IList<double> y, int halfWindowWidth)
{
    var yfiltered = new double[y.Count];

    var yLength = y.Count;

    for (var i = 0; i < yLength; i++)
    {
        var sum = 0.0;

        for (var k = i - halfWindowWidth; k <= i + halfWindowWidth; k++)
        {
            sum += y[(k + yLength) % yLength];
        }

        yfiltered[i] = sum / (2 * halfWindowWidth + 1);
    }

    return yfiltered;
}

private static double[] Foo2(IList<double> y, int halfWindowWidth)
{
    var yFiltered = new double[y.Count];
    var windowSize = 2 * halfWindowWidth + 1;

    double sum = 0;

    for (var i = -halfWindowWidth; i <= halfWindowWidth; i++)
    {
        sum += y[(i + y.Count) % y.Count];
    }

    yFiltered[0] = sum / windowSize;

    for (var i = 1; i < y.Count; i++)
    {
        sum = sum -
              y[(i - halfWindowWidth - 1 + y.Count) % y.Count] +
              y[(i + halfWindowWidth) % y.Count];

        yFiltered[i] = sum / windowSize;
    }

    return yFiltered;
}

private static TimeSpan TestFunc(Func<IList<double>, int, double[]> func, IList<double> y, int halfWindowWidth, int iteration
{
    var sw = Stopwatch.StartNew();

    for (var i = 0; i < iterations; i++)
    {
        var yFiltered = func(y, halfWindowWidth);
    }

    sw.Stop();
    return sw.Elapsed;
}

private static void RunTests()
{
    var y = new List<double>();
    var rand = new Random();

    for (var i = 0; i < 1000; i++)
    {
        y.Add(rand.Next());
    }

    var foo1Res = Foo1(y, 100);
    var foo2Res = Foo2(y, 100);

    Debug.WriteLine("Results are equal: " + foo1Res.SequenceEqual(foo2Res));

    Debug.WriteLine("Foo1: " + TestFunc(Foo1, y, 100, 1000));
    Debug.WriteLine("Foo2: " + TestFunc(Foo2, y, 100, 1000));
}

时间复杂度:

我的方式:O(n + m)

其他方式:O(n * m)

由于 Foo1 是 O(n * m) 而 Foo2 是 O(n + m) ,因此差异巨大也就不足为奇了。

在这个不是很疯狂的大规模上的结果是:

结果相等:真

Foo1:5.52 秒

Foo2:61.1 毫秒

并且在更大的范围内(在迭代和计数中将 1000 替换为 10000):

Foo1:10分钟后停止……

Foo2:6.9 秒

于 2012-05-08T09:18:27.883 回答
4

扩展我的评论,您可以使用mod ( %)运算符k来换行
from0ylength - 1

    // input is a List<double> y, output is List<double> yfiltered
    int yLength = y.Count;
    for (int i = 0; i < yLength; i++)
    {
        double sum = 0.0;
        for (int k = i - halfWindowWidth; k <= i + halfWindowWidth; k++)
        {
            sum += y[(k + yLength) % yLength];
        }
        yfiltered[i] = sum / (2 * halfWindowWidth + 1);
    }
于 2012-05-08T09:02:24.040 回答
3
for (var i = 0; i < yLength; i++)
{
    var sum = Enumerable.Range(i - halfWindowWidth, halfWindowWidth * 2 + 1)
        .Select(k => y[(yLength + k) % yLength]).Sum();

    yFiltered[i] = sum / (2 * halfWindowWidth + 1);
}

甚至:

var output = input.Select((val, i) =>
                 Enumerable.Range(i - halfWindowWidth, halfWindowWidth * 2 + 1)
                           .Select(k => input[(input.Count + k) % input.Count])
                 .Sum()).ToList();
于 2012-05-08T09:04:27.547 回答