1D快速傅里叶变换

时间:2017-04-14 14:57:55

标签: c# fft

以下是什么算法?

我从以下源代码中了解到:

  1. dir是FFT的方向:forward = 1,inverse = -1。
  2. x是真实的部分
  3. y是想象中的部分
  4. 这里m是什么?

    如果x = {1,1,1,1,0,0,0,0},y = {0,0,0,0,0,0,0 ,0,0},m的价值是什么?

            //Inplace 1D FFT
            public static void FFT1D(int dir, int m, ref double[] x, ref double[] y)
            {
                long nn, i, i1, j, k, i2, l, l1, l2;
                double c1, c2, tx, ty, t1, t2, u1, u2, z;
                /* Calculate the number of points */
                nn = 1;
                for (i = 0; i < m; i++)
                    nn *= 2;
                /* Do the bit reversal */
                i2 = nn >> 1;
                j = 0;
                for (i = 0; i < nn - 1; i++)
                {
                    if (i < j)
                    {
                        tx = x[i];
                        ty = y[i];
                        x[i] = x[j];
                        y[i] = y[j];
                        x[j] = tx;
                        y[j] = ty;
                    }
                    k = i2;
                    while (k <= j)
                    {
                        j -= k;
                        k >>= 1;
                    }
                    j += k;
                }
                /* Compute the FFT */
                c1 = -1.0;
                c2 = 0.0;
                l2 = 1;
                for (l = 0; l < m; l++)
                {
                    l1 = l2;
                    l2 <<= 1;
                    u1 = 1.0;
                    u2 = 0.0;
                    for (j = 0; j < l1; j++)
                    {
                        for (i = j; i < nn; i += l2)
                        {
                            i1 = i + l1;
                            t1 = u1 * x[i1] - u2 * y[i1];
                            t2 = u1 * y[i1] + u2 * x[i1];
                            x[i1] = x[i] - t1;
                            y[i1] = y[i] - t2;
                            x[i] += t1;
                            y[i] += t2;
                        }
                        z = u1 * c1 - u2 * c2;
                        u2 = u1 * c2 + u2 * c1;
                        u1 = z;
                    }
                    c2 = Math.Sqrt((1.0 - c1) / 2.0);
                    if (dir == 1)
                        c2 = -c2;
                    c1 = Math.Sqrt((1.0 + c1) / 2.0);
                }
                /* Scaling for forward transform */
                if (dir == 1)
                {
                    for (i = 0; i < nn; i++)
                    {
                        x[i] /= (double)nn;
                        y[i] /= (double)nn;
                    }
                }
            }
    

1 个答案:

答案 0 :(得分:4)

您发布的FFT的实施仅限于大小为2 m 的输入。因此m间接指定FFT块大小的大小。因此,对于x = {1,1,1,1,0,0,0,0}y={1,1,1,1,0,0,0,0}为大小为8 = 2 3 的数组的示例,m将等于3.

请注意,没有对数组xy的大小进行额外检查,因此请确保它们至少为该大小。