2017-10-18 87 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)) 

,然后剧情会出来正确的! :)