如何使用 matplotlib 绘制 pyaudio 输入?
How to plot pyaudio input with matplotlib?
如何绘制来自麦克风的 matplotlib 输入信号?
我曾尝试使用 plt.plot(frames) 进行绘图,但出于某种原因,frames 是一个字符串?
a) 为什么 frames 变量是一个字符串列表?
b) 为什么数据变量是字符串列表?
c) 它们应该代表单个样本的 energy/amplitude 并且是整数吗?
d) 当我指定我想要 1024 的块大小时,为什么数据长度是 2048?
(我猜是因为我使用 paInt16,但仍然看不出为什么它不能是 1024)
我有以下麦克风输入代码:
import pyaudio
import audioop
import matplotlib.pyplot as plt
import numpy as np
from itertools import izip
import wave
FORMAT = pyaudio.paInt16 # We use 16bit format per sample
CHANNELS = 1
RATE = 44100
CHUNK = 1024 # 1024bytes of data red from a buffer
RECORD_SECONDS = 3
WAVE_OUTPUT_FILENAME = "file.wav"
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT,
channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
frames = ''.join(frames)
stream.stop_stream()
stream.close()
audio.terminate()
a) Why is frames variable a string list?
作为 b) 的结果,这就是您在代码中构建它的方式。
b) Why is data variable string list?
它是一个字节串,就是一个原始的字节序列。就是这样 read()
returns.
c) Should they represent energy/amplitude of single sample and be integers?
他们是。它们只是按字节顺序打包,而不是 Python 整数。
d) Why is length of data 2048 when I specified I want chunk size of 1024?
1024
是帧数。每帧有 2 个字节长,所以你得到 2048 个字节。
How can I plot on matplotlib input signal from microphone? I have tried to plot with plt.plot(frames) but frames is for some reason a string?
取决于你想画什么。可以通过将字节串转换为 numpy 数组来获得原始振幅:
fig = plt.figure()
s = fig.add_subplot(111)
amplitude = numpy.fromstring(frames, numpy.int16)
s.plot(amplitude)
fig.savefig('t.png')
更有用的图是 spectrogram:
fig = plt.figure()
s = fig.add_subplot(111)
amplitude = numpy.fromstring(frames, numpy.int16)
s.specgram(amplitude)
fig.savefig('t.png')
但是你可以随心所欲地修改振幅,因为你已经有了一个 numpy
数组。
如何绘制来自麦克风的 matplotlib 输入信号? 我曾尝试使用 plt.plot(frames) 进行绘图,但出于某种原因,frames 是一个字符串?
a) 为什么 frames 变量是一个字符串列表?
b) 为什么数据变量是字符串列表?
c) 它们应该代表单个样本的 energy/amplitude 并且是整数吗?
d) 当我指定我想要 1024 的块大小时,为什么数据长度是 2048?
(我猜是因为我使用 paInt16,但仍然看不出为什么它不能是 1024)
我有以下麦克风输入代码:
import pyaudio
import audioop
import matplotlib.pyplot as plt
import numpy as np
from itertools import izip
import wave
FORMAT = pyaudio.paInt16 # We use 16bit format per sample
CHANNELS = 1
RATE = 44100
CHUNK = 1024 # 1024bytes of data red from a buffer
RECORD_SECONDS = 3
WAVE_OUTPUT_FILENAME = "file.wav"
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT,
channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
frames = ''.join(frames)
stream.stop_stream()
stream.close()
audio.terminate()
a) Why is frames variable a string list?
作为 b) 的结果,这就是您在代码中构建它的方式。
b) Why is data variable string list?
它是一个字节串,就是一个原始的字节序列。就是这样 read()
returns.
c) Should they represent energy/amplitude of single sample and be integers?
他们是。它们只是按字节顺序打包,而不是 Python 整数。
d) Why is length of data 2048 when I specified I want chunk size of 1024?
1024
是帧数。每帧有 2 个字节长,所以你得到 2048 个字节。
How can I plot on matplotlib input signal from microphone? I have tried to plot with plt.plot(frames) but frames is for some reason a string?
取决于你想画什么。可以通过将字节串转换为 numpy 数组来获得原始振幅:
fig = plt.figure()
s = fig.add_subplot(111)
amplitude = numpy.fromstring(frames, numpy.int16)
s.plot(amplitude)
fig.savefig('t.png')
更有用的图是 spectrogram:
fig = plt.figure()
s = fig.add_subplot(111)
amplitude = numpy.fromstring(frames, numpy.int16)
s.specgram(amplitude)
fig.savefig('t.png')
但是你可以随心所欲地修改振幅,因为你已经有了一个 numpy
数组。