2017-09-20 16 views
2

我有兩個大的列表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 

enter image description here

在這個例子中,代碼應該返回以下列表:

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

我期望這可以在Numpy中實現得相當優雅,但並沒有發現如何。

任何建議,非常感謝。

+0

你應該看看多邊形圖書館像沙普利 – kezzos

+0

@Divakar沒有第二區間開始於6秒和10秒時完成。 – Rickson

回答

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 
+0

嗯。 start_times被正確計算,但鏡頭有一些問題。結果列表基本上只包含零和8)。可能是因爲實時矢量具有非常好的分辨率(0.0001,0.00012,0.00013,...),並且它的時間戳不是等距的? – Rickson

+0

@Rickson'(np.asarray(y)> = limit).sum()'給你什麼? – Divakar

+0

77.鏡頭長度和start_times是75. – Rickson