如何对 Python 中的信号求导?
How to take the derivative of a Signal in Python?
我是 Python 语言和编码方面的新手。
我正在尝试使用 USB 串口获取和区分来自 Arduino UNO 板的实时信号。到目前为止,我正在毫无问题地获取数据,但我无法获得有关如何区分它的信息。
你们能帮我解决这个问题吗,或者告诉我在哪里可以获得有关这些东西的一些信息。
非常感谢您的帮助。
这是我的代码
Obs.: 我是初学者 :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_()
"To differentiate a signal"是英语中很少使用的表达方式(虽然根据Google似乎是正确的)。这就是你和@zvone 产生误会的原因。最好说你想要 "take the derivative" 的信号。
反正numpy.gradient函数可以做到这一点
我找到了一个不错的节目,通过搜索告诉我"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()
我是 Python 语言和编码方面的新手。
我正在尝试使用 USB 串口获取和区分来自 Arduino UNO 板的实时信号。到目前为止,我正在毫无问题地获取数据,但我无法获得有关如何区分它的信息。
你们能帮我解决这个问题吗,或者告诉我在哪里可以获得有关这些东西的一些信息。
非常感谢您的帮助。
这是我的代码
Obs.: 我是初学者 :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_()
"To differentiate a signal"是英语中很少使用的表达方式(虽然根据Google似乎是正确的)。这就是你和@zvone 产生误会的原因。最好说你想要 "take the derivative" 的信号。
反正numpy.gradient函数可以做到这一点
我找到了一个不错的节目,通过搜索告诉我"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()