Pregunta

He hecho un FFT para obtener la frecuencia fundamental en tiempo real y para implementar filtros de paso alto y bajo.

Ahora quiero ser capaz de grabar en un archivo .wav después de aplicar un filtro.

En primer lugar voy a tener que invertir la FFT y esa es mi pregunta. ¿Cuáles son los pasos para hacer esto?

Yo uso la FFT se define en esta proyecto .

Aquí está el código para ello:

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;
        }
    }
}
¿Fue útil?

Solución

Hay muchos muy buenos implementaciones FFT alrededor como FFTW que recomiendo encarecidamente utilizar uno. Vienen con ifft también. La suya, tal como se aplica, será muy lento.

Otros consejos

En función de la FFT exacta y definiciones IFFT, la diferencia es sólo una constante. La forma más fácil de determinar que constante en su caso es probable que sólo ensayo y error.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top