我正在試圖製作一個軟件,以便我可以使用它來分析我在文件中的數據。我沒有關於捕獲數據的採樣率的信息。有沒有辦法找到我的信號的頻率,因爲我可以利用我的信號的採樣率?我嘗試編寫代碼來查找頻率爲100 Hz的正弦波的FFT,但是當繪製我的信號的fft時,信號的頻率與fft軸上的頻率不匹配。以下是供您參考的完整代碼。ftthon中未知頻率的生物信號的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和餘弦波產生的數據生成一個文本文件,以便添加了一個簡單的餘弦波它可以被繪製和分析。不幸的是,我似乎不知道問題是什麼,爲什麼我沒有獲得所需的頻率。我很感謝你的幫助。謝謝
我看不到你的代碼繪製功率*與頻率*的部分。你正在繪製它與組件編號的關係,不是嗎? – abukaj
嗨abukaj,是的,我正在繪製組件編號。 – Usama
您是如何確定哪個組件是100 Hz的? – abukaj