基8FFT算法的python代码实现
时间: 2023-07-25 09:24:11 浏览: 105
以下是基于8的FFT算法的Python代码实现:
```
import math
def twiddle_factor(N):
W = []
for i in range(N):
w = complex(math.cos(2*math.pi*i/N), -math.sin(2*math.pi*i/N))
W.append(w)
return W
def fft_8(x):
N = len(x)
if N == 1:
return x
else:
x0 = x[::8]
x1 = x[1::8]
x2 = x[2::8]
x3 = x[3::8]
x4 = x[4::8]
x5 = x[5::8]
x6 = x[6::8]
x7 = x[7::8]
y0 = fft_8(x0)
y1 = fft_8(x1)
y2 = fft_8(x2)
y3 = fft_8(x3)
y4 = fft_8(x4)
y5 = fft_8(x5)
y6 = fft_8(x6)
y7 = fft_8(x7)
W = twiddle_factor(N)
y = [0]*N
for i in range(N//8):
j = 8*i
y[j] = y0[i] + y1[i]*W[j] + y2[i]*W[2*j] + y3[i]*W[3*j] + y4[i]*W[4*j] + y5[i]*W[5*j] + y6[i]*W[6*j] + y7[i]*W[7*j]
y[j+1] = y0[i] + y1[i]*W[j+1] + y2[i]*W[2*(j+1)] + y3[i]*W[3*(j+1)] + y4[i]*W[4*(j+1)] + y5[i]*W[5*(j+1)] + y6[i]*W[6*(j+1)] + y7[i]*W[7*(j+1)]
y[j+2] = y0[i] + y1[i]*W[j+2] + y2[i]*W[2*(j+2)] + y3[i]*W[3*(j+2)] + y4[i]*W[4*(j+2)] + y5[i]*W[5*(j+2)] + y6[i]*W[6*(j+2)] + y7[i]*W[7*(j+2)]
y[j+3] = y0[i] + y1[i]*W[j+3] + y2[i]*W[2*(j+3)] + y3[i]*W[3*(j+3)] + y4[i]*W[4*(j+3)] + y5[i]*W[5*(j+3)] + y6[i]*W[6*(j+3)] + y7[i]*W[7*(j+3)]
y[j+4] = y0[i] + y1[i]*W[j+4] + y2[i]*W[2*(j+4)] + y3[i]*W[3*(j+4)] + y4[i]*W[4*(j+4)] + y5[i]*W[5*(j+4)] + y6[i]*W[6*(j+4)] + y7[i]*W[7*(j+4)]
y[j+5] = y0[i] + y1[i]*W[j+5] + y2[i]*W[2*(j+5)] + y3[i]*W[3*(j+5)] + y4[i]*W[4*(j+5)] + y5[i]*W[5*(j+5)] + y6[i]*W[6*(j+5)] + y7[i]*W[7*(j+5)]
y[j+6] = y0[i] + y1[i]*W[j+6] + y2[i]*W[2*(j+6)] + y3[i]*W[3*(j+6)] + y4[i]*W[4*(j+6)] + y5[i]*W[5*(j+6)] + y6[i]*W[6*(j+6)] + y7[i]*W[7*(j+6)]
y[j+7] = y0[i] + y1[i]*W[j+7] + y2[i]*W[2*(j+7)] + y3[i]*W[3*(j+7)] + y4[i]*W[4*(j+7)] + y5[i]*W[5*(j+7)] + y6[i]*W[6*(j+7)] + y7[i]*W[7*(j+7)]
return y
```
这段代码实现了基于8的FFT算法,输入的是长度为8的复数序列。其中 twiddle_factor 函数生成旋转因子,fft_8 函数对输入进行递归计算并输出长度为8的DFT结果。
阅读全文