Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using of getSpectrum() in Libgdx library

I know the first thing you are thinking is "look for it in the documentation", however, the documentation is not clear about it.

I use the library to get the FFT and I followed this short guide: http://www.digiphd.com/android-java-reconstruction-fast-fourier-transform-real-signal-libgdx-fft/

The problem arises when it uses:

   fft.forward(array);
   fft_cpx=fft.getSpectrum();
   tmpi = fft.getImaginaryPart();
   tmpr = fft.getRealPart();

Both "fft_cpx", "tmpi", "tmpr" are float vectors. While "tmpi" and "tmpr" are used for calculate the magnitude, "fft_cpx" is not used anymore.

I thought that getSpectrum() was the union of getReal and getImmaginary but the values are all different. Maybe, the results from getSpectrum are complex values, but what is their representation?

I tried without fft_cpx=fft.getSpectrum(); and it seems to work correctly, but I'd like to know if it is actually necessary and what is the difference between getSpectrum(), getReal() and getImmaginary().

The documentation is at: http://libgdx-android.com/docs/api/com/badlogic/gdx/audio/analysis/FFT.html

public float[] getSpectrum()

Returns: the spectrum of the last FourierTransform.forward() call.

public float[] getRealPart()

Returns: the real part of the last FourierTransform.forward() call.

public float[] getImaginaryPart()

Returns: the imaginary part of the last FourierTransform.forward() call.

Thanks!

like image 516
Gabrer Avatar asked Jan 15 '13 13:01

Gabrer


1 Answers

getSpectrum() returns absolute values of complex numbers.

It is calculated like this

for (int i = 0; i < spectrum.length; i++) {
    spectrum[i] = (float)Math.sqrt(real[i] * real[i] + imag[i] * imag[i]);
}
like image 126
son of the northern darkness Avatar answered Oct 18 '22 02:10

son of the northern darkness