使用scipy.signal.spectrogram在pyqtgraph中绘制wav文件的频谱

使用scipy.signal.spectrogram在pyqtgraph中绘制wav文件的频谱,第1张

使用scipy.signal.spectrogram在pyqtgraph中绘制wav文件的频谱

Scipy频谱图的输出可以很容易地从pyqtgraph绘制为ImageItem。通常,所得频谱图仅是灰度的。您可以使用直方图最轻松地进行调整。

例如,以下是如何使SciPy示例适用于声谱图以使用pyqtgraph(以pyqtgraph中的示例为基础):

from scipy import signalimport matplotlib.pyplot as pltimport numpy as npimport pyqtgraph# Create the datafs = 10e3N = 1e5amp = 2 * np.sqrt(2)noise_power = 0.01 * fs / 2time = 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 + noisef, t, Sxx = signal.spectrogram(x, fs)# Interpret image data as row-major instead of col-majorpyqtgraph.setConfigOptions(imageAxisOrder='row-major')pyqtgraph.mkQApp()win = pyqtgraph.GraphicsLayoutWidget()# A plot area (ViewBox + axes) for displaying the imagep1 = win.addPlot()# Item for displaying image dataimg = pyqtgraph.ImageItem()p1.addItem(img)# Add a histogram with which to control the gradient of the imagehist = pyqtgraph.HistogramLUTItem()# link the histogram to the imagehist.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 windowwin.show()# Fit the min and max levels of the histogram to the data availablehist.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 pixelimg.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 spectrogramp1.setLimits(xMin=0, xMax=t[-1], yMin=0, yMax=f[-1])# Add labels to the axisp1.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 comparisonplt.pcolormesh(t, f, Sxx)plt.ylabel('Frequency [Hz]')plt.xlabel('Time [sec]')plt.colorbar()plt.show()pyqtgraph.Qt.QtGui.QApplication.instance().exec_()

Matpotlib频谱图

Pyqtgraph ImageItem



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5649566.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存