matplotlib 如何获得PSD的幅值

7lrncoxx  于 2022-12-19  发布在  其他
关注(0)|答案(1)|浏览(96)

我在这里分享我提取的python代码:Plotting a fast Fourier transform in Python。我正在制作傅里叶频谱,然后我想“挑选”从图上的特定点获得值。在下一张图片中,您可以看到50 Hz频率,但我想知道下一个峰值的频率值。我如何找到它?

import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack

N = 600
# sample spacing
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = scipy.fftpack.fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)

fig, ax = plt.subplots()
ax.plot(xf, 2.0/N * np.abs(yf[:N//2]))
plt.show()
txu3uszq

txu3uszq1#

以下代码的结果将峰分离为50.836120401337794

import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack

# Set the parameters for the signal and the Fourier transform
N = 600
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)
yf = scipy.fftpack.fft(y)
xf = np.linspace(0.0, 1.0/(2.0*T), N//2)

# Find the indices of the peaks in the spectrum
peak_indices = np.argmax(np.abs(yf[:N//2]), axis=0)

# Get the corresponding frequency values from the xf array
peaks = xf[peak_indices]

# Print the frequency values of the peaks
print(peaks)

# Generate the Fourier spectrum plot
fig, ax = plt.subplots()
ax.plot(xf, 2.0/N * np.abs(yf[:N//2]))
plt.show()

相关问题