Rahul Rentash
Rahul Rentash

Reputation: 23

How to get original sound after FFT?

I am applying a FFT to my sound like this:

from scipy.fft import fft

samplerate, data = wavfile.read('./sound.wav')
fft_sound = fft(data)

Is there a reversal method which I can use to go from fft_sound back to data?

Here's what I did:

samplerate, data = wavfile.read('./StarWars3.wav')

print(data)
print(ifft(fft(data)))

But the outcome looks completely different:

[    0     0     0 ... -1502    26   414]
[ 0.00000000e+00-1.17911210e-13j -6.75789623e-13-4.22368515e-14j
  0.00000000e+00+7.03947524e-14j ... -1.50200000e+03-5.15824216e-14j
  2.60000000e+01+3.23030463e-13j  4.14000000e+02+9.36792340e-13j]

Did I use it correctly?

Upvotes: 1

Views: 505

Answers (1)

mkrieger1
mkrieger1

Reputation: 23140

Is there a reversal method which I can use to go from fft_sound back to data?

Yes, it's called inverse discrete Fourier transform.

Just like the fft function, SciPy has a function for it; unsurprisingly named ifft.

Did I use it correctly?

It returns a complex-valued array, where the j suffix denotes the imaginary part (see Complex numbers in python and https://docs.python.org/3/library/stdtypes.html#numeric-types-int-float-complex).

As its documentation says,

[...] ifft(fft(x)) == x to within numerical accuracy.

The outcome is shown in scientific notation, so it looks different but numerically it is very close:

  • -1502 became -1.50200000e+03-5.15824216e-14j (real part is -1.50200000e+03, which is -1502; imaginary part is -5.15824216e-14, which is very close to 0)

  • 26 became 2.60000000e+01+3.23030463e-13j (real part is 2.60000000e+01, which is 26; imaginary part is 3.23030463e-13, which is very close to 0)

etc.

Upvotes: 1

Related Questions