如何从fft函数获取频率轴? [英] How to get frequency axis from an fft function?

查看:140
本文介绍了如何从fft函数获取频率轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我可能缺少明显的东西,但是我搜索了许多教程和文档,但似乎找不到直接的答案.如何找到在Python中执行fft的函数的频率轴(特别是scipy库中的fft)?

So, I am probably missing something obvious, but I have searched through lots of tutorials and documentation and can't seem to find a straight answer. How do you find the frequency axis of a function that you performed an fft on in Python(specifically the fft in the scipy library)?

我试图获取原始的EMG信号,对其执行带通滤波器,然后执行fft来查看其余的频率分量.但是,我不确定如何找到准确的x组件列表.我目前正在处理的特定信号以1000 Hz采样,并有5378个采样.

I am trying to get a raw EMG signal, perform a bandpass filter on it, and then perform an fft to see the remaining frequency components. However, I am not sure how to find an accurate x component list. The specific signal I am working on currently was sampled at 1000 Hz and has 5378 samples.

是否只是创建一个从0开始到fft'd数据长度的线性x?我看到很多人在创建从0到采样点乘以采样间隔的linspace.但是在这种情况下,我的样本间距是多少?只是采样率/采样率吗?还是完全其他的东西?

Is it just creating a linear x starting from 0 and going to the length of the fft'd data? I see a lot of people creating a linspace from 0 to sample points times the sample spacing. But what would be my sample spacing in this case? Would it just be samples/sampling rate? Or is it something else completely?

推荐答案

这里是一个示例.

首先创建一个具有预定采样间隔的正弦波.我们将结合两个频率为20和40的正弦波.请记住,如果时间间隔较长,则高频可能会混叠.

First create a sine wave with sampling interval pre-determined. we will combine two sine waves with frequencies 20 and 40. Remember high frequencies might be aliased if the time interval is large.

#Import the necessary packages
from scipy import fftpack
import matplotlib.pyplot as plt
import numpy as np

# sampling freq in herts 20Hz, and 40Hz
freq_sampling1 = 10
freq_sampling2 = 20
amplitude1 = 2 # amplitude of first sine wave
amplitude2 = 4 # amplitude of second sine wave
time = np.linspace(0, 6, 500, endpoint=True) # time range with total samples of 500 from 0 to 6 with time interval equals 6/500
y = amplitude1*np.sin(2*np.pi*freq_sampling1*time) + amplitude2*np.sin(2*np.pi*freq_sampling2*time)

plt.figure(figsize=(10, 4))
plt.plot(time,y, 'k', lw=0.8)
plt.xlim(0,6)
plt.show()

图中注意两个正弦波叠加在一起.与频率一. 10和振幅2,另一个具有频率. 20和振幅4.

Notice in the figure that two sine waves are superimposed. One with freq. 10 and amplitude 2 and the other with freq. 20 and amplitude 4.

# apply fft function
yf = fftpack.fft(y, time.size)

amp = np.abs(yf) # get amplitude spectrum 
freq = np.linspace(0.0, 1.0/(2.0*(6/500)), time.size//2) # get freq axis

# plot the amp spectrum

plt.figure(figsize=(10,6))
plt.plot(freq, (2/amp.size)*amp[0:amp.size//2])
plt.show()

在幅度频谱中注意,两个频率已恢复,而其他频率处的幅度为零.振幅值也分别是2和4.

Notice in the amplitude spectrum the two frequencies are recovered while amplitude is zero at other frequencies. the Amplitude values are also 2 and 4 respectively.

您可以改用fftpack.fftfreq来获取 tom10 建议的频率轴 因此,代码更改为

you can use instead fftpack.fftfreq to obtain frequency axis as suggested by tom10 Therefore, the code changes to

yf = fftpack.fft(y, time.size)
amp = np.abs(yf) # get amplitude spectrum 
freq = fftpack.fftfreq(time.size, 6/500)
plt.figure(figsize=(10,6))
plt.plot(freq[0:freq.size//2], (2/amp.size)*amp[0:amp.size//2])
plt.show()

我们只绘制幅度谱的正部分[0:amp.size//2]

We are only plotting the positive part of the amplitude spectrum [0:amp.size//2]

这篇关于如何从fft函数获取频率轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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