有人可以帮助我更快地制作Python代码吗?目前我每秒达到约11次预测。我希望得到这么快,但不知道该怎么做。
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import time
from pylab import *
###################################################################
import Accelneu as Accel
import RPi.GPIO as GPIO
from scipy.fftpack import fft
GPIO.setmode(GPIO.BCM)
HIGH=True
LOW=False
###Variablendefinitionen:
##ADDA Wandler:
#Pins:
SCLK = 18 #Taktuhr
MOSI = 24 #Master-Out-Slave-In
MISO = 23 #Master-In-Slave-Out
CS1 = 25 #Chip-Select 1 (A/D)
CS2 = 12 #Chip-Select 2 (D/A)
CS3 = 4 #Chip-Select 2 (Acceleration)
#sonst Infos:
numBit1 = 12 # IN Anzahl Bits IC 1 (A/D)
numBit2 = 10 # OUT Anzahl Bits IC 2 (D/A) #Variablendefinition
VoltageMaxIn = 5.000
VoltageMaxOut = 4.0955 #Maximalspannung
Voltage = 2.500
##Drucksensor:
VoltageOffset = 0.014652014652014652 #V Spannungsoffset Typ:20mV
PressureSensitivity = 0.0035 #V/kPa Empfindlichkeit
#AccelerationSensitivity = 0.015625
##GPIOs
GPIO.setup(SCLK, GPIO.OUT)
GPIO.setup(MOSI, GPIO.OUT)
GPIO.setup(MISO, GPIO.IN)
GPIO.setup(CS1, GPIO.OUT)
GPIO.setup(CS2, GPIO.OUT)
GPIO.setup(CS3, GPIO.OUT)
#######################
xlimit=500
fig=figure(figsize=(8,7),dpi=100)
x=[i+1 for i in range (xlimit)]
y=[0 for i in range (xlimit)]
ax1=fig.add_subplot(211)
ax2=fig.add_subplot(212)
line1, =ax1.plot(x,y,'r-')
line2, =ax2.plot(x,y,'g-')
ax1.set_ylim([-0.5, 2.5])
ax2.set_ylim([0, 0.4])
############################################################
#FFT:
N=xlimit #512#1000 #Number of samplepoints/sampling rate
Spacing=2048 #1024
T=1.0/Spacing #Sample Spacing/sampling interval
xf=np.linspace(0.0, 1.0/(2.0*T), N/2)
line2.set_xdata(xf[1:N/2-1])
############################################################
def animate(i):
t0=time.time()
y[1:]=y[:-1] ##Zeitraum 1
t1=time.time()
y[0],z=Accel.readAxisAcceleration(1) ##Zeitraum 2
t2=time.time()
line1.set_ydata(y) ##Zeitraum 3
t3=time.time()
ffty=fft(y) ##Zeitraum 4
t4=time.time()
ffty=ffty[1:N/2-1] ##Zeitraum 5
t5=time.time()
line2.set_ydata(2.0/N*np.abs(ffty[0:N/2]))#ffty) ##Zeitraum 6
t6=time.time()
print(t1-t0,t2-t1,t3-t2,t4-t3,t5-t4,t6-t5)
return line1, line2 #lines
t7=time.time()
ani =animation.FuncAnimation(fig,animate,xrange(1,200),interval=0,blit=True)
t8=time.time()
print (t8-t7)
plt.show()
也许任何人都可以告诉我如何加快速度。 Accelneu类是一个从加速度传感器读出数据的函数(不是慢行为的来源,我对此进行了分析)。