我正在为fft实时编写一个python脚本。该代码使用arduino作为adc转换器并通过串行端口读取值。在while循环的每一步,我加载数据数组,然后我进行傅里叶变换。问题是,在第一个周期后,只需0.01秒即可完成阵列,其他周期需要更长的时间,即0.98秒。 提前谢谢
import time
import math
import serial
from scipy.fftpack import fft
import matplotlib.pyplot as plt
from scipy.signal import blackman
import numpy as np
t=0
maxLenght=256
stringa=np.zeros(shape=(maxLenght))
seriale=serial.Serial('/dev/ttyACM0',57600, timeout=0.1)
time.sleep(3)
cycle=0
while cycle<100:
print cycle
cycle+=1
#seriale.flushInput()
t = time.time()
for i in range(0,maxLenght):
try:
stringa[i]=seriale.readline()
except:
()
elapsed = time.time() - t
print elapsed
#dati=float(dati)
#print dati
#print stringa
s = time.time()
T=0.005
xf = np.linspace(0.0, 1.0/(2.0*T), maxLenght/2)
w = blackman(maxLenght)
ywf = fft(stringa*w)
elapseds = time.time() - s
print elapseds
time.sleep(0.5)
stringa=np.zeros(shape=(maxLenght))
seriale.close()