我在python中有以下代码:
import numpy as np
import matplotlib.pyplot as plt
from scipy.fft import fft, ifft, fftshift
def FourierTrans(S, t):
fs = 1 / (t[5] - t[4])
y = fft(S, len(S)*10)
n = len(y)
fshift = np.arange(-n/2,n/2)*(fs/n)
yshift = fftshift(y)
return fshift, yshift
def InvFourierTrans(Amp, freq):
fs = 1/(freq[6]-freq[5])
signal = ifft(Amp, len(Amp)*10)
n = len(signal)
time = np.arange(-n/2, n/2)*(fs/n)
signal = fftshift(signal)
return time, signal
plt.figure()
t = np.arange(-2.5, 2.5, 0.001)
sigma=np.diff(t)[0]*10
theta = 10000*t**2
y = np.exp(1j*theta) * np.exp(-t**2 / (2*sigma**2))
xnot, Snot= FourierTrans(y, t)
xinv, Sinv= InvFourierTrans(Snot, xnot)
plt.plot(xinv,np.angle(Sinv),linewidth=1,linestyle='-')
plt.plot(xinv, abs(Sinv)/max(abs(Sinv)),linewidth=1,linestyle='-')
plt.legend()
plt.grid()
plt.show()
我希望 np.angle(Sinv) 与 np.angle(y) 相同,但事实并非如此。我怎样才能从 Sinv 检索 theta? (知道 abs(y) 和 abs(Sinv) 完全一致,无论 x 轴有什么变化)。
我把
InvFourierTrans
改成了:
def InvFourierTrans(Amp, freq):
fs = 1/(freq[6]-freq[5])
Amp = fftshift(Amp)
signal = ifft(Amp, len(Amp)*1)
n = len(signal)
time = np.arange(-n/2, n/2)*(fs/n)
signal = fftshift(signal)
return time, signal
这解决了我的问题