I have a float array that I get from a sensor and want to get the amplitude after running the signal through a FFT (before that, samples were processed using high-pass filter and Hann window function).
Using AForge.Math library FFT class, which takes as a parameter an array of complex numbers I’ve came up with the following code:
Complex[] complex = new Complex[1024];
for (int i = 0; i < 1024 - 1; i++)
{
complex[i] = new Complex(windowedSamples[i], 0);
}
FourierTransform.FFT(complex, FourierTransform.Direction.Forward);
return complex.Select(x => Math.Sqrt(Math.Sqrt(x.Re) + Math.Sqrt(x.Im))).ToArray();
Running this I got some strange results and have trouble locking on the problem.
First 10 input values:
0 0 -3.8454E-05 0.0001737584 0.0006910793 0.001071334 0.00204984 0.00276812 0.001741312 0.001796867
First 10 output values:
0.482303347948843 0.706458195192639 NaN NaN NaN NaN NaN NaN NaN NaN
My first guess was that it might be something with the imaginary part but so far everything I’ve read said that it should be set to 0 in this case.
I would really appreciate some help solving this.
This expression
looks a bit strange to me. Are you trying to get the absolute value of
xhere? Then you should probably raise the imaginary and the real part to the power of two, not take the square root of them.If the absolute value is what you want then just replace that expression with