Signature
public static void FFT( float[] data, int length, FourierDirection direction )
- You pass an array of complex numbers, represented as pairs. Since you only have real numbers (samples), you should place your samples in even locations in the array - data [0], data [2], data [4], etc. Odd locations must be 0, data [1] = data [3] = 0 ...
- Length is the number of samples for which you want to calculate your FFT; it should be exactly half the length of the data array. You can use FFT with all of your WAV or parts of it, depending on what you want to see. Audacity will display the power spectrum of the selected part of the file, if you want to do the same, transfer the entire WAV or selected parts.
- FFT will only show frequencies up to half the sampling frequency. Thus, you should have values โโfrom 0 to half the sampling rate. The number of values โโdepends on the number of samples that you have (the number of samples affects the accuracy of the calculation).
- Audacity displays the power spectrum. You must take each pair of complex numbers in the resulting array and calculate its ABS. ABS is defined as sqrt (r ^ 2 + i ^ 2). Each ABS value will correspond to one frequency.
Here is an example of working code:
float[] data = new float[8]; data[0] = 1; data[2] = 1; data[4] = 1; data[6] = 1; Fourier.FFT(data, data.Length/2, FourierDirection.Forward);
I give him 4 samples, anyway. Therefore, I expect to get something only at frequency 0. And indeed, after starting, I get
data [0] == 1, data [2] == 1, data [4] == 1, data [6] == 1
And others - 0.
If I want to use complex array overloading
Complex[] data2 = new Complex[4]; data2[0] = new Complex(1,0); data2[1] = new Complex(1, 0); data2[2] = new Complex(1, 0); data2[3] = new Complex(1, 0); Fourier.FFT(data2,data2.Length,FourierDirection.Forward);
Please note that here the second parameter is equal to the length of the array, since each element of the array is a complex number. I get the same result as before.
I think I missed a complicated overload before. I seem to be less error prone and more natural to use, as long as your data already falls in pairs.
source share