Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from PyQt4 import QtGui, QtCore
  4.  
  5. class LiveFFTWidget(QtGui.QWidget):
  6. def __init__(self):
  7. QtGui.QWidget.__init__(self)
  8.  
  9. # customize the UI
  10. self.initUI()
  11.  
  12. # init class data
  13. self.initData()
  14.  
  15. # connect slots
  16. self.connectSlots()
  17.  
  18. # init MPL widget
  19. self.initMplWidget()
  20.  
  21. def initUI(self):
  22.  
  23. hbox_gain = QtGui.QHBoxLayout()
  24. autoGain = QtGui.QLabel('Auto gain for frequency spectrum')
  25. autoGainCheckBox = QtGui.QCheckBox(checked=True)
  26. hbox_gain.addWidget(autoGain)
  27. hbox_gain.addWidget(autoGainCheckBox)
  28.  
  29. # reference to checkbox
  30. self.autoGainCheckBox = autoGainCheckBox
  31.  
  32. hbox_fixedGain = QtGui.QHBoxLayout()
  33. fixedGain = QtGui.QLabel('Manual gain level for frequency spectrum')
  34. fixedGainSlider = QtGui.QSlider(QtCore.Qt.Horizontal)
  35. hbox_fixedGain.addWidget(fixedGain)
  36. hbox_fixedGain.addWidget(fixedGainSlider)
  37.  
  38. self.fixedGainSlider = fixedGainSlider
  39.  
  40. vbox = QtGui.QVBoxLayout()
  41.  
  42. vbox.addLayout(hbox_gain)
  43. vbox.addLayout(hbox_fixedGain)
  44.  
  45. # mpl figure
  46. self.main_figure = MplFigure(self)
  47. vbox.addWidget(self.main_figure.toolbar)
  48. vbox.addWidget(self.main_figure.canvas)
  49.  
  50. self.setLayout(vbox)
  51.  
  52. self.setGeometry(300, 300, 350, 300)
  53. self.setWindowTitle('LiveFFT')
  54. self.show()
  55. # timer for callbacks, taken from:
  56. # http://ralsina.me/weblog/posts/BB974.html
  57. timer = QtCore.QTimer()
  58. timer.timeout.connect(self.handleNewData)
  59. timer.start(100)
  60. # keep reference to timer
  61. self.timer = timer
  62.  
  63.  
  64. def initData(self):
  65. mic = MicrophoneRecorder()
  66. mic.start()
  67.  
  68. # keeps reference to mic
  69. self.mic = mic
  70.  
  71. # computes the parameters that will be used during plotting
  72. self.freq_vect = np.fft.rfftfreq(mic.chunksize,
  73. 1./mic.rate)
  74. self.time_vect = np.arange(mic.chunksize, dtype=np.float32) / mic.rate * 1000
  75.  
  76. def connectSlots(self):
  77. pass
  78.  
  79. def initMplWidget(self):
  80. """creates initial matplotlib plots in the main window and keeps
  81. references for further use"""
  82. # top plot
  83. self.ax_top = self.main_figure.figure.add_subplot(211)
  84. self.ax_top.set_ylim(-32768, 32768)
  85. self.ax_top.set_xlim(0, self.time_vect.max())
  86. self.ax_top.set_xlabel(u'time (ms)', fontsize=6)
  87.  
  88. # bottom plot
  89. self.ax_bottom = self.main_figure.figure.add_subplot(212)
  90. self.ax_bottom.set_ylim(0, 1)
  91. self.ax_bottom.set_xlim(0, self.freq_vect.max())
  92. self.ax_bottom.set_xlabel(u'frequency (Hz)', fontsize=6)
  93. # line objects
  94. self.line_top, = self.ax_top.plot(self.time_vect,
  95. np.ones_like(self.time_vect))
  96.  
  97. self.line_bottom, = self.ax_bottom.plot(self.freq_vect,
  98. np.ones_like(self.freq_vect))
  99.  
  100.  
  101.  
  102. def handleNewData(self):
  103. """ handles the asynchroneously collected sound chunks """
  104. # gets the latest frames
  105. frames = self.mic.get_frames()
  106.  
  107. if len(frames) > 0:
  108. # keeps only the last frame
  109. current_frame = frames[-1]
  110. # plots the time signal
  111. self.line_top.set_data(self.time_vect, current_frame)
  112. # computes and plots the fft signal
  113. fft_frame = np.fft.rfft(current_frame)
  114. if self.autoGainCheckBox.checkState() == QtCore.Qt.Checked:
  115. fft_frame /= np.abs(fft_frame).max()
  116. else:
  117. fft_frame *= (1 + self.fixedGainSlider.value()) / 5000000.
  118. #print(np.abs(fft_frame).max())
  119. self.line_bottom.set_data(self.freq_vect, np.abs(fft_frame))
  120.  
  121. # refreshes the plots
  122. self.main_figure.canvas.draw()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement