1
votes

I am trying to get the frequencies from a sound wave using fft. I have found an FFT method, but I am having trouble understanding what it wants me to put in, how to get information out of it, and how to use what I get out of it. I have tried entering the value at index of the buffer as y, with the buffer position as x, but I get weird results. int m seems to be something to do with samples, but I have no clue how to choose what I want to input there. But it effects how much is stored into the x and y double. Luckily dir, is rather easy to understand, it's basically acting like a bool for whether you FFT forward or not.

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
See this answer for an outline of what you need to do with the FFT inputs and outputs.Paul R
see How to compute Discrete Fourier Transform? and all the sub-links. Also beware that you will obtain Niquist frequencies not the actual frequencies of your signal so you need to take in mind aliasing will be present. Also using non power of 2 datasets creates big problems with the frequency acquisition if used FFT instead of DFT ...Spektre

1 Answers

1
votes

My guess is:

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)

I have not tested so please let me know if it works.