【问题标题】:FFT : not the same boundaries as the exampleFFT:与示例的边界不同
【发布时间】:2018-05-02 21:35:58
【问题描述】:

我必须重新制作示例的 FFT 以进行训练,但我遇到了一个问题:当我绘制 FFT 时,我成功地获得了相同的形状,但边界与示例不同。

这是我必须绘制并进行 FFT 的函数:

该函数的截止频率预计为 2 Hz(0.5 秒)。
(如果您不理解:这意味着在 0.5 秒(FFT 为 2 Hz),函数等于 0)

左边是与时间相关的函数,右边是傅里叶变换。
这就是例子。 简单的红线,是我正在开发的功能

这是我得到的:

如您所见,我的 FFT 图的边界与示例非常不同。 我在情节中有 10² 的系数

您认为问题出在我使用 FFT 的方式上吗? 都说函数是归一化的,你觉得区别是从这里来的吗?

这是我写的代码:

npas      = 32768                                # steps for discretization
t         = np.linspace(0,200,npas)              # time discretized (array of npas elements, from 0 to 200)     
f         = np.fft.fftfreq(t.size, d=1.0/npas)   # frequency (for Fourier Transform)  d = sample spacing (inverse of the sampling rate)
swh       = np.zeros(npas)                       # initialization of the array swh (the function we use)
tau0      = 1.0/20

# creation of the function and it's Fourier transform
swh      = (1/tau0)*np.exp(-t/tau0)*(1 + t/tau0 + (t**2)/(2*tau0**2) + (t**3)/(3*tau0**3) - 0.490*t**4/tau0**4) - np.exp(-t/tau0)*(1/tau0 + t/tau0**2 + t**2/tau0**3 - 0.490*(4*t**3)/tau0**4)
swh_f    = np.fft.fft(swh)                       # Fourier transform of swh

##### Plot #####

plt.figure(1)
plt.subplot(1,2,1)
plt.title("Time Function")
plt.plot(t, swh, 'r'  ,  linewidth=1, label = "SWH B = 0.490")             # plot of swh, function of t
plt.legend(loc = 1, prop={'size': 7})                                      # legend position
plt.xlim(0,1.5)

plt.subplot(1,2,2)
plt.title("Frequency Function")
plt.plot(f[0:f.size//2], abs(swh_f[0:npas//2]), 'r'  ,  linewidth=1)       # plot of the Fourier transform, function of f (frequency)
plt.xscale('log')
plt.yscale('log')

plt.show()

感谢您的关注!

【问题讨论】:

    标签: python matplotlib fft


    【解决方案1】:

    您可能希望在 FFT 中使用“规范”选项:

    swh_f = np.fft.fft(swh, norm = "ortho")
    

    您可以在此处找到有关 NumPy 实现 FFT 的更多信息:https://docs.scipy.org/doc/numpy/reference/routines.fft.html#module-numpy.fft

    【讨论】:

    • 感谢您的回答。我收到了这个错误信息:fft() got an unexpected keyword argument 'norm'
    • 是我进口的吗?我像这样导入函数 fft: from scipy.fftpack import fft import matplotlib.pyplot as plt import numpy as np
    • 您可能拥有过时的 NumPy 版本。将 swh_f 除以 np.sum(swh) 可以得到相同的结果。
    • 谢谢,解决了一半的问题。我有错误的横坐标,但正确的纵坐标。
    • 你弄错了采样率。您的信号由 200 秒的“npas”点定义。因此采样率为“npas/200”。只需将频率轴更改为:f = np.fft.fftfreq(t.size, d=200/npas)
    猜你喜欢
    • 1970-01-01
    • 2017-11-09
    • 2014-10-14
    • 2019-04-30
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 2022-01-07
    相关资源
    最近更新 更多