所以当特定事件发生时,我有一个时间列表Z(见下文)。我如何看到这个信号有多接近周期? 我想,我可以找到连续事件之间的成对时间差异,看看它是否是一个近似稳定的数量,但是有一种pythonic方式来做这个吗?
Z = [7.72, 10.9, 13.9, 16.69, 19.5, 22.31, 25.0, 27.69...]
答案 0 :(得分:5)
您的问题似乎更多地是关于分析信号的算法,但是为了获得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]
使用此功能,您可以定义一个函数,将差异与给定值和容差进行比较:
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
答案 1 :(得分:1)
>>> Z = [7.72, 10.9, 13.9, 16.69, 19.5, 22.31, 25.0, 27.69]
>>> iterate_till = len(Z)-1
>>> [(Z[i+1] - val) for i, val in enumerate(Z) if i<iterate_till]
[3.1800000000000006, 3.0, 2.7900000000000009, 2.8099999999999987, 2.8099999999999987, 2.6900000000000013, 2.6900000000000013]