I know this seems to be somewhat of a common question, but none of the answers currently seem to help my situation. I have a 2D numpy array which stores a spectrogram of a song. I want to identify the peaks using numpy's where function (I know people have other solutions for peak finding, but that's not what I'm looking for).
When I used this on my 2D array, I'm under the impression that it returns an array of x coordinates, and an array of y coordinates. Except almost all my x coordinates, except the last few, are all 5. The y coordinates seem like they would work except they go to high.
Here is an example of the output:
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
and my code looks like:
peaksx, peaksy = numpy.where(arr2D > (arr2Dcoefvar*threshold))
amplitudes = arr2D[peaksx,peaksy]
print(peaksx)
print(peaksy)
Here you can see I want to get the coordinates for any point which value (z value really) is above 7.3055...
The Shape of the arr2D is: (2049, 5037)
Am I not using the where function correctly? From what I've read it seems like I am, but the values are complete wrong.
Example Picture of it plotting incorrectly:
Example Picture of a good plot:
Thanks a bunch!
To answer this question for others who are curious about the answer, it was an issue with how they are indexed vs matplotlib. Kind of like when you study matrices and they list the height and then the length. It is similar here. Therefore the code:
peaksx, peaksy = numpy.where(arr2D > (arr2Dcoefvar*threshold))
should be
peaksy, peaksx = numpy.where(arr2D > (arr2Dcoefvar*threshold))
and then the plot will come out correct! :)