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):
vals[i].append(RI(0,random_range))
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):
magnitudes[i].append(math.sqrt(fftc[i][j].real**2+fftc[i][j].imag**2))
phases[i].append(math.atan(fftc[i][j].imag/fftc[i][j].real))
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])
for_ifft[i].append(complex(rl,im))
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.