我是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))
答案 0 :(得分:3)
numpy.fft.rfft
是一个库函数,用于计算真实数据的fft
样本需要在时域中均匀分布。
由于某些样本在buf
中可能不均匀,因此使用numpy.interp
进行插值
self.buf[0]
是buf
的第一项
self.buf[-1]
是buf
的最后一项
len(self.buf)
是buf
因此,您最终得到相同数量的样本,但沿时间轴移动,使它们均匀分布(存储在变量interpolated
中)。
现在interpolated
可以传递给numpy.fft.rfft