librosa.display.specshow(S_DB, sr=sr_1nico14, hop_length=hop_length, x_axis='time', y_axis='mel');
时间: 2024-06-06 16:10:09 浏览: 129
这是一行使用librosa库中的specshow函数来显示音频信号的频谱图的代码。其中S_DB是频谱图的数据,sr是音频采样率,hop_length是帧移(即相邻帧之间的时间间隔),x_axis和y_axis分别表示频谱图的x轴和y轴的标尺。x_axis='time'表示x轴表示时间,y_axis='mel'表示y轴表示梅尔频率。
相关问题
mel = librosa.filters.mel(sr=sr_1nico14, n_fft=n_fft, n_mels=n_mels)
这是一个使用Librosa库的函数调用,用于计算音频信号的Mel频率倒谱系数(Mel Spectrogram)。其中,sr参数表示音频采样率,n_fft参数表示傅里叶变换的窗口大小,n_mels参数表示Mel滤波器的数量。Mel频率倒谱系数是一种常用的音频特征,用于语音识别、情感分析、音乐信息检索等应用。
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtChart import * class RandomGenerator(QObject): dataReady = pyqtSignal(int) stopSignal = pyqtSignal() def init(self): super().init() self.queue = [] self.count = 0 def start(self): while True: val = random.randint(1, 100) if val < 50: self.queue.append(val) if len(self.queue) == 1: self.dataReady.emit(val) else: self.count += 1 if self.count >= 2: self.stopSignal.emit() break class ChartDrawer(QObject): finished = pyqtSignal() def init(self, queue): super().init() self.queue = queue self.series = QLineSeries() def start(self): while True: if len(self.queue) > 0: val = self.queue.pop(0) self.series.append(self.series.count(), val) else: break self.finished.emit() class MainWindow(QMainWindow): def init(self): super().init() self.generator = RandomGenerator() self.drawer = ChartDrawer(self.generator.queue) self.chartView = QChartView() self.chart = QChart() self.chart.addSeries(self.drawer.series) self.chart.createDefaultAxes() self.chartView.setChart(self.chart) self.startButton = QPushButton("Start") self.startButton.clicked.connect(self.start) self.stopButton = QPushButton("Stop") self.stopButton.clicked.connect(self.stop) self.stopButton.setEnabled(False) self.statusBar().showMessage("Ready") layout = QVBoxLayout() layout.addWidget(self.chartView) layout.addWidget(self.startButton) layout.addWidget(self.stopButton) widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) def start(self): self.statusBar().showMessage("Running") self.generatorThread = QThread() self.generator.moveToThread(self.generatorThread) self.generatorThread.started.connect(self.generator.start) self.generator.dataReady.connect(self.handleDataReady) self.generator.stopSignal.connect(self.handleStopSignal) self.generatorThread.start() self.drawerThread = QThread() self.drawer.moveToThread(self.drawerThread) self.drawer.finished.connect(self.handleDrawerFinished) self.drawerThread.start() self.startButton.setEnabled(False) self.stopButton.setEnabled(True) def stop(self): self.statusBar().showMessage("Stopping") self.generator.stopSignal.emit() self.generatorThread.quit() self.drawerThread.quit() self.startButton.setEnabled(True) self.stopButton.setEnabled(False) def handleDataReady(self, val): self.drawer.series.append(self.drawer.series.count(), val) def handleStopSignal(self): self.generatorThread.quit() def handleDrawerFinished(self): self.chartView.update() self.statusBar().showMessage("Finished") self.startButton.setEnabled(True) self.stopButton.setEnabled(False) if name == 'main': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) 请检查上述代码为何没有画图并展示
这段代码缺少了一些必要的部分,例如 `random` 模块的导入,以及 `RandomGenerator` 类中 `init` 方法的名称错误。此外,`ChartDrawer` 的构造函数应该接受 `queue` 参数并将其存储为实例属性。
另外,`MainWindow` 类中 `start` 方法的最后一行应该是 `self.chartView.show()`,而不是 `self.chartView.update()`。
以下是修改后的代码,应该可以正确地显示图表:
```
import random
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtChart import *
class RandomGenerator(QObject):
dataReady = pyqtSignal(int)
stopSignal = pyqtSignal()
def __init__(self):
super().__init__()
self.queue = []
self.count = 0
def start(self):
while True:
val = random.randint(1, 100)
if val < 50:
self.queue.append(val)
if len(self.queue) == 1:
self.dataReady.emit(val)
else:
self.count += 1
if self.count >= 2:
self.stopSignal.emit()
break
class ChartDrawer(QObject):
finished = pyqtSignal()
def __init__(self, queue):
super().__init__()
self.queue = queue
self.series = QLineSeries()
def start(self):
while True:
if len(self.queue) > 0:
val = self.queue.pop(0)
self.series.append(self.series.count(), val)
else:
break
self.finished.emit()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.generator = RandomGenerator()
self.drawer = ChartDrawer(self.generator.queue)
self.chartView = QChartView()
self.chart = QChart()
self.chart.addSeries(self.drawer.series)
self.chart.createDefaultAxes()
self.chartView.setChart(self.chart)
self.startButton = QPushButton("Start")
self.startButton.clicked.connect(self.start)
self.stopButton = QPushButton("Stop")
self.stopButton.clicked.connect(self.stop)
self.stopButton.setEnabled(False)
self.statusBar().showMessage("Ready")
layout = QVBoxLayout()
layout.addWidget(self.chartView)
layout.addWidget(self.startButton)
layout.addWidget(self.stopButton)
widget = QWidget()
widget.setLayout(layout)
self.setCentralWidget(widget)
def start(self):
self.statusBar().showMessage("Running")
self.generatorThread = QThread()
self.generator.moveToThread(self.generatorThread)
self.generatorThread.started.connect(self.generator.start)
self.generator.dataReady.connect(self.handleDataReady)
self.generator.stopSignal.connect(self.handleStopSignal)
self.generatorThread.start()
self.drawerThread = QThread()
self.drawer.moveToThread(self.drawerThread)
self.drawer.finished.connect(self.handleDrawerFinished)
self.drawerThread.start()
self.startButton.setEnabled(False)
self.stopButton.setEnabled(True)
def stop(self):
self.statusBar().showMessage("Stopping")
self.generator.stopSignal.emit()
self.generatorThread.quit()
self.drawerThread.quit()
self.startButton.setEnabled(True)
self.stopButton.setEnabled(False)
def handleDataReady(self, val):
self.drawer.series.append(self.drawer.series.count(), val)
def handleStopSignal(self):
self.generatorThread.quit()
def handleDrawerFinished(self):
self.chartView.show()
self.statusBar().showMessage("Finished")
self.startButton.setEnabled(True)
self.stopButton.setEnabled(False)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
```
阅读全文