LINQ中的圆形移动平均滤波器

时间:2012-05-08 08:49:44

标签: c# .net linq filtering signal-processing

我正在寻找一种在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);
}

3 个答案:

答案 0 :(得分:4)

在我的comment上展开,您可以使用mod (%)运算符让k换行 从0ylength - 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);
    }

答案 1 :(得分:4)

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

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

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

  

比较实施代码后的两种方法......

我只是第一次总结一堆,然后一次又一次减去尾巴并添加头部:

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));
}
  

时间复杂性:

     

MyWay:O(n + m)

     

其他方式:O(n * m)

由于Foo1为O(n * m)且Foo2为O(n + m),因此差异很大并不奇怪。

这个的结果并不是真正疯狂的大规模

  

结果相同:正确

     

Foo1:5.52秒

     

Foo2:61.1毫秒

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

  

Foo1:10分钟后停止......

     

Foo2:6.9秒

答案 2 :(得分: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();