Python-实时高通滤波器(HPF)频谱分析仪落后

时间:2018-08-09 06:07:08

标签: python highpass-filter

我创建了一个python编码,将HPF实时应用于输入音频。效果很好。

import pyaudio
import time
import numpy as np
from scipy.signal import firwin, firwin2, lfilter, freqz

WIDTH = 2
CHANNELS = 2
RATE = 44100

p = pyaudio.PyAudio()
f1 = 500/1000

def bytes_to_float(byte_array):
    int_array = np.frombuffer(byte_array, dtype=np.float32)
    return int_array

def float_to_bytes(float_array):
    int_array = float_array.astype(np.float32)
    return int_array.tostring()

def callback(in_data, frame_count, time_info, flag):
    signal = bytes_to_float(in_data)
    a = [1]
    b = firwin(129, f1, pass_zero=False)
    filtered= lfilter(b, a, signal)
    output = float_to_bytes(filtered)
return (output, pyaudio.paContinue)

stream = p.open(format=pyaudio.paFloat32,
                channels=CHANNELS,
                rate=RATE,
                output=True,
                input=True,
                stream_callback=callback)

stream.start_stream()

while stream.is_active():
    time.sleep(0.1)

stream.stop_stream()
stream.close()

p.terminate()

现在,我想显示经过过滤的音频的频谱。因此,我编辑了上面的python编码。

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import time
import pyqtgraph as pg
import pyaudio
from scipy.signal import firwin, firwin2, lfilter, freqz

app = QtGui.QApplication([])
win = pg.GraphicsWindow(title="Basic plotting examples")
win.resize(1000,600)
win.setWindowTitle('pyqtgraph example: Plotting')
pg.setConfigOptions(antialias=True)

p6 = win.addPlot(title="Updating plot")
p6.setYRange(-100, 20)
curve = p6.plot(pen='y')

chunks = 1024

freq = np.linspace(1e-6, (chunks / 2), chunks, endpoint=True)

def bytes_to_float(byte_array):
    int_array = np.frombuffer(byte_array, dtype=np.float32)
    return int_array

def float_to_bytes(float_array):
    int_array = float_array.astype(np.float32)
    return int_array.tostring()

def update_freq_plot(x):
    global curve, freq
    h = 20 * np.log10(np.abs(np.fft.rfft(x, n=2 * chunks)[:chunks]))
    curve.setData(x=freq, y=h, clear=True)

def update():

    WIDTH = 2
    CHANNELS = 2
    RATE = 44100
    p = pyaudio.PyAudio()

    f1 = 500/1000

    def callback(in_data, frame_count, time_info, flag):
        signal = bytes_to_float(in_data)
        a = [1]
        b = firwin(129, f1, pass_zero=False)
        z = np.zeros(129 - 1)
        filtered, z = lfilter(b, a, signal, zi=z)
        update_freq_plot(filtered)
        output = float_to_bytes(filtered)
        return (output, pyaudio.paContinue)

    stream = p.open(format=pyaudio.paFloat32,
                channels=CHANNELS,
                rate=RATE,
                output=True,
                input=True,
                stream_callback=callback)

    stream.start_stream()


timer = QtCore.QTimer()
timer.timeout.connect(update)
timer.start(20)

## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
    import sys
    if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
        QtGui.QApplication.instance().exec_()

我使用this作为参考。当我运行编码时,它确实显示了频谱,但它滞后了。我正在使用jupyter来运行编码。

我想问一下如何改善编码,以免出现滞后问题?任何帮助深表感谢。谢谢。

0 个答案:

没有答案
相关问题