【问题标题】:Determine how long a signal is above a predefined limit确定信号超过预定义限制的时间
【发布时间】:2017-09-20 09:54:55
【问题描述】:

我有两个大列表ty,我想以一种高效的方式确定y 中的数据在哪些时间和多长时间超过了预定义的limit,即>=limit

可以用以下示例数据说明问题:

t = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
y = [8,6,4,2,0,2,4,6,8,6,4,2,0,2,4,6,8]
limit = 4

在此示例中,代码应返回以下列表:

t_exceedance_start = [0,6,14]
t_how_long_above_limit = [2,4,2]

我希望这可以在Numpy 中非常优雅地实现,但不知道如何实现。

非常感谢任何建议。

【问题讨论】:

  • 你应该看看像 shapley 这样的多边形库
  • @Divakar No 第二个间隔从 6 秒开始,在 10 秒结束。

标签: python python-2.7 numpy signal-processing


【解决方案1】:

这是一种利用布尔值提高性能效率的矢量化方法 -

# Get array versions if aren't already
y = np.asarray(y)
t = np.asarray(t)

# Get mask of thresholded y with boundaries of False on either sides.
# The intention is to use one-off shifted comparison to catch the
# boundaries of each island of thresholed True values (done in next step).
# Those appended False values act as triggers to catch the start of 
# first island and end of last island.
mask = np.concatenate(( [False], y>=limit, [False] ))
idx = np.flatnonzero(mask[1:] != mask[:-1])

# The starting indices for each island would be the indices at steps of 2.
# The ending indices would be steps of 2 as well starting from first index.
# Thus, get the island lengths by simply differencing between start and ends.
starts = idx[::2]
ends =   idx[1::2] - 1
lens = ends - starts

# Get starts, ends, lengths according to t times
start_times = t[starts]
end_times = t[ends]
len_times = end_times - start_times

【讨论】:

  • 嗯。 start_times 计算正确,但镜头存在一些问题。结果列表基本上只包含零 8 和一些)。可能是因为实时向量具有非常精细的分辨率(0.0001、0.00012、0.00013,...),并且它的时间戳不是等距的?
  • @Rickson (np.asarray(y)>=limit).sum() 给你什么?
  • 77。镜头长度和 start_times 为 75。
  • @Rickson 让我问你 - 如果不是 y = [8,6,4,2,0,2,4,6,8,6,4,2,0,2,4,6,8],你有:y = [2,2,4,2,0,2,4,6,8,6,4,2,0,2,4,6,8],即前两个元素发生了变化,输出会是什么?有些长度会为零,因为您解释长度的方式并不完全正确。
  • 是的。我还需要一个“结束”列表,以便能够计算每个间隔长度(例如,每个间隔 i 的 end[i]-starts[i])。
猜你喜欢
  • 1970-01-01
  • 2012-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
  • 2015-08-05
相关资源
最近更新 更多