2

Can anyone help me to make my Python code faster? At the moment I reach about 11 meassurements per second. I hope to get this much faster but don't know how to do it.

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()

Maybe anyone can tell me how to make this faster. The class Accelneu is a function that reads out data from a acceleration sensor (not the source of the slow behaviour, I profiled this).

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
manuel
  • 139
  • 1
  • 1
  • 9
  • 3
    You mention you profiled your code. Please share the results. You might also consider switching to fftw. – ThatOneDude Jul 16 '15 at 20:41
  • 2
    You can use `rfft` instead of `fft` since your signal is real-valued – gg349 Jul 16 '15 at 20:52
  • Are you doing all the time.time() calls just for testing? If so, try disabling those and see how much faster it runs, getting the time can be slow, depending on the method. Also, instead of print(), you could write() it to a buffered file instead. – fileoffset Jul 17 '15 at 06:39
  • 3
    I think [Code Review](http://codereview.stackexchange.com/) would be a better place for your question. – jrenk Jul 17 '15 at 12:41
  • So the reason you show all this completely unrelated code instead of just the necessary parts is..? Heck you're doing the io on the same thread as the computations, that does seem like a likely candidate. – Voo Jul 17 '15 at 12:42
  • @jrenk It could be, but it would need some improvement before it survives there. – Mast Jul 17 '15 at 12:46
  • @Mast but it's definitely wrong here. – jrenk Jul 17 '15 at 12:47

0 Answers0