【问题标题】:Finding prominent frequency from FFT从 FFT 中找到显着频率
【发布时间】:2020-09-22 09:29:25
【问题描述】:

我已经使用音频频谱分析仪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。

【问题讨论】:

标签: python scipy fft frequency-analysis audio-analysis


【解决方案1】:

如果 yf 是 fft 的结果,那么你需要找到其中的最大值,对吗?如果它是一个 numpy 数组,amax() 函数将在这里为您提供帮助。 @DarrylG 为您指明了正确的方向; Print highest peak value of the frequency domain plot

【讨论】:

  • 这只是返回一个复数负载:/
  • @Peter 如果您渴望成为一名高效的独立软件工程师,您需要鼓起勇气掌握如何处理复数数组
  • @ScottStensland 是的,你是对的。我什至不知道fft是什么。我想为朋友制作一个钢琴调音器,但我想这不适合我。
  • 不要放弃! FFT 部分并不太可怕。 math.stackexchange.com/questions/1002/…
  • @asylumax 非常感谢!我会阅读这篇文章并再试一次。
【解决方案2】:

我使用问题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.")

【讨论】:

    猜你喜欢
    • 2012-02-08
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 2013-01-13
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 2011-07-16
    相关资源
    最近更新 更多