2017-09-20 1 views
2

초과하는 시간 I는 두 개의 큰 목록 ty 있고 I가 소정 limit 초과 y에있는 시간과 기간을 데이터에 확대됨 방법, 즉 >=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 같은 다각형 라이브러리를 살펴해야 없음 두 번째 간격은 기가 시작하지 않고 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는 올바르게 계산되지만 렌즈에는 몇 가지 문제가 있습니다. 결과 목록은 기본적으로 0과 8로 구성됩니다. 실시간 벡터가 매우 정밀한 해상도 (0.0001, 0.00012, 0.00013, ...)를 가지며 시간 스탬프가 등거리가 아니기 때문일 수 있습니까? – Rickson

+0

@Rickson'(np.asarray (y)> = limit) .sum()'은 무엇을 제공합니까? – Divakar

+0

77. 렌즈의 길이와 시작 시간은 75입니다. – Rickson