0

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))
d-coder
  • 12,813
  • 4
  • 26
  • 36
user3218971
  • 547
  • 1
  • 6
  • 21

1 Answers1

3

numpy.fft.rfft is a library function that computes an fft from real data

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

self.buf[0] is the first item of buf
self.buf[-1] is the last item of buf
len(self.buf) is the number of items in 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).

Now interpolated can be passed to numpy.fft.rfft

John La Rooy
  • 295,403
  • 53
  • 369
  • 502