首页 > 解决方案 > 使用来自 scipi 的 fft 来缩放 X 的适当方法是什么?

问题描述

我整天都在玩这个代码,我似乎无法理解我的 X 轴发生了什么。大部分代码来自这里

这是我第一次尝试在现实生活中实际使用fft,如果我把一切都搞砸了,我很抱歉!

代码至少可以运行 90%。我能够区分稳定信号和振荡信号。

已知的好: 已知良好,无明显峰

已知不良: 已知的坏,明显的峰值

对我来说,问题是我觉得我的 X 比例是任意的。如,我知道如何设置它,但对于我的生活,我无法弄清楚为什么“正确”的结果应该是它。

编码:

import numpy as np
import matplotlib.pyplot as plt
import scipy.fftpack
import xlrd

start = 5 #start number in xl
end = 593 #end number from xl

fig = plt.figure(figsize=[14,4])
N = (end-start)           # Number of samplepoints
Fs = 10*N        #I believe this is where my troubles are coming from, i don't know what it's supposed to be!
T = 1.0 / 3.175*np.pi      # N_samps*T (#samples x sample period) is the sample spacing.
N_fft = 250 # Number of bins (chooses granularity)
x = np.linspace(0, N*T, N)     # the interval
#y = np.sin(50.0 * 2.0*np.pi*x) + 0.5*np.sin(80.0 * 2.0*np.pi*x)   # the test signal
workbook = xlrd.open_workbook('MY_FILE_LOCATION') #put actual file loc 
worksheet = workbook.sheet_by_name('Sheet1')
y = worksheet.col_values(12, start, end)# data loc in xl

#add a known signal to the system
#This is how I got the "correct" answer
# for i in range(len(y)):
#     if i % 10 == 0:
#         y[i] += 1


# removing the mean of the signal
mean_removed = np.ones_like(y)*np.mean(y) 
y = y - mean_removed

# Compute the fft.
yf = scipy.fftpack.rfft(y,n=N_fft)
xf = np.arange(0,Fs,Fs/N_fft) #not clear what this is supposed to be doing

##### Plot the fft #####
ax = plt.subplot(111)
pt, = ax.plot(xf,np.abs(yf), lw=1.0, c='b')
ax.set_xlim((ax.get_xlim()[0],Fs))
ax.set_title('FFT', fontsize= 16, fontweight="bold")
ax.set_ylabel('Strength')
ax.set_xlabel('Length')
ax.grid()
plt.show()

所以我试图做的是添加一个人工信号来看看会发生什么。这就是我得到的: 定期添加强信号

好,很好。我的信号在那里,但它应该(大约)每 100 个单位出现一次。为了做到这一点,我必须将该Fs变量设置为 ~500。我真的不明白。

500 只是没有出现在我正在做的任何事情中。所以我无法想象它应该来自哪里。

也许有人可以帮助我了解我所缺少的内容,或者只是为我指出正确的方向,以便更好地了解 fft 的实际应用。我觉得我已经看过足够多的例子,人们对已知函数进行采样,执行 fft,然后惊呼:“看,它有效!”

处理脏数据有哪些好方法?(我读过一些关于韦尔奇的东西,我认为这是第一个要看的地方)

尝试获取更多数据是否值得?理论上更平滑的数据(我会假设傅立叶不太适合尖峰)?

标签: pythonpython-3.xnumpyscipyfft

解决方案


推荐阅读