4

I am new at Python language and coding.

I am trying to acquire and differentiate a live signal from a Arduino UNO Board using the USB Serial. So far, I am acquiring the data with no problems, but I cant get information about how to differentiate it.

Would you guys help me on that or point me out where I can get some information on this stuff.

I would really appreciate your help.

Here is my code

Obs.: I am begginer :D

# -*- coding: utf-8 -*-

from collections import deque
import serial
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
arduinoData = serial.Serial('COM4', 4800)

win = pg.GraphicsWindow()
win.setWindowTitle('pyqtgraph example: Scrolling Plots')

#    In these examples, the array size is fixed.
p1 = win.addPlot()
p2 = win.addPlot()

data1= [0,0]
vector=deque()

for i in range(300):

    string = arduinoData.readline()
    stringx = string.split(',')

    time=float(stringx[0])
    distance=float(stringx[1])
    vector=(time, distance) 
    vectorx = np.array(vector)
    data1=np.vstack((data1,vectorx))   

curve1 = p1.plot(data1)
curve2 = p2.plot(data1)
ptr1 = 0



def update1():
    global data1, curve1, ptr1

    data1[:-1] = data1[1:]  

    string = arduinoData.readline()

    stringx = string.split(',')
    time=float(stringx[0])
    distance=float(stringx[1])
    vector=(time, distance)
    vectorx=np.array(vector)
    data1[-1]=vectorx
    #print(data1)

    curve1.setData(data1)

    ptr1 += 1
    curve2.setData(data1)
    curve2.setPos(ptr1, 0)

# update all plots
def update():
    update1()

timer = pg.QtCore.QTimer()
timer.timeout.connect(update)
timer.start(50)



## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()
Hugo Oliveira
  • 197
  • 2
  • 3
  • 8
  • It is a serious question. You did not explain what you want to differentiate. All data you get from USB is *live data*, isn't it? – zvone Oct 29 '16 at 20:33
  • Ok, I apologize. I want to differentiate the "distance" signal with respect the "time" signal. Would you point me out a place where I can learn it or give me any clue? Thanks. – Hugo Oliveira Oct 29 '16 at 20:45
  • 1
    Assuming that you control both what is sent and what is received, I suggest tagging the data. It can be a simple `time: something` vs. `distance: something`, or something more optimized, e.g. a single byte for a tag (`T17`, `D97`). – zvone Oct 29 '16 at 20:51

2 Answers2

2

"To differentiate a signal" is an expression that is seldom used in English (although it seems to be correct according to Google). That's why you and @zvone had a misunderstanding. It's probably better to say that you want to "take the derivative" of the signal.

Anyway, the numpy.gradient function can do this.

titusjan
  • 5,376
  • 2
  • 24
  • 43
  • Thank you very much titusjan and @zvone. I apologize for the misunderstood. – Hugo Oliveira Oct 30 '16 at 18:58
  • 2
    I disagree. Differentiating is a commonly used term starting at calculus. A differential equations class is a very common requirement for a run of the mill engineering degree. – Brian Oct 30 '16 at 21:31
2

I found a nice show and tell by searching "how to differentiate a waveform in python"

https://plot.ly/python/numerical-differentiation/

import numpy as np
import matplotlib.pyplot as plt

fig = plt.figure()
img = fig.add_subplot(1, 1, 1)

x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)

dy = np.zeros(y.shape,np.float)
dy[0:-1] = np.diff(y)/np.diff(x)
dy[-1] = (y[-1] - y[-2])/(x[-1] - x[-2])

img.plot(x,y, label='$f(x) = sin(x)$')
img.plot(x,dy, label='$f\'(x) = cos(x)$')

img.legend()
plt.show()
FunnyHarry
  • 31
  • 6