简体   繁体   English

我如何使用 scipy plot 和 python 中的 fft 并修改频率范围,使其在中心显示两个峰值频率?

[英]How do I plot an fft in python using scipy and modify the frequency range so that it shows the two peaks frequencies in the center?

The following Python code uses numpy to produce a frequency plot of a sinusoid graph:以下 Python 代码使用 numpy 生成正弦曲线图的频率 plot:

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

# Number of samplepoints
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()

在此处输入图像描述

Based on the code above, we are plotting a sine wave with two frequencies, one at 50Hz and another at 80 Hz.基于上面的代码,我们绘制了一个具有两个频率的正弦波,一个频率为 50Hz,另一个频率为 80Hz。 You can clearly see the Fourier transform plot shows peaks at those two frequencies.您可以清楚地看到傅立叶变换 plot 显示了这两个频率的峰值。

My question: How do I modify the above code so that the x-axis ranges from 0-100Hz?我的问题:如何修改上面的代码,使 x 轴的范围为 0-100Hz?

If I change如果我改变

xf = np.linspace(0.0, 1.0/(2.0*T), N//2)

to

xf = np.linspace(0.0, 100, N//2)

Then my graph looks like:然后我的图表看起来像:

在此处输入图像描述

But the graph now shows my peaks at around 11 and 20Hz, which is not correct.但是图表现在显示我的峰值在 11 和 20Hz 左右,这是不正确的。 When I change my axis, the peak values should not change.当我改变轴时,峰值不应改变。

What am I doing wrong?我究竟做错了什么?

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

# Number of samplepoints
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]))
ax.set(
    xlim=(0, 100)
)
plt.show()

simply add the xlim只需添加xlim

在此处输入图像描述

An alternate solution is to plot the appropriate range of values.另一种解决方案是 plot 的适当范围值。

When performing a FFT, the frequency step of the results, and therefore the number of bins up to some frequency, depends on the number of samples submitted to the FFT algorithm and the sampling rate.执行 FFT 时,结果的频率步长以及达到某个频率的 bin 数量取决于提交给 FFT 算法的样本数量和采样率。 So there is a simple calculation to perform when selecting the range to plot, eg the index of bin with center f is:因此,选择到plot的范围时,可以执行一个简单的计算,例如,带有中心F的bin索引为:

idx = ceil(f * t.size / sr)

This code works:此代码有效:

from numpy import arange, sin, pi, abs
from math import ceil
import matplotlib.pyplot as plt
from scipy.fft import rfft, rfftfreq

sr = 800 # Sampling rate = 800Hz
N = 600 # Number of samplepoints (duration = 0.75s)
t = arange(0, N/sr, 1/sr) # Sampling times

# Sampled values
s = sin(50 * 2*pi*t) + 0.5*sin(80 * 2*pi*t)

# FFT and bin centers
y = rfft(s)
f = rfftfreq(t.size, 1/sr)

# Index of 100Hz bin
f_hi = 100
idx_hi = ceil(f_hi * t.size / sr)

# Plot spectrum
fig, ax = plt.subplots()
ax.plot(f[:idx_hi], abs(y[:idx_hi]))
plt.show()

and produces:并产生:

在此处输入图像描述


As you only work with the real signal, you can cut computing time using rfft instead of fft which works with complex values.由于您只处理真实信号,因此可以使用rfft而不是处理复数的fft来缩短计算时间。 To get the frequency bin centers, you can use rfftfreq要获得频点中心,您可以使用rfftfreq

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 FFT 中找到峰值和相应的频率? - How do I find the Peaks and the corresponding frequencies from the FFT? 如何使用 scipy FFT 限制频率范围 - How to limit frequency range using scipy FFT 我如何找到 plot 和 output Python 中实时绘制的快速傅立叶变换 (FFT) 的峰值? - How do I find, plot, and output the peaks of a live plotted Fast Fourier Transform (FFT) in Python? 使用heapq函数“最大”在Python中查找FFT的峰值及其对应的频率 - Using the heapq function 'nlargest' to find the peaks of an FFT and their corresponding frequencies in python 如何使用Python绘制直方图,以使x值成为频谱的频率? - How do I plot a histogram using Python so that x-values are frequencies of a spectra? 如何使 plot 与日期时间和 Python 中的 scipy 峰值? - How to make plot with datetime and scipy peaks in Python? 如何使用Python找到FFT图的峰值? - How to find peaks of FFT graph using Python? 使用Python Numpy / Scipy进行FFT确定超低频过程 - FFT Determining ultra low frequency processes using Python Numpy/Scipy 需要帮助选择频率范围以进行FFT操作后的峰搜索 - Need help selecting frequency range for peaks search after FFT operation 如何找到作者的频率并使用Python进行绘制? - How do I find frequency of Authors and plot this using Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM