Mar 4, 2011 at 3:42 PM
Edited Mar 4, 2011 at 3:43 PM

I've finally got the FFT figured out, but I used the publicly available FFTPACK at http://www.netlib.org/fftpack/ The library is written in fortran but you can create a DLL that can be used in
whatever language you want, and will run extremely fast. I used
rfftf.f because I'm only dealing with real values. Instructions on creating a dll from a fortran library can be found here:
http://afome.com/fortrandll.php Once you have your dll working, to use it you just do the following. At the top of your code, insert:
using System.Runtime.InteropServices;
Then in your code, insert:
[DllImport("FFT.dll")] public static extern Int32 rffti(ref Int32 size, float[] workArray); [DllImport("FFT.dll")] public static extern Int32 rfftf(ref Int32 size, float[] mainArray, float[] workArray);
Where FFT.dll is your fortran compiled library, and rffti and rfftf are the functions I exported in the dll. Below is the code for the actual usage:
int sampleRate = 2048;
float frequency = 500;
float[] data = new float[sampleRate];
int j = 0;
for (float i = 0; i < 1; i += 1f / sampleRate)
{
data[j] = (float)(Math.Sin(2 * Math.PI * frequency * i));
j++;
}
float[] workArray = new float[(sampleRate * 2) + 15];
rffti(ref sampleRate, workArray);
rfftf(ref sampleRate, data, workArray);
To get your actual frequency bin values back, you'll need to take the distance of every point as your final value. For example, to write them to a file:
StreamWriter striter = new StreamWriter(@"C:\fft.txt");
for (int i = 1; i < data.Length  1; i += 2)
{
striter.WriteLine(Math.Sqrt(data[i] * data[i] + data[i + 1] * data[i + 1]));
}
striter.Close();
You'll notice that I started at 1 rather than 0. The zero spot holds the average power of your FFT. Hope this is useful to someone other than myself. Also, you'll have to map the output to your frequency bins to 1 over your sampleRate, if I remember correctly.
