我使用这段 python 代码将数据转换为傅里叶相位和幅度,然后检索原始数据。
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
当我比较输出时,结果是不同的。例如,两个典型的输出是:
//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]]
我知道它背后的理论。例如,为了计算相位和幅度,我们这样做:
Magnitude = Square_Root(real*real+imaginary*imaginary)
Phase = Inverse_Tangent(imaginary/real)
为了从幅度和相位计算复数,我们这样做:
Real = Magnitude * cos(Phase)
Imaginary = Magnitude * sin(Phase)
那么,问题的原因是什么。是否是数据扩展问题。