2014-10-09 60 views
0

我是一名Python學習者,正在開發一個與圖像分析有關的小型項目,以瞭解我試圖理解各種python代碼的概念,但是這次我吸了,可以任何人解釋這一點代碼?特別是FFT部分?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)) 
+0

是你的問題理解FFT或做它的Python代碼? – 2014-10-09 05:20:08

+0

我知道FFT的神權觀念,但不知道這個python代碼如何實現FFT,所以問題是代碼不是FFT – user3218971 2014-10-09 05:27:42

回答

3

numpy.fft.rfft是一個庫函數,從實際數據

樣品需要在時域中均勻地間隔計算的FFT。

由於一些樣品可能不會均勻地在buf隔開他們正在使用numpy.interp

self.buf[0]內插是buf
self.buf[-1]中的第一項的buf
len(self.buf)的最後一個項目是項目中buf

所以你最終得到相同數量的樣本,但沿着時間軸移動,因此它們均勻間隔(存儲在變量ble interpolated)。

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

+0

感謝您的解釋 – user3218971 2014-10-09 09:54:48