带有scipy.fftpack的fft图中出现恒定线 [英] constant lines occur in plot of fft with scipy.fftpack
问题描述
当我在信号上使用scipy.fftpack计算FFT并将其绘制后,我得到一条恒定的水平线(以及一条垂直线在我的数据上),谁能解释为什么会出现这些线并提出绘制图的解决方案没有谱线的光谱?
When I am computing a FFT with scipy.fftpack on a signal and plot it afterwards, I get a constant horizontal line (and a vertical line on my data) Can anyone explain why these lines occur and maybe present a solution to plot the spectrum without the lines?
让我们看一个简单的信号.正弦波,频率为12赫兹.在此示例中,您可以在y = 2.1
处看到水平线.
Let's take a look at a simple signal. Sinusoidal wave with a frequenz of 12 Hertz. In this example you can see the horizontal line at about y = 2.1
.
import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack
# test signal
omega = 2 * np.pi * 12
t = np.linspace(-15, 15, 1000)
ampl = np.sin(omega * t)
# compute fft, fft frequences
fft = abs(scipy.fft(amp))
freq = scipy.fftpack.fftfreq(fft.size, t[1] - t[0])
# plot
plt.plot(freq, fft)
这是我的实验信号的fft图(放大).您可以看到水平线(y =~ 0.0015
)和垂直线(在x = 0
).
And here's the fft plot from my experimental signal (zoomed in). You can see the horizontal line (y =~ 0.0015
) and also a vertical line (at x = 0
).
根据这些行,我无法在数组中找到任何数据.为什么会出现这些行?
I cannot find any data in my arrays according to these lines. Why do these lines occur?
推荐答案
您正确的是,此行连接了第一个和最后一个数据.正如@SleuthEye先前解释的那样,标准fft
函数倾向于先绘制正频率,然后绘制负频率.
You are right that this line joins the first and last data. As previously explained by @SleuthEye, standard fft
function prefers to plot the positive frequencies first, then the negative frequencies.
通过添加第三和第四行来解决此问题非常容易,如下所示:
It is very easy to fix this problem by adding the third and fourth lines, as follows
fftData = np.fft.fft(data)
freq = np.fft.fftfreq(lenData, 1/fSamp)
fftData = np.fft.fftshift(fftData)
freq = np.fft.fftshift(freq)
这篇关于带有scipy.fftpack的fft图中出现恒定线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!