2017-10-18 94 views
0

我知道這似乎是一個常見問題,但目前沒有任何答案似乎對我的情況有所幫助。我有一個2D numpy陣列,可以存儲歌曲的聲譜圖。我想用numpy的where函數來識別峯值(我知道人們有其他的峯值搜索解決方案,但這不是我正在尋找的)。獲取2D numpy陣列中最大點的索引

當我在我的2D數組上使用它時,我感覺它返回一個x座標數組和一個y座標數組。除了最後幾個之外,幾乎所有的x座標都是5. y座標看起來就像他們會工作,除非他們高。

下面是輸出的一個例子:

Coefficient of Variation = 0.310873 
Skew = 33.2851477504 
Signal to Noise Ratio = 3.21674642281 
Peak threshold Scaler = 23.5 
Peak Amplitude threshold = 7.30551834404 

[5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 
6 6 6] 
[ 259 283 324 388 389 412 424 449 453 501 1357 1422 1458 1459 1482 
1483 1486 1487 1535 1809 1874 1938 1939 1976 1999 2003 2068 2069 2084 2085 
2100 2101 2102 2116 2117 2118 2133 2134 2149 2150 2165 2166 2181 2182 2197 
2198 2199 2213 2214 2215 2229 2230 2231 2246 2247 2262 2263 2278 2279 2294 
2295 2296 2326 2350 2366 2367 2379 2391 2415 2431 2443 2455 2456 2480 2496 
2508 2520 2544 2556 2557 2568 2569 2843 3101 3126 3142 3154 3166 3190 3206 
3207 3218 3219 3231 3255 3271 3283 3295 3296 3319 3320 3331 3332 3344 3356 
3400 3412 3424 3449 3465 3477 3489 3513 3514 3529 3530 3541 3542 3554 3578 
3590 3602 3614 4119 4127 4135 4159 4175 4176 4187 4188 4200 4224 4240 4252 
4264 4265 4288 4289 4304 4305 4317 4329 4353 4365 4377 4389 4390 4393 4418 
4434 4446 4458 4482 4498 4499 4510 4511 4523 4547 4563 4575 4587 4588 4611 
4612 4623 4624 4636 4648 4652 4676 4692 4704 4716 4741 4757 4769 4781 4805 
4806 4821 4822 4833 4834 424 1974 1976] 
Total Time: 0.853456020355 seconds 
Time to find peaks: 0.0450880527496 seconds 
Number of x coords: 188 
Number of y coords: 188 
Number of amplitudes: 188 

和我的代碼如下所示:

peaksx, peaksy = numpy.where(arr2D > (arr2Dcoefvar*threshold)) 
amplitudes = arr2D[peaksx,peaksy] 

print(peaksx) 
print(peaksy) 

在這裏你可以看到我想要得到的任何點的座標,其值(Z值真的)高於7.3055 ...

的arr2D的形狀是:(2049,5037)

我沒有正確使用where功能?從我讀過的東西看來,我很喜歡,但是價值觀是完全錯誤的。

其繪製錯誤的示例圖片: bad maxima

例的好情節的圖片: good maxima

感謝一大堆!

+0

你是如何產生「好」情節的?這是不同的數據相同的方法? – bnaecker

+0

很難說有限的數據,不知道你的完整代碼是什麼樣的。 np.where會給你在arr2D中的索引,但它是否完全對應於你的x和y值?如果沒有,那麼你是否轉換了這些數據?這也可能是數據本身的問題嗎? – BenT

+0

@bnaecker是的,好的情節是從一些來自stackoverflow的代碼生成的,但是它需要將近44秒才能完成計算。但它使用相同的數據。 – Clement

回答

0

爲了回答這個問題,對於那些對答案感到好奇的人來說,這是一個關於它們如何與matplotlib建立索引的問題。有點像當你學習矩陣時,他們列出了高度,然後是長度。這裏類似。因此,代碼:

peaksx, peaksy = numpy.where(arr2D > (arr2Dcoefvar*threshold)) 

應該

peaksy, peaksx = numpy.where(arr2D > (arr2Dcoefvar*threshold)) 

,然後劇情會出來正確的! :)