Skip to main content
 首页 » 编程设计

python之给定离散事件时间,检查离散信号是否周期性(或接近)

2025年05月04日15tintown

所以当特定事件发生时,我有一个时间 Z(见下文)列表。我怎样才能看到这个信号有多接近周期性? 我想,我可以找到序列事件之间的成对时间差,看看它是否是一个大致稳定的数量,但是有没有一种 pythonic 方法可以做到这一点?

Z = [7.72, 10.9, 13.9, 16.69, 19.5, 22.31, 25.0, 27.69...] 

请您参考如下方法:

您的问题似乎更多地是关于分析信号的算法,但要获得 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