在Python中计算FFT手抖动信号 [英] Computing FFT on hand tremor signal in Python
问题描述
因此,我们正在研究通过使用3轴加速度计从手抖动获得的信号。我们想要计算信号的FFT以找出主导频率。在matlab中获得的主导频率高达10 Hz,这是预期的但是,在python中获得的结果并不像预期的那样。有人请告诉我们在python中计算FFT的程序吗?
我有什么试过:
import csv
import numpy as np
# 将scipy导入为sy
# 将scipy.fftpack导入为syfp
# 将pylab导入为pyl
打开(' < span class =code-string> C:\ Users \Sharvari Inamdar \Desktop\F0005CH1.CSV') as csvfile:
spamreader = csv.reader(csvfile,delimiter = ' ',quotechar = ' |')
行 spamreader中的class =code-keyword>:
print (' ,' .join(row))
# 从此处读取文件中的数据
array = np.loadtxt( C:\ Users \Sharvari Inamdar \Desktop\F0005CH1.CSV)
来自 scipy.fftpack import fft
# 样本点数
N = len(数组)
# < span class =code-comment>样本间距
T = 1.0 / N
x = np.linspace( 0 。 0 , 2 * np.pi * 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 = fft(array)
xf = np.linspace( 0 。 0 , 1 .0 /( 2 。 0 * T),N / 2)
import matplotlib.pyplot as plt
plt.plot(xf, 2 .0 / N * np.abs(yf [< span class =code-digit> 0 :N / 2]))
plt.grid()
plt.show()
计划2
import csv
import numpy as np
import pylab as pyl
打开(' C:\ Users \Sharvari Inamdar \Desktop\F0005CH1.CSV') as csvfile:
spamreader = csv.reader(csvfile,delimiter = < span class =code-string>' ',quotechar = ' |')
行 spamreader :
print (' ,' .join(row))
# 从此处的文件读入数据
array = np.loadtxt( C:\Users \ Sharvari Inamdar \Desktop\F0005CH1.CSV)
Ts = 1.00E- 03 ;
Fs = 1 / Ts;
Fn = Fs / 2;
# np.isnan(array)= []
# Ly1 = array.shape
N = len(数组)
T = np.linspace( 0 , 1 ,N)* Ts
# pyl.plot(T,array)
来自 scipy.fftpack import fft
FF = fft(array / N);
# Fv = np.linspace(0,1,np.fix(N / 2) )+1)* Fn;
# Iv = len(Fv)
pyl.plot(T,FF)
我们已经尝试过上述2个程序和该程序的许多其他版本。
嗯,你没有具体说明你所看到的与众不同的东西......但我会抓住一个常见的问题。 FFT的结果在第一个区间中具有DC频率(即0),由实际频率频谱区段进行,然后由负频谱区间进行。使用移位功能将零容器移动到中间并重新排列负分量,使其为零,以便绘图:
fftshift - 重新排列fft输出,将零频率移动到频谱中心 [ ^ ]
间距频谱仓的基本上是Fs / N,其中Fs是采样频率,N是FFT大小。 FFT大小越大,给定采样率的bin间隔越紧(即频率分辨率越高)。由于计算复杂性,通常N将是2的幂...如果您向FFT库提供的样本少于2的幂,它们将零填充到最接近的2的幂。
至于图表的幅度缩放,那么......有一些缩放选项,但是1 / N和1 / sqrt(N)是常见的选项。
祝你好运!
So,we are working on a signal obtained from hand tremors by using a 3 axis accelerometer.We want to compute FFT of the signal to find out the dominating frequency.The dominating frequency obtained in matlab was upto 10 Hz which was expected.However, the result obtained in python are not as expected.Can someone please let us know the program for computation of FFT in python?
What I have tried:
import csv
import numpy as np
#import scipy as sy
#import scipy.fftpack as syfp
#import pylab as pyl
with open('C:\Users\Sharvari Inamdar\Desktop\F0005CH1.CSV') as csvfile:
spamreader=csv.reader(csvfile,delimiter=' ',quotechar='|')
for row in spamreader:
print(', '.join(row))
#Read in data from file here
array=np.loadtxt("C:\Users\Sharvari Inamdar\Desktop\F0005CH1.CSV")
from scipy.fftpack import fft
# Number of sample points
N = len(array)
# sample spacing
T=1.0/N
x = np.linspace(0.0, 2*np.pi*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 = fft(array)
xf = np.linspace(0.0, 1.0/(2.0*T), N/2)
import matplotlib.pyplot as plt
plt.plot(xf, 2.0/N * np.abs(yf[0:N/2]))
plt.grid()
plt.show()
program 2
import csv
import numpy as np
import pylab as pyl
with open('C:\Users\Sharvari Inamdar\Desktop\F0005CH1.CSV') as csvfile:
spamreader=csv.reader(csvfile,delimiter=' ',quotechar='|')
for row in spamreader:
print(', '.join(row))
#Read in data from file here
array=np.loadtxt("C:\Users\Sharvari Inamdar\Desktop\F0005CH1.CSV")
Ts=1.00E-03;
Fs=1/Ts;
Fn=Fs/2;
#np.isnan(array)=[]
#Ly1=array.shape
N=len(array)
T=np.linspace(0,1,N)*Ts
#pyl.plot(T,array)
from scipy.fftpack import fft
FF=fft(array/N);
#Fv=np.linspace(0,1,np.fix(N/2)+1)*Fn;
#Iv=len(Fv)
pyl.plot(T,FF)
we have tried the above 2 programs and many other versions of this program.
Well, you don't specify what exactly you're seeing that's different... but I'll take a stab at a common problem. The result of an FFT has the DC frequency (i.e. 0) in the first bin, proceeded by the real frequency spectral bins, then proceeded by the negative spectral bins. Use a "shift" function to shift the zero bin to the middle and re-arrange the negative components to be left of zero for plotting:
fftshift - Rearranges the fft output, moving the zero frequency to the center of the spectrum[^]
The spacing of the spectral bins is essentially Fs/N, where Fs is the sampling frequency and N is the FFT size. The larger the FFT size, the tighter your bin spacing for a given sample rate (i.e. you get more frequency resolution). Due to computational complexity, typically N will be a power of 2... if you feed less samples than a power of 2 to an FFT library, they'll zero pad up to the closest power of 2.
As for the magnitude scaling of your graph, well... there's a handful of scaling options but 1/N and 1/sqrt(N) are common options.
Good luck!
这篇关于在Python中计算FFT手抖动信号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!