kal*_*yan 1 python signal-processing matplotlib discrete-mathematics
因此,当特定事件发生时,我有一个时间列表Z(见下文).我如何看到这个信号有多接近周期?我想,我可以找到串行事件之间的成对时间差异,看看它是否是一个近似稳定的数量,但是有一种pythonic方式来做到这一点吗?
Z = [7.72, 10.9, 13.9, 16.69, 19.5, 22.31, 25.0, 27.69...]
Run Code Online (Sandbox Code Playgroud)
您的问题似乎更多地是关于分析信号的算法,但是为了获得Python中连续对之间的差异,您可以使用以下代码:
>>> Z = [7.72, 10.9, 13.9, 16.69, 19.5, 22.31, 25.0, 27.69]
>>> diffs = [a-b for a, b in zip(Z, Z[1:])]
>>> diffs
[-3.1800000000000006, -3.0, -2.790000000000001, -2.8099999999999987, -2.8099999999999987, -2.6900000000000013, -2.6900000000000013]
Run Code Online (Sandbox Code Playgroud)
使用此功能,您可以定义一个函数来比较给定值和容差的差异:
def is_periodic(samples, value, tolerance=0):
diffs = [a-b for a, b in zip(samples, samples[1:])]
return all(d-tolerance <= value <= d+tolerance for d in diffs)
>>> is_periodic(Z, -3, 1)
True
>>> is_periodic(Z, -3, 0.5)
True
>>> is_periodic(Z, -3, 0.25)
False
Run Code Online (Sandbox Code Playgroud)