简体   繁体   English

将FFT函数从Python 2.x转换为Python 3.x,并从中计算IFFT

[英]Translating an FFT function from Python 2.x to Python 3.x, and computing the IFFT from it

I have an Fast Fourier Transform function in Python for versions 2.x. 对于版本2.x,我在Python中具有快速傅立叶变换功能。 I want to make it in Python 3.x, but I have some problems with "xrange" and list identifiers(as my compiler said). 我想在Python 3.x中创建它,但是“ xrange”和列表标识符(如我的编译器所说)存在一些问题。 I also have no idea how to compute Inversed FFT from my FFT without using of any non-standard libraries. 我也不知道如何在不使用任何非标准库的情况下从FFT计算逆FFT。 Code is below. 代码如下。 Thanks in advance... 提前致谢...

 from cmath import exp,pi

 def FFT(X):
  n = len(X)
  w = exp(-2*pi*1j/n)
  if n > 1:
    X = FFT(X[::2]) + FFT(X[1::2])
    for k in xrange(n/2):
        xk = X[k]
        X[k] = xk + w**k*X[k+n/2]
        X[k+n/2] = xk - w**k*X[k+n/2]
 return X 

UPD: Totally reconstructed ,my FFT and constructed IFFT due to your advices. UPD:根据您的建议,完全重建了我的FFT,并构造了IFFT。 PS How to close post? PS如何关闭职位?

There are a couple ways to convert your FFT into an IFFT. 有几种方法可以将FFT转换为IFFT。 The easiest is to get rid of the minus sign inside the parameter to your exp() function for w. 最简单的方法是摆脱w的exp()函数参数内的减号。 The next is to take the complex conjugate of the FFT of the complex conjugate of the input. 接下来是对输入的复共轭进行FFT的复共轭。

If you don't scale your forward FFT, then common practice is to scale your IFFT computation by 1/N (the length), so that IFFT(FFT()) results in the same total sum magnitude. 如果不缩放正向FFT,则通常的做法是将IFFT计算缩放1 / N(长度),以使IFFT(FFT())得出相同的总和幅度。 If you do scale your FFT by 1/N, then don't scale your IFFT computation. 如果确实将FFT缩放1 / N,则不要缩放IFFT计算。 Or scale both by 1/sqrt(N). 或按1 / sqrt(N)缩放比例。

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

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