views:

371

answers:

2

Hello,

I've done a fft to get fundamental frequency in real time and to implement high and low pass filters.

Now I want to be able to record to a .wav file after I apply a filter.

First I'll have to invert the fft and that is my question. What are the steps to do this?

I use the FFT defined in this project.

Here is the code for it:

using System;
using System.Collections.Generic;
using System.Text;

namespace SoundLog
{
    public class FourierTransform
    {
        static private int n, nu;

        static private int BitReverse(int j)
        {
            int j2;
            int j1 = j;
            int k = 0;
            for (int i = 1; i <= nu; i++)
            {
                j2 = j1 / 2;
                k = 2 * k + j1 - 2 * j2;
                j1 = j2;
            }
            return k;
        }

        static public double[] FFT(ref double[] x)
        {
            // Assume n is a power of 2
            n = x.Length;
            nu = (int)(Math.Log(n) / Math.Log(2));
            int n2 = n / 2;
            int nu1 = nu - 1;
            double[] xre = new double[n];
            double[] xim = new double[n];
            double[] magnitude = new double[n2];
            double[] decibel = new double[n2];
            double tr, ti, p, arg, c, s;
            for (int i = 0; i < n; i++)
            {
                xre[i] = x[i];
                xim[i] = 0.0f;
            }
            int k = 0;
            for (int l = 1; l <= nu; l++)
            {
                while (k < n)
                {
                    for (int i = 1; i <= n2; i++)
                    {
                        p = BitReverse(k >> nu1);
                        arg = 2 * (double)Math.PI * p / n;
                        c = (double)Math.Cos(arg);
                        s = (double)Math.Sin(arg);
                        tr = xre[k + n2] * c + xim[k + n2] * s;
                        ti = xim[k + n2] * c - xre[k + n2] * s;
                        xre[k + n2] = xre[k] - tr;
                        xim[k + n2] = xim[k] - ti;
                        xre[k] += tr;
                        xim[k] += ti;
                        k++;
                    }
                    k += n2;
                }
                k = 0;
                nu1--;
                n2 = n2 / 2;
            }
            k = 0;
            int r;
            while (k < n)
            {
                r = BitReverse(k);
                if (r > k)
                {
                    tr = xre[k];
                    ti = xim[k];
                    xre[k] = xre[r];
                    xim[k] = xim[r];
                    xre[r] = tr;
                    xim[r] = ti;
                }
                k++;
            }
            for (int i = 0; i < n / 2; i++)
                //magnitude[i] = (float)(Math.Sqrt((xre[i] * xre[i]) + (xim[i] * xim[i])));
                decibel[i] = 10.0 * Math.Log10((float)(Math.Sqrt((xre[i] * xre[i]) + (xim[i] * xim[i]))));
            //return magnitude;
            return decibel;
        }
    }
}
A: 

Depending on your exact FFT and IFFT definitions, the difference is only a constant. The easiest way to determine that constant in your case is probably just trial & error.

MSalters
I use the FFT defined in this projecto:http://www.codeproject.com/KB/audio-video/SoundCatcher.aspxCan you help me?
aF
Trial and error?Look at my comment above.
Dr. Tim
AAT
+2  A: 

There are so many really good fft implementations around such as FFTW that I highly recommend using one. They come with ifft as well. Yours, as implemented, will be excruciatingly slow.

Dr. Tim
The velocity is not a problem at the moment.I need to do it in c# and FFTW only has c++.Can you tell me how can I convert the one that I posted plz?
aF
There are two differences between the forward and inverse fts. The inverse needs to be scaled by 1/n and the exponential is negative. So, for the inverse ft (assuming I understand your implementation) change the sign of arg and multiply each point in the result by 1/n.
Dr. Tim
Also, you are using a complex fft for real data by setting all of the imaginaries to zero. This is mathematically sound but wasteful. Look at the Numerical Recipes web page (nr.com) for information on real valued ffts.
Dr. Tim
I'm passing from fft to ifft because I want to record to a .wav format.After passing to ifft (that has the same length then the fft) do I need to make it bigger? And how do I put that data into a .wav? How do I make the .wav?
aF