python - 应如何重新调整 fft 点以获得与解析解相同的结果?

标签 python numpy fft

我想使用 numpy fft 包进行快速傅里叶变换,然后我尝试比较解析解和快速傅里叶变换之间的结果,尽管我可以通过我所做的图看到曲线相似,很明显尺度不同。

我尝试了几种不同版本的频率(角频率、频率和波数),但我所有的尝试都不起作用,并且在numpy文档中,并不清楚快速傅里叶变换是如何准确定义的。例如,我想将指数函数及时进行傅里叶变换到角频域, f(t)=Exp(-a|t|), F(w)=a/pi*(a²+w² )(该解析解有多个版本,具体取决于我们考虑的频率空间)


def e(t):
    return np.exp(-0.5*abs(t))
def F(w):
    return 0.5/(np.pi)*(1/(((0.5)**2)+((w)**2)))

t=np.linspace(0,100,1000)

w=np.fft.fftfreq(len(t))
plt.plot(w,F(w),'o',label='F(w)')
plt.legend()
plt.show()

fourier=np.fft.fft(e(t))
plt.plot(w,fourier,'o')
plt.show()

我已经尝试了上述代码的多种不同变体,专门用于频率,但我仍然没有达到 fft 和解析解相似的程度。有人可以帮我吗?

最佳答案

Fourier transform可应用于可积函数,例如 np.exp(-0.5*abs(t))。但是Discrete Fourier Transform计算周期信号的傅里叶变换。请参阅https://dsp.stackexchange.com/questions/26884/about-fourier-transform-of-periodic-signal What FFTW Really Computes .

因此,长度为 T 的帧的 DFT 对应于周期帧的傅里叶变换。由于帧从 0 开始,因此计算周期性右侧指数衰减的傅里叶变换: enter image description here 如您所见,函数np.exp(-0.5*abs(t))的一半未显示。让我们纠正它并添加该函数的周期性增加部分两侧指数衰减。 我使用频率作为参数:

import matplotlib.pyplot as plt
import numpy as np

def e(t):
    return np.exp(-0.5*abs(t))
def F(w):
    return 0.5/(np.pi)*(1/(((0.5)**2)+((w)**2)))

def Fc(xi):
    #ok , that's sourced from https://en.wikipedia.org/wiki/Fourier_transform ... Square-integrable functions, one-dimensional, line 207
    return 2*0.5/(((0.5)**2)+(4*(np.pi*xi)**2))


framelength=100.
nbsample=1000
def ep(t):
    #the periodized negative part is added at the end of the frame.
    return np.maximum(np.exp(-0.5*abs(t)),np.exp(-0.5*abs(t-framelength)))

t=np.linspace(0,framelength,nbsample, endpoint=False)

#plotting the periodized signal, to see what's happening
ein=ep(t)
tp=np.linspace(0,10*framelength,10*nbsample, endpoint=False)
periodized=np.zeros(10*nbsample)
for i in range(10):
    for j in range(nbsample):
       periodized[i*nbsample+j]=ein[j]

plt.plot(tp,periodized,'k-',label='periodized frame')
plt.legend()
plt.show()

fourier=np.fft.fft(ep(t))/np.size(ep(t))*framelength

#comparing the mean is useful to check the overall scaling
print np.mean(ep(t))*framelength
print fourier[0]
print Fc(0)

#the frenquencies of the DFT of a frame of length T are 1/T, 2/T ... and negative for the second part of the array.
xi=np.fft.fftfreq(len(t), framelength/len(t))

# comparison between analytical Fourier transform and dft.
plt.plot(xi,Fc(xi),'o',label='F(xi)')
plt.plot(xi,np.real(fourier),'k-', lw=3, color='red', label='DTF')
plt.legend()
plt.show()

结果如下:

enter image description here

对于实验性非周期信号,当帧被周期化时,会出现人为的不连续性。它导致spectral leakagewindows用于减弱不连续性及其影响。其中一个潜在窗口称为泊松窗口,是两侧指数衰减!

关于python - 应如何重新调整 fft 点以获得与解析解相同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55519419/

相关文章:

python 重新: no such group

python - 附加到 python/pandas 中的系列不起作用

python - Numpy:为什么 (2,1) 数组和垂直矩阵切片的差异不是 (2,1) 数组

python - 如何从 wav 文件中获取振幅和频率列表

python - 如何向 Python REST 请求添加基本身份验证?

python - 由于 OSError : [WinError 5] Access is denied 无法安装软件包

python - 将文本文件导入为 numpy 中的矩阵

python - 列表列表到 numpy 数组中

python - python中离散功率谱密度的正确归一化实际问题

c++ - 如何在 FFTW 库中进行实数到实数的反向 FFT