0

I have an Fast Fourier Transform function in Python for versions 2.x. I want to make it in Python 3.x, but I have some problems with "xrange" and list identifiers(as my compiler said). I also have no idea how to compute Inversed FFT from my FFT without using of any non-standard libraries. Code is below. Thanks in advance...

 from cmath import exp,pi

 def FFT(X):
  n = len(X)
  w = exp(-2*pi*1j/n)
  if n > 1:
    X = FFT(X[::2]) + FFT(X[1::2])
    for k in xrange(n/2):
        xk = X[k]
        X[k] = xk + w**k*X[k+n/2]
        X[k+n/2] = xk - w**k*X[k+n/2]
 return X 

UPD: Totally reconstructed ,my FFT and constructed IFFT due to your advices. P.S. How to close post?

Reodont
  • 329
  • 1
  • 14
  • don't trust `rfft` and `irfft` in `numpy.fft`? – abcd Apr 21 '15 at 19:39
  • it should be enough to replace `xrange` with `range` to make the code work with python 3. – jepio Apr 21 '15 at 19:43
  • Haven`t saw them. But i want to know how to rewrite this code. Because it is very short and easy to remeber. `Cause i need to remember this code before programming competition. And in those libraries codes are quite long. – Reodont Apr 21 '15 at 19:45
  • Jepio, i tried to do it, but i had some errors with it. Traceback (most recent call last): File "C:\Users\Aspartum\Desktop\qwr.py", line 13, in print (FFT([32,432,432])) File "C:\Users\Aspartum\Desktop\qwr.py", line 7, in FFT X = FFT(X[::2]) + FFT(X[1::2]) File "C:\Users\Aspartum\Desktop\qwr.py", line 8, in FFT for k in range(n/2): TypeError: 'float' object cannot be interpreted as an integer – Reodont Apr 21 '15 at 19:48
  • @Wespocan: That's fixed by changing the `/` to a `//` (to force integer division). See [PEP 238](https://www.python.org/dev/peps/pep-0238/). – Kevin Apr 21 '15 at 20:01

1 Answers1

0

There are a couple ways to convert your FFT into an IFFT. The easiest is to get rid of the minus sign inside the parameter to your exp() function for w. The next is to take the complex conjugate of the FFT of the complex conjugate of the input.

If you don't scale your forward FFT, then common practice is to scale your IFFT computation by 1/N (the length), so that IFFT(FFT()) results in the same total sum magnitude. If you do scale your FFT by 1/N, then don't scale your IFFT computation. Or scale both by 1/sqrt(N).

hotpaw2
  • 70,107
  • 14
  • 90
  • 153
  • Thanks for your reply. Have another question. If advance to FFT real input data (not complex), if I IFFT this real data FFT I should have complex data with zero imaginary part(like 23.23141 + 0j), or something else? Because I`m not so good at math, and quite young.. – Reodont Apr 22 '15 at 13:15
  • An IFFT(FFT()) of strictly real data will incur arithmetic rounding errors or numerical noise. So the imaginary part (and thus the phase) won't be zero, but should be ignored as too tiny to be significant or useful. – hotpaw2 Apr 24 '15 at 19:16
  • I want to make FFT multiplication of two big integers. I FFT first number. Then FFT second number. Multiplicate elements of them (like [q*w for q,w in zip(a,b)]) and then calculate IFFT of this multiplication. But it produce some unknow data. Example: Input: 10 10 Output: [(0.5+0j), (0.5+0j)] – Reodont Apr 25 '15 at 15:48