python中的FFT结果取决于所选择的linspace? [英] FFT-results in python dependend on linspace chosen?

查看:55
本文介绍了python中的FFT结果取决于所选择的linspace?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python的新手,我有一个基本的理解问题.在我看来,FFT的结果只是基于自己选择的线性空间.

I'm very new to Python and I have a basic understanding problem. To me it seems that the result of an FFT is just based on the linspace chosen by oneself.

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

plt.plot(xf, 2.0/N * np.abs(yf[0:N/2]))
plt.grid()
plt.show()

通过运行此fft作为示例,我得到了两个尖峰,分别为50 Hz和80 Hz.当我将xf更改为:

By running this fft as an example I get two spikes, at 50 and at 80 Hz. When I change xf to:

xf = p.linspace(0.0, 5.0/(2.0*T), N/2)

尖峰在250和400 Hz左右.

the spikes are around 250 and 400 Hz.

这不是意味着我必须事先知道正确的结果(在这种情况下,输入信号所包含的两个窦波的频率),以便以后可以调整轴的缩放比例以适合这些结果?可能不是,所以很高兴有人可以解释这个问题.

Doesn't this mean, that I have to know the correct results beforehand (in this case the frequencies of the two sinus waves the input signal consists of), so that I can later adjust the scaling of the axis to fit those results? Probably not, so I'd be glad if someone could explain this problem.

推荐答案

您必须根据采样率,采样数和fft(NFFT)中使用的采样数来计算正确的频率.FFT算法不知道您使用的是时标.例如,频率轴也可以角频率给出,在这种情况下,您只需将轴标为2 * pi.

You must calculate the correct frequencies depending on sample rate, number of samples and samples used in the fft (NFFT). The FFT algorithm does not know what time-scale you are operating in. For example, the frequency axis could also be given in angular frequency in which case you would just scale the axis with 2*pi.

因此,您必须知道输入信号的采样率和采样数才能为FFT结果创建正确的频率轴-但是,您无需了解任何有关输入信号形状的信息(忽略混叠)关注).

Therefore, you must know the sample rate and number of samples of the input signal to create a correct frequency axis for the FFT result - you do not, however, need to know anything about the shape of the input signal (disregarding aliasing concerns).

可以使用以下公式计算频率[Hz]:

The frequencies [Hz] can be calculated using:

dt = 0.0001 # your T
Fs = 1 / dt # sample rate
xt = np.arange (0, 10, dt)
nt = len (xt) # length of time series, your N

# make signal
st = .5 * np.sin (50 * 2 * np.pi * xt) + .5 * np.sin (80 * 2 * np.pi * xt)

# take fourier transform and shift frequency spectra
S  = np.fft.fftshift(np.fft.fft (st))

## make argument array, frequency [Hz]
#
# extending from -Fs/2 to Fs/2, the Nyquist frequency, with the same
# number of samples as in the time series (xt and st).

freqs = Fs * np.linspace (-1/2, 1/2, nt)

# plot absolute value, scaled with number of samples
# ( note that it is also interesting to plot the complex angle
#   using np.angle () ). together they make a bode-plot.

plt.plot (freqs, np.abs(S) / nt)
plt.xlabel ('Frequency [Hz]')

这篇关于python中的FFT结果取决于所选择的linspace?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆