我已经使用音频频谱分析仪的代码位https://github.com/markjay4k/Audio-Spectrum-Analyzer-in-Python/blob/master/audio%20spectrum_pt2_spectrum_analyzer.ipynb设置了一个 python 音频流和 fft (我删除了所有的绘图代码),我想从我的 fft 中找到最突出的频率。
import numpy as np
import pyaudio
import struct
from scipy.fftpack import fft
import sys
import time
class AudioStream(object):
def __init__(self):
# stream constants
self.CHUNK = 1024 * 2
self.FORMAT = pyaudio.paInt16
self.CHANNELS = 1
self.RATE = 44100
self.pause = False
# stream object
self.p = pyaudio.PyAudio()
self.stream = self.p.open(
format=self.FORMAT,
channels=self.CHANNELS,
rate=self.RATE,
input=True,
output=True,
frames_per_buffer=self.CHUNK,
)
self.start_recording()
def start_recording(self):
print('stream started')
while True:
#Get data from stream and unpack to data_int
data = self.stream.read(self.CHUNK)
data_int = struct.unpack(str(2 * self.CHUNK) + 'B', data)
# compute FFT
yf = fft(data_int)
# find the most prominent frequency from this fft
if __name__ == '__main__':
AudioStream()
下面是 github 上未适配的音频频谱分析仪输出的截图,显示了我想从 fft(最突出的频率)得到的值。在这种情况下,该值约为 1555Hz。