Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to take the derivative of a Signal in Python?

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_()
like image 949
Hugo Oliveira Avatar asked Sep 14 '25 06:09

Hugo Oliveira


1 Answers

"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.

like image 89
titusjan Avatar answered Sep 16 '25 19:09

titusjan