I am trying to find a solution for some weeks now and I really have read all threads related with this topic, so I really hope someone can help me out with this.
What I want to do is to use a wave-file as input, fourier-transform it and take these values to compare them to another file and get the difference.
The challenge I've been working on so long is to get that Fourier-transform to work.
Instead of giving me reasonable results I only get NaN-values.
I have the following code:
public FFTPerformer(float[] soundvalues)
{
buffer = new System.Numerics.Complex[4096];
try {
for (int i = 0; i < 4096; i++)
{
System.Numerics.Complex tmp = new System.Numerics.Complex(soundvalues[i], 0);
buffer[i] = tmp;
}
}
catch(Exception ex)
{
System.Windows.MessageBox.Show("Es ist ein Fehler bei der Konvertierung des float arrays zum Complex-Array aufgetreten; " + ex.Message);
}
}
The 4096 is normally replaced by a higher number which is still a power of two, but it doesn't even work with that one.
public void performFFT()
{
try
{
MathNet.Numerics.IntegralTransforms.Fourier.Forward(buffer, MathNet.Numerics.IntegralTransforms.FourierOptions.Matlab);
}
catch(Exception ex)
{
System.Windows.MessageBox.Show("Fehler: " + ex.Message);
}
}
Little more detailed description: I set the length for 4096 values, because that is a factor of two and this length is only for test purposes, so that this is a little bit faster to check than using the original soundfile of some millions of values ;) so this is only like this, as long the algorithm isn't proved to work. This is also the reason for the weird for-loop. Even tough Math.Net uses Bluestein's algorithm to calculate, try it with factor of two arrays anyway, because they just should work even if they use any other algorithm atm.
tried to get an error-message with that try-catch because of Float-Decimal conversion, but it doesn't raise any.
My problem is now, that I send 4096 Complex numbers in that fft, all of them have Y=0, but X is depending on the audio-file (read in another thread that Y-values are not needed for audio-fft so you should set them to 0). All of the X-values are normal float-numbers, no NaNs. But still, the FFT just gives back an array full of NaNs.
I first tried to do the conversion to System.Numerics.Complex implicit, which didn't work out, so I that that construct with the tmp variable.
EDIT: I ended up reading even more articles on FFT algorithms. Now I still get my soundvalues als float and convert them to a Complex-Array. I checked all conversion-steps to consistency and it's fine in that area. So my question is if I really only do have to use this
MathNet.Numerics.IntegralTransforms.Fourier.Forward(buffer, MathNet.Numerics.IntegralTransforms.FourierOptions.Matlab);
or if I need somehting before that to do an clean FFT in Math.NET