我的代码让用户引入将用傅立叶转换的数字数组。但我想从文件中读取这个数组。我怎样才能做到这一点?我刚开始学习python..
import cmath
from scipy.fftpack import fft
f=open("complex.txt","r+")
c=[]
for line in f:
line=line.split()
if line:
line=[complex(i.replace('i','j')) for i in line]
c.append(line)
def omega(p, q):
return cmath.exp((2.0 * cmath.pi * 1j * q) / p)
def fft(signal):
n = len(signal)
if n == 1:
return signal
else:
Feven = fft([signal[i] for i in xrange(0, n, 2)])
Fodd = fft([signal[i] for i in xrange(1, n, 2)])
combined = [0] * n
for m in xrange(n/2):
combined[m] = Feven[m] + omega(n, -m) * Fodd[m]
combined[m + n/2] = Feven[m] - omega(n, -m) * Fodd[m]
return combined