# [PYTHON MATRIX-SIG] FFT

Jim Hugunin hugunin@mit.edu
Fri, 7 Mar 1997 11:04:01 -0500

```Duncan Child writes:
>
> Hello all,
>
> Having much fun with Numeric Python but I am a bit confused
> by the FFT module. Any suggestions gratefully accepted as to
> what is wrong with the following:
>
>
> #!/usr/local/bin/python
>
> from Numeric import *
> from FFT import *
>
> raw_a = array ( [ 1.3 , 1.2, 1.5, 1.4 ]  )
>
> print "raw_a ",raw_a
>
> freq_a = real_fft (raw_a)
>
> print "freq_a ",freq_a

real_fft returns the left half of the fft of a real array (the fft of a
real array is conjugate symmetric, so only half of it is needed).  Notice
that you can use the regular fft function on a real array and you'll get
back the whole transform.  This is probably normally your best choice.  The
real_fft function is just a little bit more efficient (it only has to do
half the work).  It's there for those frequent cases where you'll take the
fft of a real signal and then immediately throw out the right half.

> prep_b = inverse_real_fft (freq_a)

This should raise an exception (I added it to my bug list, but the fix
might not happen before the next beta as it's not a "real" error).
inverse_real_fft is not the inverse function of real_fft, but it instead
returns the left half of the inverse fft of a real valued vector.  This
naming convention is from fftpack, and I don't think it's bad enough to
fix.

> print "prep_b ",prep_b
>
> print "length of arrays: ",len(raw_a),len(freq_a),len(prep_b)

Do all of these results make sense now?

> If I swap fft for real_fft and inverse_fft for inverse_real_fft the
> results seem OK.

That's good!  inverse_fft(fft(a)) == a - give or take machine precision
issues.  This is not at all true of real_fft and inverse_real_fft.  I'll
add some notes to the manual, but the take-home message is that real_fft is
an unusual function that should only be used in special circumstances (I
added it because the speed is essential for computing speech spectrograms
and for 2d fft's of images...)

-Jim

_______________
MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org