我是 python 新手,我正在尝试 FFT 的 python 实现。我已经收到上述错误一段时间了。我该怎么办?
import numpy
import cmath
def twiddle(r,s):
x = (cmath.exp((2*cmath.pi*1j*s)/r))
return x
def fft(signal):
n = len(signal)
if n==1:
return signal
else:
Feven=fft([signal[k] for k in range(0,n,2)])
Fodd=fft([signal[k] for k in range(1,n,2)])
for l in range (n/2):
F1 = Feven[l] + twiddle(n, -l) * Fodd[l]
F2 = Feven[l] - twiddle(n, -l) * Fodd[l]
return F1+F2
当我为 Feven 和 Fodd 添加打印语句并输入以下内容时:
print (fft([4.5, 3.4, 4.7, 3.8, 6.7, 8.0, 4.6, 7.8]))
我得到:
Traceback (most recent call last):
File "FFT.py", line 41, in <module>
print (fft([4.5, 3.4, 4.7, 3.8, 6.7, 8.0, 4.6, 7.8]))
File "FFT.py", line 29, in fft
Feven=fft([signal[k] for k in range(0,n,2)])
File "FFT.py", line 34, in fft
F1 = Feven[l] + twiddler(n, -l) * Fodd[l]
TypeError: 'complex' object has no attribute '__getitem__'