我试图编写一个简单的python脚本,从它的fourier变换中恢复正弦波的振幅和相位。
我应该能够通过计算给定频率的傅里叶变换的实数和虚数所定义的向量的大小和方向来做到这一点,即:
Amplitude_at_freq = sqrt(real_component_at_freq^2 + imag_component_at_freq^2)
Phase = arctan(imag_component_at_freq/real_component_at_freq)
参考:1分钟45秒进入这个视频:continue=106&v=IWQfj05i87g
我用numpy的fft库编写了一个简单的python脚本来尝试重现这个过程,但是尽管我的推导和上面一样,我无法得到振幅和相位,尽管我可以正确地恢复测试正弦波的原始频率。上一篇文章从np.fft计算振幅和增加信号长度时FFT不恢复原始幅值的原因指出了同一个问题(其中振幅是2的因子)。具体来说,解决方案是“乘以2(除去一半的光谱,所以能量必须保持)”,但我需要澄清这意味着什么。其次,我没有提到恢复相位变化的问题,而且振幅的计算与这里的情况不同。
# Define amplitude, phase, frequency
_A = 4 # Amplitude
_p = 0 # Phase shift
_f = 8 # Frequency
# Construct a simple signal
t = np.linspace(0, 2*np.pi, 1024 + 1)[:-1]
g = _A * np.sin(_f * t + _p)
# Apply the fourier transform
ff = np.fft.fft(g)
# Get frequency of original signal
ff_ii = np.where(np.abs(ff) > 1.0)[0][0] # Just get one frequency, the other one is just mirrored freq at negative value
print('frequency of:', ff_ii)
# Get the complex vector at that frequency to retrieve amplitude and phase shift
yy = ff[ff_ii]
# Calculate the amplitude
T = t.shape[0] # domain of x; which we will divide height to get freq amplitude
A = np.sqrt(yy.real**2 + yy.imag**2)/T
print('amplitude of:', A)
# Calculate phase shift
phi = np.arctan(yy.imag/yy.real)
print('phase change:', phi)
然而,我得到的结果是:
>> frequency of: 8
>> amplitude of: 2.0
>> phase change: 1.5707963267948957
所以频率是准确的,但是当它应该是4的时候,振幅是2,π/2的相位变化,当它应该是零时。
我的数学是错的,还是我对numpy的fft实现的理解不正确?
发布于 2019-10-20 20:34:00
https://stackoverflow.com/questions/58475809
复制相似问题