我正在尝试制作一个软件,这样我就可以用来分析文件中的数据。我没有关于数据采集的采样率的信息。有没有办法找到我的信号频率,因为我可以发挥周围的采样率我的信号?我试着写一个代码来寻找频率为100hz的正弦波的FFT,但是当我绘制信号的FFT时,信号的频率与FFT轴上的频率不匹配。以下是完整的代码供您参考。你知道吗
from scipy import *
import numpy as np
import matplotlib.pyplot as plt
from Tkinter import *
import tkFileDialog
class FFTGUI(object):
def __init__(self, master):
self.master = master
self.layout_init(self.master)
def layout_init(self, master):
self.master = master
self.title_label = Label(self.master, text="SIGNAL ANALYSER", font="-weight bold")
self.title_label.grid(row=0, column=0, columnspan=2)
self.load_data_file_label = Label(self.master, text="LOAD DATA: ")
self.load_data_file_label.grid(row=1, column=0)
self.load_data_file_button = Button(self.master, text="LOAD",command = self.askopenfilename)
self.load_data_file_button.grid(row=1, column=1)
self.no_sample_label = Label(self.master, text="ENTER THE NUMBER OF SAMPLE POINTS: ")
self.no_sample_label.grid(row=2, column=0)
self.no_sample_entry = Entry(self.master, text="Enter Sample")
self.no_sample_entry.grid(row=2, column=1)
self.sample_spacing_label = Label(self.master, text="ENTER THE SAMPLING SPACE (s):")
self.sample_spacing_label.grid(row=3, column=0)
self.sample_spacing_entry = Entry(self.master, text="ENTER SPACE")
self.sample_spacing_entry.grid(row=3, column=1)
self.plot_graph_button = Button(self.master, text="PLOT", command = self.fft_data_plot)
self.plot_graph_button.grid(row=4, columnspan =2)
def askopenfilename(self):
# get filename
self.filename = tkFileDialog.askopenfilename()
# open file on your own
#~ print self.filename
if self.filename:
return open(self.filename, 'r')
def selectfile(self):
self.file = tkFileDialog.askopenfilename(parent=root,mode='rb',title='Choose a file')
if self.file != None:
self.f= np.loadtxt(file, delimiter='\t', skiprows=0, unpack=True)
#~ print type(f)
return f
def fft_data_plot(self):
N = int(self.no_sample_entry.get())
SAMPLE_SPACE_T = int(self.sample_spacing_entry.get())
T = 1.0/SAMPLE_SPACE_T
lines = [line.rstrip('\n') for line in open(self.filename)]
lines = [float(i) for i in lines]
#~ print lines
yf = fft(lines)
yf_power = np.abs(yf)**2
energy_yf = 0
for i in range(N/2):
energy_yf += yf_power[i]
#~ print energy_yf
energy_nor_yf = []
for i in range(N/2):
temp_energy_nor_yf = (yf_power[i]/energy_yf)*100
energy_nor_yf.append(temp_energy_nor_yf)
#~ print energy_nor_yf
plt.subplot(3,1,1)
plt.plot(lines)
plt.subplot(3,1,2)
plt.plot(np.abs(yf))
plt.ylim([0,100])
plt.xlim([0,N/2])
plt.subplot(3,1,3)
plt.plot(energy_nor_yf)
plt.ylim([0, max(energy_nor_yf)])
plt.show()
root = Tk()
FFTGUI(root)
root.mainloop()
我试图加载和分析的文件可以在这里找到:http://www.filedropper.com/cosine100hz 这个文件是我在excel上生成的一个简单的余弦波,余弦波的结果数据被添加到一个文本文件中,以便可以绘制和分析。不幸的是,我似乎不知道问题是什么,为什么我没有得到理想的频率。我会感激你的帮助。谢谢
我所知道的从纯样本中获取频率的唯一方法是知道信号中存在的某个频率(例如EEG数据中的电网频率伪影)。你知道吗
[编辑]
因为信号中有一个已知的频率,所以可以找到采样率(或者更确切地说是采样间隔)。如果您使用
numpy.fft.fft()
(我猜scipy.fft()
方法是它的别名),您可以使用numpy.fft.fftfreq()
获得各个组件的频率。你知道吗numpy.fft.fftfreq(n, d=1.)
的第一个参数是采样数,而第二个(d
)是采样间隔(1/f)。你需要找到一个合适的d
。你知道吗相关问题 更多 >
编程相关推荐