我已经建立了一个python音频流和快速傅立叶变换使用位的音频频谱分析仪https://github.com/markjay4k/Audio-Spectrum-Analyzer-in-Python/blob/master/audio%20spectrum_pt2_spectrum_analyzer.ipynb (我删除了所有的绘图代码),我想找到最突出的频率从我的快速傅立叶变换。
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 (最显著的频率)获得的值。在这种情况下,该值约为1555 In。

发布于 2020-06-04 00:19:28
我找到了一些使用问题Audio Frequencies in Python来做这件事的代码,我将把它留在下面:
# compute FFT
fftData=abs(np.fft.rfft(data_int))**2
# find the maximum
which = fftData[1:].argmax() + 1
# use quadratic interpolation around the max
if which != len(fftData)-1:
y0,y1,y2 = np.log(fftData[which-1:which+2:])
x1 = (y2 - y0) * .5 / (2 * y1 - y2 - y0)
# find the frequency and output it
thefreq = (which+x1)*self.RATE/self.CHUNK
print(f"The freq is {thefreq} Hz.")
else:
thefreq = which*self.RATE/self.CHUNK
print (f"The freq is {thefreq} Hz.")发布于 2020-06-03 23:54:03
如果yf是fft的结果,那么你需要找到其中的最大值,对吗?如果它是一个numpy数组,amax()函数将在这里帮助您。@DarrylG为你指明了正确的方向;Print highest peak value of the frequency domain plot
https://stackoverflow.com/questions/62176972
复制相似问题