简体   繁体   English

Python 中的快速傅里叶变换性能

[英]Fast Fourier Transform performance in Python

So I wrote a short Python program to estimate the accuracy of the Python's FFT method.所以我写了一个简短的 Python 程序来估计 Python 的 FFT 方法的准确性。

import numpy as np
import matplotlib.pyplot as plt

#Aufgabe 1
x0=0
a=2.5
k0=3
X=np.linspace(-4,4,100)
timestep=0.1
k=np.fft.fftfreq(X.size,d=timestep)
psi_analytical=[(2/(np.pi*a**2))**(1/4)*np.exp(-((i-x0)**2)/a**2)*np.exp(1j*k0*(i-x0)) for i in X]
psi_tilde_numerical=np.fft.fft(psi_analytical)
psi_tilde_analytical=[(2/(np.pi*a**2))**(1/4)*(a/2)*np.exp(-(a*(i-k0))**2/4)*np.exp(-1j*i*x0) for i in k]
psi_numerical=np.fft.ifft(psi_tilde_analytical)


#plt.plot(k,np.abs(psi_tilde_numerical),label='numerical psi tilde')
#plt.plot(k,np.abs(psi_tilde_analytical),'--',color='tab:orange', label='analytical psi tilde')

plt.plot(X,np.abs(psi_analytical),label='analytical psi, real')
plt.plot(X,np.abs(psi_numerical),'--',color='tab:orange',label='numerical psi, real')
plt.legend()
plt.show()

The analytical function is as follows:解析function如下:

分析功能

To my surprise, the numerical and analytical functions are totally different.令我惊讶的是,数值函数和分析函数完全不同。 However, I'm not sure why this is the case.但是,我不确定为什么会这样。

The normalisation constant N is (2/(np.pi*a**2))**(1/4)归一化常数N(2/(np.pi*a**2))**(1/4)

Doing a bit more research, I think I might have an answer for you.做更多的研究,我想我可能会给你一个答案。

Discrete Fourier Transform ( DFT ), which is computed efficiently using the Fast Fourier Transform algorithm ( FFT ), operates on discrete time domain signals.使用快速傅里叶变换算法 ( FFT ) 高效计算的离散傅里叶变换 ( DFT ) 对离散时域信号进行运算。 The Fourier Transform ( FT ) operates on function in continuous time domain.傅里叶变换 ( FT ) 在连续时域中对 function 进行操作。

DFT will approximate the FT under certain condition. DFT在一定条件下会逼近FT One of those conditions is that the signal has to be band limited .这些条件之一是信号必须是频带限制的。 This means that the FT for the function has to be zero for all frequencies above a certain frequency threshold α and the DFT has to have a sample rate that is at least 2*α This goes back to Nyquist-Shannon sampling theorem .这意味着对于高于某个频率阈值α的所有频率,function 的FT必须为零,并且 DFT 的采样率必须至少为2*α这可以追溯到Nyquist-Shannon 采样定理

In your case, you are trying to approximate a Gaussian function exp(-x²) which is not band limited.在您的情况下,您正在尝试逼近不受频带限制的高斯 function exp(-x²) This is because as you can see from your formulas, FT of a Gaussian is also a Gaussian.这是因为从公式中可以看出,高斯的FT也是高斯的。 This means that it has negligible but non-zero components for frequencies all the way to infinity.这意味着它在一直到无穷大的频率中具有可忽略不计但非零的分量。 As a result, you won't be able to approximate the FT using a DFT since you would need to have infinite sampling rate.因此,您将无法使用DFT来近似FT ,因为您需要具有无限的采样率。

In conclusion, its important to realize that the DFT and FT are vastly different transforms and thus can not just be compared.总之,重要的是要认识到DFTFT是截然不同的变换,因此不能仅仅进行比较。

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

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