我需要帮助搞清楚这种FFT方法

时间:2018-02-13 21:49:45

标签: c# math audio signal-processing fft

我试图用fft从声波中获取频率。我找到了一种FFT方法,但是我无法理解它希望我输入什么,如何从中获取信息,以及如何使用我从中获得的信息。我已经尝试将缓冲区索引处的值输入为y,缓冲区位置为x,但是我得到了奇怪的结果。 int m似乎与样本有关,但我不知道如何选择我想要输入的内容。但它会影响存储到x和y中的多少。幸运的是,dir很容易理解,它基本上就像你是否向前推进FFT一样。

public static void FFT(short dir, int m, double[] x, double[] y)
    {
        int n, i, i1, j, k, i2, l, l1, l2;
        double c1, c2, tx, ty, t1, t2, u1, u2, z;

        // Calculate the number of points 

        n = 1;

        for (i = 0; i < m; i++)
            n *= 2;

        // Do the bit reversal 

        i2 = n >> 1;
        j = 0;
        for (i = 0; i < n - 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 < n; 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 < n; i++)
            {
                x[i] /= n;
                y[i] /= n;
            }
        }

1 个答案:

答案 0 :(得分:1)

我的猜测是:

m: Log2 of the data length
x: real part of input data
y: imaginary part of input data (array of 0s if the signal consists of only real numbers)

我没有测试过,请告诉我它是否有效。