实时数字上升和跌落边缘在声音信号上绘制

发布于 2025-01-24 17:47:40 字数 2743 浏览 2 评论 0原文

您好,我需要在同一子图上发声和数字信号。对于第一个和secong的图是声音信号,我可以很快阅读。我需要第三个数字信号才能查看。在这种情况下,当我按键按钮时,我需要一个数字高。当我编写Python Code 3rd Signal(键按)时,图中中间没有更改。我的意思是我看不到情节期间的上升和下降边缘。我该如何解决?

import numpy as np
import pyaudio
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
import time
plt.style.use('bmh')

BUFFER = 1024*4 # number of data points to read at a time
SAMPLERATE = 44100 # time resolution of the recording device (Hz)

p = pyaudio.PyAudio() # instantiate PyAudio
stream=p.open(format=pyaudio.paInt16,channels=1,rate=SAMPLERATE,input=True,
              frames_per_buffer=BUFFER) # use default input device to open audio stream
pause = False
key = False

# üç satır bir sütün üç adet ax ekseni hazırla, fig class ı çağır
fig, (ax1,ax2,ax3) = plt.subplots(3,1,sharex=True)

#x-y veri dizilerini ayrı ayrı diziler oluştur
xdata, ydata, ydata2, ydata3= [], [], [], []

#ax1, ax2 ve ax3 ü çiz 2D obje olarak ln, ln2 ve ln3'e boş olarak döndür
ln, = ax1.plot([], [], lw=1)
ln2, = ax2.plot([], [],lw=1)
ln3, = ax3.step([],[], lw=2) #yükslen ve alçalan kenarı göstermek için .step

# xdata zaman ekseni olarak buffer kadar eşit aralıkta dizi oluştur.
xdata = np.linspace(0, BUFFER-1, BUFFER)

# methods for animation
def init():
    ax1.set_xlim(0, BUFFER-1)       # ax1 x ekseni sınırla
    ax1.set_ylim(-100000, 100000)   #ax1 y ekseni sınırla 100 000
    ax2.set_ylim(-10000, 10000)   # ax2 ye ekseni sınırla 10 000
    ax3.set_ylim(-0.5,1.5)              # ax3 0-1 arası dijital
    return ln,ln2,ln3,    # Artist objelerin geri döndür

def animate(i):
    ydata = np.frombuffer(stream.read(BUFFER), dtype=np.int16)
    #ydata2 = np.frombuffer(stream.read(BUFFER), dtype=np.int16)
    ydata3= key
    
    print("ydata3",ydata3)
    ln.set_data(xdata, ydata)       # xdata ve ydata yı ln 2D objesinde birleştir
    ln2.set_data(xdata, ydata / 2) # ydata 10 a böl
    ln3.set_data(xdata, ydata3)
    return ln,ln2,ln3,

def onClick(event):
    global pause
    pause ^= True
    if pause==False:
        anim.resume()
    else:
        anim.pause()
    #print(pause)

def onKey(event):
    global key
    #time.sleep(1)
    key ^= True
    
    
#mouse klik oluşunca onClick fonksiyonuna git
fig.canvas.mpl_connect('button_press_event', onClick)
fig.canvas.mpl_connect('key_press_event', onKey)
anim = FuncAnimation(fig, animate, init_func=init, frames=200, interval=20,
       blit=True)

plt.show()

# stop and close the audio stream
stream.stop_stream()
stream.close()
p.terminate()

键按LOL

键按高

Hello i need both sound singnal and a digital signal on the same subplots. for 1st and secong plots are sound signals and i can read it very fast. I need a 3rd digital digital signal to see. In this case when i press key button i need a digital HIGH. when i wrote the python code 3rd signal (key press) is not changing in the middle of the plot. i mean i cant see the rising and falling edges during the plot. how can i solve it?

import numpy as np
import pyaudio
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
import time
plt.style.use('bmh')

BUFFER = 1024*4 # number of data points to read at a time
SAMPLERATE = 44100 # time resolution of the recording device (Hz)

p = pyaudio.PyAudio() # instantiate PyAudio
stream=p.open(format=pyaudio.paInt16,channels=1,rate=SAMPLERATE,input=True,
              frames_per_buffer=BUFFER) # use default input device to open audio stream
pause = False
key = False

# üç satır bir sütün üç adet ax ekseni hazırla, fig class ı çağır
fig, (ax1,ax2,ax3) = plt.subplots(3,1,sharex=True)

#x-y veri dizilerini ayrı ayrı diziler oluştur
xdata, ydata, ydata2, ydata3= [], [], [], []

#ax1, ax2 ve ax3 ü çiz 2D obje olarak ln, ln2 ve ln3'e boş olarak döndür
ln, = ax1.plot([], [], lw=1)
ln2, = ax2.plot([], [],lw=1)
ln3, = ax3.step([],[], lw=2) #yükslen ve alçalan kenarı göstermek için .step

# xdata zaman ekseni olarak buffer kadar eşit aralıkta dizi oluştur.
xdata = np.linspace(0, BUFFER-1, BUFFER)

# methods for animation
def init():
    ax1.set_xlim(0, BUFFER-1)       # ax1 x ekseni sınırla
    ax1.set_ylim(-100000, 100000)   #ax1 y ekseni sınırla 100 000
    ax2.set_ylim(-10000, 10000)   # ax2 ye ekseni sınırla 10 000
    ax3.set_ylim(-0.5,1.5)              # ax3 0-1 arası dijital
    return ln,ln2,ln3,    # Artist objelerin geri döndür

def animate(i):
    ydata = np.frombuffer(stream.read(BUFFER), dtype=np.int16)
    #ydata2 = np.frombuffer(stream.read(BUFFER), dtype=np.int16)
    ydata3= key
    
    print("ydata3",ydata3)
    ln.set_data(xdata, ydata)       # xdata ve ydata yı ln 2D objesinde birleştir
    ln2.set_data(xdata, ydata / 2) # ydata 10 a böl
    ln3.set_data(xdata, ydata3)
    return ln,ln2,ln3,

def onClick(event):
    global pause
    pause ^= True
    if pause==False:
        anim.resume()
    else:
        anim.pause()
    #print(pause)

def onKey(event):
    global key
    #time.sleep(1)
    key ^= True
    
    
#mouse klik oluşunca onClick fonksiyonuna git
fig.canvas.mpl_connect('button_press_event', onClick)
fig.canvas.mpl_connect('key_press_event', onKey)
anim = FuncAnimation(fig, animate, init_func=init, frames=200, interval=20,
       blit=True)

plt.show()

# stop and close the audio stream
stream.stop_stream()
stream.close()
p.terminate()

key press LOW

key press HIGH

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文