使用 scipy.signal.spectrogram 在 pyqtgraph 中绘制 wavfile 的频谱 [英] plotting the spectrum of a wavfile in pyqtgraph using scipy.signal.spectrogram

查看:121
本文介绍了使用 scipy.signal.spectrogram 在 pyqtgraph 中绘制 wavfile 的频谱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用于音乐和语音分析的 PyQt 和 pyqtgraph 程序,我想绘制 wav 文件的频谱(使用 scipy python 包计算).我可以在 matplotlib 中完成,但由于 matplotlib 的性能,我需要切换到 pyqtgraph,但我找不到任何一致的方法将 scipy.signal.spectrogram 的输出绘制到 pyqtgraph

I have a PyQt plus pyqtgraph program for music and speech analysis and I want to plot the spectrum of a wav file (calculated using scipy python package). I can do it in matplotlib but due to matplotlib's performance I need to switch to pyqtgraph but I cant find any consistent method to plot the output of scipy.signal.spectrogram in to pyqtgraph

谢谢!

推荐答案

Scipy Spectrogram 的输出可以轻松地绘制为来自 pyqtgraph 的 ImageItem.通常,生成的频谱图只有灰度.您可以最轻松地使用直方图进行调整.

The output of the Scipy Spectrogram can be easily plotted as an ImageItem from pyqtgraph. Normally, the resulting Spectrogram is only in greyscale. You can most easily adjust this using a histogram.

举个例子,这里是如何将 SciPy 示例用于频谱图以使用 pyqtgraph(使用 pyqtgraph 中的示例作为基础):

As an example, here is how to adapt the SciPy example for a spectrogram to use pyqtgraph (using an example from pyqtgraph as the basis):

from scipy import signal
import matplotlib.pyplot as plt
import numpy as np
import pyqtgraph

# Create the data
fs = 10e3
N = 1e5
amp = 2 * np.sqrt(2)
noise_power = 0.01 * fs / 2
time = np.arange(N) / float(fs)
mod = 500*np.cos(2*np.pi*0.25*time)
carrier = amp * np.sin(2*np.pi*3e3*time + mod)
noise = np.random.normal(scale=np.sqrt(noise_power), size=time.shape)
noise *= np.exp(-time/5)
x = carrier + noise
f, t, Sxx = signal.spectrogram(x, fs)

# Interpret image data as row-major instead of col-major
pyqtgraph.setConfigOptions(imageAxisOrder='row-major')

pyqtgraph.mkQApp()
win = pyqtgraph.GraphicsLayoutWidget()
# A plot area (ViewBox + axes) for displaying the image
p1 = win.addPlot()

# Item for displaying image data
img = pyqtgraph.ImageItem()
p1.addItem(img)
# Add a histogram with which to control the gradient of the image
hist = pyqtgraph.HistogramLUTItem()
# Link the histogram to the image
hist.setImageItem(img)
# If you don't add the histogram to the window, it stays invisible, but I find it useful.
win.addItem(hist)
# Show the window
win.show()
# Fit the min and max levels of the histogram to the data available
hist.setLevels(np.min(Sxx), np.max(Sxx))
# This gradient is roughly comparable to the gradient used by Matplotlib
# You can adjust it and then save it using hist.gradient.saveState()
hist.gradient.restoreState(
        {'mode': 'rgb',
         'ticks': [(0.5, (0, 182, 188, 255)),
                   (1.0, (246, 111, 0, 255)),
                   (0.0, (75, 0, 113, 255))]})
# Sxx contains the amplitude for each pixel
img.setImage(Sxx)
# Scale the X and Y Axis to time and frequency (standard is pixels)
img.scale(t[-1]/np.size(Sxx, axis=1),
          f[-1]/np.size(Sxx, axis=0))
# Limit panning/zooming to the spectrogram
p1.setLimits(xMin=0, xMax=t[-1], yMin=0, yMax=f[-1])
# Add labels to the axis
p1.setLabel('bottom', "Time", units='s')
# If you include the units, Pyqtgraph automatically scales the axis and adjusts the SI prefix (in this case kHz)
p1.setLabel('left', "Frequency", units='Hz')

# Plotting with Matplotlib in comparison
plt.pcolormesh(t, f, Sxx)
plt.ylabel('Frequency [Hz]')
plt.xlabel('Time [sec]')
plt.colorbar()
plt.show()

pyqtgraph.Qt.QtGui.QApplication.instance().exec_()

Matpotlib 频谱图

Matpotlib Spectrogram

Pyqtgraph 图像项

Pyqtgraph ImageItem

这篇关于使用 scipy.signal.spectrogram 在 pyqtgraph 中绘制 wavfile 的频谱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆