python中的多锥谱分析



我使用python上的频谱库进行多锥分析(https://pyspectrum.readthedocs.io/en/latest/install.html),但我不能完全理解输出的幅度。

这里有一段代码用于说明:

from spectrum import *
N=500
dt=2*10**-3
# Creating a signal with 2 sinus waves.
x = np.linspace(0.0, N*dt, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
# classical FFT
yf = fft.fft(y)
xf = np.linspace(0.0, 1.0/(2.0*dt), N//2)
# The multitapered method
NW=2.5
k=4
[tapers, eigen] = dpss(N, NW, k)
Sk_complex, weights, eigenvalues=pmtm(y, e=eigen, v=tapers, NFFT=500, show=False)
Sk = abs(Sk_complex)
Sk = np.mean(Sk * np.transpose(weights), axis=0)
# ploting both the results
plt.plot(xf,abs(yf[0:N//2])*dt*2)
plt.plot(xf,Sk[0:N//2])

两个结果是相似的,并且在50和80Hz处发现频率峰值。经典FFT也发现了良好的幅度(1和0.5(但是多锥法并没有找到合适的振幅。在这个例子中,它大约是重要性的5倍。有人知道如何正确显示结果吗?感谢

根据我的理解,这里有几个因素在起作用。

首先,要获得功率谱密度的多任务估计,您应该这样计算:

Sk = abs(Sk_complex)**2
Sk = np.mean(Sk * np.transpose(weights), axis=0) * dt

也就是说,你需要平均功率谱,而不是傅立叶分量。

其次,要获得功率谱,你只需要用fft将能谱除以估计值的N,然后像你那样乘以dt(你需要**2来从傅立叶分量中获得功率(:

plt.plot(xf,abs(yf[0:N//2])**2 / N * dt)
plt.plot(xf,Sk[0:N//2])

最后,应该直接比较的不是功率谱密度中的振幅,而是总功率。您可以查看:

print(np.sum(abs(yf[0:N//2])**2/N * dt), np.sum(Sk[0:N//2]))

非常匹配。

所以你的整个代码变成了:

from spectrum import *
N=500
dt=2*10**-3
# Creating a signal with 2 sinus waves.
x = np.linspace(0.0, N*dt, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
# classical FFT
yf = fft.fft(y)
xf = np.linspace(0.0, 1.0/(2.0*dt), N//2)
# The multitapered method
NW=2.5
k=4
[tapers, eigen] = dpss(N, NW, k)
Sk_complex, weights, eigenvalues=pmtm(y, e=eigen, v=tapers, NFFT=N, show=False)
Sk = abs(Sk_complex)**2
Sk = np.mean(Sk * np.transpose(weights), axis=0) * dt
# ploting both results
plt.figure()
plt.plot(xf,abs(yf[0:N//2])**2 / N * dt)
plt.plot(xf,Sk[0:N//2])
# ploting both results in log scale
plt.semilogy(xf, abs(yf[0:N // 2]) ** 2 / N * dt)
plt.semilogy(xf, Sk[0:N // 2])
# comparing total power
print(np.sum(abs(yf[0:N//2])**2 / N * dt), np.sum(Sk[0:N//2]))

最新更新