读取realtim的串行数据

2024-10-01 22:33:24 发布

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

我正在为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()

Tags: fromimportffttimeasnpserialscipy

热门问题