简体   繁体   English

fft功率谱困难

[英]fft power spectrum woes

I'm having trouble getting a frequency spectrum out of a fourier transform... I have some data: 我无法从傅里叶变换中获取频谱......我有一些数据: 数据

That I have mean-centered, and doesn't seem to have too much of a trend... 我以中心为中心,似乎没有太多的趋势......

I plot the fourier transform of it: 我绘制了它的傅立叶变换:

傅里叶变换

And I get something that is not nice.... 而且我得到的东西并不好......

Here is my code: 这是我的代码:

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])

As adapted from code taken from here . 改编自此处的代码。

It seems to work for some naive sin wave data: 它似乎适用于一些天真的sin波数据:

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

sin谱

So my questions are: I'm expecting a non-zero-almost-everywhere-spectrum, what am I doing wrong? 所以我的问题是:我期待一个非零几乎无处不在的频谱,我做错了什么? If I'm not doing anything wrong, what features of my data are causing this? 如果我没有做错什么,我的数据的哪些功能导致了这个? Also, if I'm not doing anything wrong, and fft is just unsuited for my data for some reason, what should I do to extract important frequencies from my data? 另外,如果我没有做错任何事情,并且由于某种原因fft不适合我的数据,我该怎么做才能从我的数据中提取重要的频率?

It turns out that I just didn't understand the units of the x-axis in the frequency spectrum, which is Hz. 事实证明,我只是不理解频谱中x轴的单位,即Hz。 Because my sample spacings were on the order of a second, and my period was on the order of a day, the only units really visible on my frequency spectrum were ~1/s (at the edges) to about ~1/m (near the middle), and anything with a longer period than that was indistinguishable from 0. My misunderstanding stemmed from the graph on this tutorial, where they do conversions so that the x-axis units are in time, as opposed to inverse time. 因为我的样本间距大约为一秒,而我的周期大约是一天,所以在我的频谱上真正可见的唯一单位是~1 / s(在边缘处)到大约1 / m(近中间的),任何时间长于此的东西都与0无法区分。我的误解源于教程中的图形,它们进行转换以使x轴单位及时,而不是反时间。 I rewrote my frequency_spectrum plotting function to do the appropriate "zooming" on the resulting graph... 我重写了我的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM