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 ? Especially the FFT part ?
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))