I use this snippet of python code to transform data to Fourier phase and magnitude and then retrieving original data.

from random import randint as RI
import numpy.fft as FFT
import math

w = 4
h = 4
random_range = 255

vals = [[] for i in range(h)]

for i in range(h):
    for j in range(w):

fftc = FFT.fft2(vals) 

magnitudes = [[] for i in range(h)]
phases = [[] for i in range(h)]

for i in range(h):
    for j in range(w):

for_ifft = [[] for i in range(h)]

for i in range(h):
    for j in range(w):
        rl = magnitudes[i][j]*math.cos(phases[i][j])
        im = magnitudes[i][j]*math.sin(phases[i][j])

ifftc = FFT.ifft2(for_ifft)

print vals
print ifftc

When i compare outputs, results are different. for example two typical outputs are:

//original data
[[115, 4, 33, 91], [228, 123, 46, 111], [83, 227, 81, 55], [229, 188, 138, 41]]

//retrieved data from phase and magnitude informations
[[ 309,182.25,105,57.25],[ 33,118.75,120.50,116.25],[ 148,128.75,79,94.75],[ 165,112.25,-6.50,29.75]]

I know the theory behind it. for example for calculating phase and magnitude we do this :

Magnitude = Square_Root(real*real+imaginary*imaginary)
Phase = Inverse_Tangent(imaginary/real)

and for calculating complex number from magnitude and phase we do this :

Real = Magnitude * cos(Phase)
Imaginary = Magnitude * sin(Phase)

So, what is the cause of problem. Is it a data scaling issue.

  • 4
    $\begingroup$ You should use "atan2" instead of "atan" to calculate the phase, because atan doesn't work in all quadrants. $\endgroup$
    – Jim Clay
    Commented Apr 24, 2013 at 13:21
  • $\begingroup$ Thanks. It works. I will use "atan2(Imaginary,Real)" instead of "atan(Imaginary/Real)". $\endgroup$ Commented Apr 24, 2013 at 13:44
  • $\begingroup$ @JimClay: You should convert your comment to an answer. $\endgroup$
    – Jason R
    Commented Apr 25, 2013 at 12:42

1 Answer 1


You need to change how you're calculating the phase from "atan" to "atan2". The reason is that atan doesn't work in all quadrants. More on atan2 here.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.