簡體   English   中英

fft(快速傅立葉變換)如何工作

[英]how fft (fast Fourier transform) works

我是python的學習者,並且正在開發一個與圖像分析有關的小項目,以學習我試圖理解各種python代碼的概念,但是這次我很爛,可以有人解釋這個代碼嗎? 特別是FFT部分?

class HeartMonitor(object):

    def __init__(self, window_duration, fps = 30, min_bpm = 50, max_bpm = 200):
        """
        Class which detects heart-beats in a sequence of image colour samples.
        @param window_duration The number of seconds of samples to use
        @param fps             The nominal sample rate
        @param min_bpm         Minimum cut-off for possible heartrates
        @param max_bpm         Maximum cut-off for possible heartrates
        """

        self.min_bpm = min_bpm
        self.max_bpm = max_bpm

        # The maximum number of samples to buffer
        self.buf_size = int(window_duration*fps)

        # Buffer of (timestamp, value) tuples
        self.buf = []


    @property
    def fps(self):
        """
        The average framerate/samplerate of the buffer
        """
        return float(len(self.buf)) / (self.buf[-1][0] - self.buf[0][0])


    def get_fft(self):
        """
        Perform an Fast-Fourier-Transform on the buffer and return (magnitude,
        phase) tuples for each of the bins.
        """
        # Get the "ideal" evenly spaced times
        even_times = numpy.linspace(self.buf[0][0], self.buf[-1][0], len(self.buf))

        # Interpolate the data to generate evenly temporally spaced samples
        interpolated = numpy.interp(even_times, *zip(*self.buf))

        # Perform the FFT
        fft = numpy.fft.rfft(interpolated)
        return zip(numpy.abs(fft), numpy.angle(fft))

numpy.fft.rfft是一個庫函數,可從實際數據中計算出numpy.fft.rfft

樣本需要在時域中均勻間隔。

由於某些樣本可能無法在buf均勻分布,因此需要使用numpy.interp進行插值

self.buf[0]buf的第一項
self.buf[-1]buf的最后一項
len(self.buf)buf的項目數

因此,最終得到的樣本數相同,但沿時間軸移動,因此它們均勻分布(存儲在interpolated變量中)。

現在可以將interpolated傳遞給numpy.fft.rfft

暫無
暫無

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

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