Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/visual-studio-code/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中带谱的多锥度谱分析_Python_Spectrum - Fatal编程技术网

python中带谱的多锥度谱分析

python中带谱的多锥度谱分析,python,spectrum,Python,Spectrum,我使用python()上的频谱库进行多锥度分析,但我不能完全理解输出的振幅 这里有一段代码用于说明: 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 = ff

我使用python()上的频谱库进行多锥度分析,但我不能完全理解输出的振幅

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

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和80赫兹。 经典FFT也能找到良好的振幅(1和0.5) 但多锥度法没有找到合适的振幅。在本例中,它大约是重要性的5倍。 有人知道如何正确显示结果吗?
谢谢

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

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

Sk = abs(Sk_complex)**2
Sk = np.mean(Sk * np.transpose(weights), axis=0) * dt
也就是说,你需要平均功率谱,而不是傅里叶分量

其次,为了得到功率谱,你只需要用fft将能量谱除以你估计的N,再乘以dt,就像你所做的那样(你需要**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]))

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

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

Sk = abs(Sk_complex)**2
Sk = np.mean(Sk * np.transpose(weights), axis=0) * dt
也就是说,你需要平均功率谱,而不是傅里叶分量

其次,为了得到功率谱,你只需要用fft将能量谱除以你估计的N,再乘以dt,就像你所做的那样(你需要**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]))

如果下面的答案回答了您的问题,请将其标记为。如果没有,请告诉我们遗漏了什么。如果下面的答案回答了您的问题,请标记为遗漏。如果没有,请告诉我们遗漏了什么。谢谢,我完全错过了有人的回答!我会仔细看看的!谢谢,我完全没有听到有人回答!我会仔细看看的!