关注我的问题:我有一个PSD的理论模型,在此基础上我计算了一个时间序列。经过一些修改后,这个时间序列将被转换回我的PSD。不幸的是,我没有得到我原来的PSD,但是有点不同,我不知道为什么......
这是我的一些说明性代码:
import numpy as np
from scipy import fftpack
import matplotlib.pyplot as plt
bins = [1,2,3,4,5,6] #spots in the frequency domain
PSD = [7,8,7,10,6,3]
plt.loglog(bins, PSD)
t = np.linspace(0,1,2**9, endpoint = 'false')
signal = np.zeros_like(t)
for i in range(6):
signal += np.sqrt(PSD[i]) * np.cos(2*np.pi* t * bins[i] + random.uniform(0,2*np.pi))
n = signal.size
timestep = t[1]-t[0]
freq = np.fft.fftfreq(n, d=timestep)
freq = freq[:freq.size/2]
PSD_from_timeserie = abs(scipy.fftpack.fft(signal)/ n * 2)**2
PSD_from_timeserie = PSD_from_timeserie[:PSD_from_timeserie.size/2]
plt.loglog(freq, PSD_from_timeserie, 'x')
plt.show()
这几乎可以提供正确的PSD,因为您可以轻松查看...有关错误位置的任何建议吗?
答案 0 :(得分:0)
问题解决了!
必须是
t = np.linspace(0,1,2**9, endpoint = False)
使用大写并使用'... info numpy不会抛出错误,但这种方式有效;)'