一个简单的python脚本中的Cooley Tukey旋转因子

2024-09-30 20:17:36 发布

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

我正在阅读cooley tukey method works,但我对以下python脚本有一些问题:

def fft_CT_twiddles(x, inverse = False, verbose = False, twiddles = None) :
    """
    Computes the DFT of x using Cooley-Tukey's FFT algorithm. Twiddle factors
    are precalculated in the first function call, then passed down recursively.
    """
    t = time.clock()
    N = len(x)
    inv = -1 if not inverse else 1
    if N % 2 :
        return dft(x, inverse, False)
    M = N // 2
    if not twiddles :
        twiddles = [math.e**(inv*2j*math.pi*k/N) for k in xrange(M)]+[N]
    x_e = x[::2]
    x_o  = x[1::2]
    X_e = fft_CT_twiddles(x_e, inverse, False, twiddles)
    X_o  = fft_CT_twiddles(x_o, inverse, False, twiddles)
    X = []
    for k in range(M) :
        X += [X_e[k] + X_o[k] * twiddles[k * twiddles[-1] // N]]
    for k in range(M,N) :
        X += [X_e[k-M] - X_o[k-M] * twiddles[(k - M) * twiddles[-1] // N]]
    if inverse :
        X = [j/2 for j in X]
    t = time.clock() - t
    if verbose :
        print "Computed","an inverse" if inverse else "a","CT FFT of size",N,
        print "in",t,"sec."
    return X

是什么 twiddles=[math.e**(投资*2j*数学.pi*k/N)对于x范围内的k(M)]+[N] 行吗?看起来像一个数组,但为什么是+[N]?在

那么为什么要访问twidles[-1]值呢?在

我想不通


Tags: oftheinfftfalseforverboseif
2条回答

当提问者的专业水平未知时,试图解释代码是很困难的。上面说的是:

  1. python有一个用于序列nl的连接运算符。+所以twiddles =行创建了某种排序的序列,并将数字N附加到它后面。在
  2. twiddles[-1]访问序列中的最后一个元素,如注释所示,N。在
  3. twiddles序列表达式使用复数生成由单位圆上的N点组成的序列,方法是将其分成N等分片。在

您询问的代码正在执行以下操作:

+[N] # appends N to the end of the array


twiddles[-1] # is the last element of the array ( the N in this case ).

代码似乎只是为了方便起见,将“N”添加到twidles数组的末尾,以便以后使用,并且可以轻松地将其作为twidles参数的一部分传递给函数,而不是将其作为单独的变量传递。在

相关问题 更多 >