Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
pyqtgraph live running spectrogram from microphone
Tested on Linux with python 3.7
Must have portaudio installed (e.g. dnf install portaudio-devel)
pip install pyqtgraph pyaudio PyQt5
import numpy as np
import pyqtgraph as pg
import pyaudio
from PyQt5 import QtCore, QtGui
FS = 44100 #Hz
CHUNKSZ = 1024 #samples
class MicrophoneRecorder():
def __init__(self, signal):
self.signal = signal
self.p = pyaudio.PyAudio() =,
def read(self):
data =, exception_on_overflow=False)
y = np.fromstring(data, 'int16')
def close(self):
class SpectrogramWidget(pg.PlotWidget):
read_collected = QtCore.pyqtSignal(np.ndarray)
def __init__(self):
super(SpectrogramWidget, self).__init__()
self.img = pg.ImageItem()
self.img_array = np.zeros((1000, int(CHUNKSZ/2+1)))
# bipolar colormap
pos = np.array([0., 1., 0.5, 0.25, 0.75])
color = np.array([[0,255,255,255], [255,255,0,255], [0,0,0,255], (0, 0, 255, 255), (255, 0, 0, 255)], dtype=np.ubyte)
cmap = pg.ColorMap(pos, color)
lut = cmap.getLookupTable(0.0, 1.0, 256)
# set colormap
# setup the correct scaling for y-axis
freq = np.arange((CHUNKSZ/2)+1)/(float(CHUNKSZ)/FS)
yscale = 1.0/(self.img_array.shape[1]/freq[-1])
self.img.scale((1./FS)*CHUNKSZ, yscale)
self.setLabel('left', 'Frequency', units='Hz')
# prepare window for later use = np.hanning(CHUNKSZ)
def update(self, chunk):
# normalized, windowed frequencies in data chunk
spec = np.fft.rfft(chunk* / CHUNKSZ
# get magnitude
psd = abs(spec)
# convert to dB scale
psd = 20 * np.log10(psd)
# roll down one and replace leading edge with new data
self.img_array = np.roll(self.img_array, -1, 0)
self.img_array[-1:] = psd
self.img.setImage(self.img_array, autoLevels=False)
if __name__ == '__main__':
app = QtGui.QApplication([])
w = SpectrogramWidget()
mic = MicrophoneRecorder(w.read_collected)
# time (seconds) between reads
interval = FS/CHUNKSZ
t = QtCore.QTimer()
t.start(1000/interval) #QTimer takes ms
Copy link

rxa254 commented Mar 5, 2017

I get:
IOError: [Errno -9981] Input overflowed

and then a bunch of
IOError: [Errno -9988] Stream closed

when trying this. I've tried playing around with the chunk size, but no luck. Do you find this still works with modern pyqt ?

Copy link

pranav6670 commented Mar 2, 2019

@rxa254, update Pyaudio to the latest version and give the parameter in read() method like this : data =, exception_on_overflow=False)

Copy link

conraddisc commented May 11, 2020

I changed the from PyQt4 in line 4 to from PyQt5, added the above mentioned exception_on_overflow=False) to line 20

And added int to line 37:
self.img_array = np.zeros((1000, int(CHUNKSZ/2+1)))
and it works!

Copy link

AB9IL commented Dec 10, 2020

You can use colormaps from matplotlib:

from matplotlib import cm

# colormap
colormap = cm.get_cmap("CMRmap")
lut = (colormap._lut * 255).view(np.ndarray)

# set colormap

Copy link

boylea commented Dec 12, 2020

I finally got around to firing this up again and updated the gist with the suggested fixes, thanks @pranav6670 and @conraddisc !

Also thanks for the comment about using matplotlib's pre-defined maps @AB9IL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment