fft功率谱困难

时间:2017-03-08 21:45:07

标签: python time-series signals fft

我无法从傅里叶变换中获取频谱...我有一些数据: data

我以中心为中心,似乎没有太多的趋势...

我绘制了它的傅立叶变换:

fourier transform

我得到的东西不好......

这是我的代码:

def fourier_spectrum(X, sample_freq=1):
    ps = np.abs(np.fft.fft(X))**2
    freqs = np.fft.fftfreq(X.size, sample_freq)
    idx = np.argsort(freqs)

    plt.plot(freqs[idx], ps[idx])

改编自here

的代码

它似乎适用于一些天真的sin波数据:

fourier_spectrum(np.sin(2*np.pi*np.linspace(-10,10,400)), 20./400)

sin spectrum

所以我的问题是:我期待一个非零几乎无处不在的频谱,我做错了什么?如果我没有做错什么,我的数据的哪些功能导致了这个?另外,如果我没有做错任何事情,并且由于某种原因fft不适合我的数据,我该怎么做才能从我的数据中提取重要的频率?

1 个答案:

答案 0 :(得分:0)

事实证明,我只是不了解频谱中x轴的单位,即Hz。因为我的样本间距大约为一秒,而我的周期大约是一天,所以在我的频谱上真正可见的唯一单位是~1 / s(在边缘处)到大约1 / m(近中间),任何时间长于此的东西都与0无法区分。我的误解来自于this教程中的图表,他们进行转换以使x轴单位及时,而不是反向时间。我重写了我的frequency_spectrum绘图功能,以进行适当的"缩放"在结果图上......

def fourier_spectrum(X, sample_spacing_in_s=1, min_period_in_s=5):
    '''
        X: is our data
        sample_spacing_in_s: is the time spacing between samples
        min_period_in_s: is the minimum period we want to show up in our
            graph... this is handy because if our sample spacing is
            small compared to the periods in our data, then our spikes
            will all cluster near 0 (the infinite period) and we can't
            see them.  E.g. if you want to see periods on the order of
            days, set min_period_in_s=5*60*60 #5 hours
    '''
    ps = np.abs(np.fft.fft(X))**2
    freqs = np.fft.fftfreq(X.size, sample_spacing_in_s)
    idx = np.argsort(freqs)
    plt.plot(freqs[idx], ps[idx])
    plt.xlim(-1./min_period_in_s,1./min_period_in_s) # the x-axis is in Hz
相关问题