簡體   English   中英

從 FFT 中找到顯着頻率

[英]Finding prominent frequency from FFT

I've set up a python audio stream and fft using bits of code for an audio spectrum analyser https://github.com/markjay4k/Audio-Spectrum-Analyzer-in-Python/blob/master/audio%20spectrum_pt2_spectrum_analyzer.ipynb (我刪除了所有的繪圖代碼),我想從我的 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 上的非適配音頻頻譜分析儀的 output 的屏幕截圖,顯示了我想從 fft(最突出的頻率)獲得的值。 在這種情況下,該值約為 1555Hz。

期望值的圖像

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

如果 yf 是 fft 的結果,那么您需要找到其中的最大值,對嗎? 如果它是 numpy 數組,amax() function 將在這里為您提供幫助。 @DarrylG 為您指明了正確的方向; 打印頻域plot的最高峰值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM