Furthermore, you can use "Butterfly" computation(http://www.cmlab.csie.ntu.edu.tw/cml/dsp/training/coding/transform/fft.html) to gain a much better performance.
private Complex[] FFT(Complex[] input,bool invert)
{
if (input.Length == 1)
{
return new Complex[] { input[0] };
}
int length = input.Length;
int half = length / 2;
Complex[] result = new Complex[length];
double fac = -2.0 * Math.PI / length;
if (invert)
{
fac = -fac;
}
Complex[] evens = new Complex[half];
for (int i = 0; i < half; i++)
{
evens[i] = input[2 * i];
}
Complex[] evenResult = FFT(evens,invert);
Complex[] odds = evens;
for (int i = 0; i < half; i++)
{
odds[i] = input[2 * i + 1];
}
Complex[] oddResult = FFT(odds,invert);
for (int k = 0; k < half; k++)
{
double fack = fac * k;
Complex oddPart = oddResult[k] * new Complex(Math.Cos(fack), Math.Sin(fack));
result[k] = evenResult[k] + oddPart;
result[k + half] = evenResult[k] - oddPart;
}
return result;
}
{
if (input.Length == 1)
{
return new Complex[] { input[0] };
}
int length = input.Length;
int half = length / 2;
Complex[] result = new Complex[length];
double fac = -2.0 * Math.PI / length;
if (invert)
{
fac = -fac;
}
Complex[] evens = new Complex[half];
for (int i = 0; i < half; i++)
{
evens[i] = input[2 * i];
}
Complex[] evenResult = FFT(evens,invert);
Complex[] odds = evens;
for (int i = 0; i < half; i++)
{
odds[i] = input[2 * i + 1];
}
Complex[] oddResult = FFT(odds,invert);
for (int k = 0; k < half; k++)
{
double fack = fac * k;
Complex oddPart = oddResult[k] * new Complex(Math.Cos(fack), Math.Sin(fack));
result[k] = evenResult[k] + oddPart;
result[k + half] = evenResult[k] - oddPart;
}
return result;
}