获取二维 numpy 数组中最大点的索引

Obtaining Indexes for maximum points in a 2D numpy array

我知道这似乎是一个常见问题,但 none 目前的答案似乎对我的情况有所帮助。我有一个 2D numpy 数组,用于存储歌曲的频谱图。我想使用 numpy 的 where 函数来识别峰值(我知道人们有其他的峰值查找解决方案,但这不是我要找的)。

当我在二维数组上使用它时,我的印象是它 returns 一个 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 函数?从我读到的内容来看,我似乎是,但价值观完全错误。

绘图错误的示例图片:

一个好的情节的示例图片:

非常感谢!

要为其他对答案感到好奇的人回答这个问题,这是一个问题,它们是如何与 matplotlib 建立索引的。有点像当你研究矩阵时,它们先列出高度,然后列出长度。这里也是类似的。因此代码:

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

应该是

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

然后剧情就出来了! :)