简体   繁体   English

fft(快速傅立叶变换)如何工作

[英]how fft (fast Fourier transform) works

I am a learner of python and developing a small project related to image analysis, to learn the concept I tried to understand various python codes, but this time I sucked and can any one explain this code ? 我是python的学习者,并且正在开发一个与图像分析有关的小项目,以学习我试图理解各种python代码的概念,但是这次我很烂,可以有人解释这个代码吗? Especially the FFT part ? 特别是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 is a library function that computes an fft from real data numpy.fft.rfft是一个库函数,可从实际数据中计算出numpy.fft.rfft

The samples need to be evenly spaced in the time domain. 样本需要在时域中均匀间隔。

Since some samples may not be evenly spaced in buf they are interpolated using numpy.interp 由于某些样本可能无法在buf均匀分布,因此需要使用numpy.interp进行插值

self.buf[0] is the first item of buf self.buf[0]buf的第一项
self.buf[-1] is the last item of buf self.buf[-1]buf的最后一项
len(self.buf) is the number of items in buf len(self.buf)buf的项目数

So you end up with the same number of samples, but moved along the time axis so they are evenly spaced (stored in the variable interpolated ). 因此,最终得到的样本数相同,但沿时间轴移动,因此它们均匀分布(存储在interpolated变量中)。

Now interpolated can be passed to numpy.fft.rfft 现在可以将interpolated传递给numpy.fft.rfft

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM