fft功率谱的困境

2024-03-28 19:12:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我很难从傅里叶变换中得到频谱。。。我有一些数据: 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的代码。在

它似乎适用于一些原始的正弦波数据:

^{pr2}$

sin spectrum

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


Tags: 数据sample代码fftdefnpabs趋势
1条回答
网友
1楼 · 发布于 2024-03-28 19:12:25

结果我不明白频谱中x轴的单位,也就是赫兹。因为我的采样间隔大约是一秒,而周期是一天,所以在我的频谱上真正可见的单位只有~1/s(在边缘)到~1/m(靠近中间),而且任何比这更长的周期都无法与0区分。我的误解源于this图形教程,在那里他们进行转换,以便x轴单位是在时间上,而不是反时间。我重写了我的频谱绘制功能,在结果图上做适当的“缩放”。。。在

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

相关问题 更多 >