0

I want to ask a question about finding the maxima of a set of peaks using matplotlib and numpy.

I have been given data containing peaks and asked to calculate the maxima of the set of peaks.

Below is a picture of the peaks.

enter image description here

I discovered the find_peaks method and attempted to solve the problem using this.

I wrote the following block of code in Jupyter:

%pylab inline
from scipy.signal import find_peaks

testdata = loadtxt("testdata.dat", usecols=(0,1))
testdata_x = testdata[100:200,0]
testdata_y = testdata[100:200,1]
plot(testdata_x, testdata_y)
show()

peaks = find_peaks(testdata_y)
peaks

However, I get the following output for peaks:

(array([ 7, 12, 36, 40, 65, 69, 93, 97]), {})

I cannot understand why I get an output as above and am struggling to find a solution.

I attempted also to pass the following:

peaks = find_peaks(testdata_y, testdata_x)

but this was to no avail.

How can I sort out the matter?

I have attached the data file here as a download link if necessary (hoested on filehosting.org)

vik1245
  • 546
  • 2
  • 9
  • 26
  • I can see by a naked eye from your numbers and your plot that your `array([ 7, 12, 36, 40, 65, 69, 93, 97])` corresponds to indices of `testdata[100:200]` where maximums are met. – mathfux Feb 16 '20 at 23:04
  • The same is described in documentation – mathfux Feb 16 '20 at 23:06

1 Answers1

1

Like the comments say, the values returned by find_peaks are the indices (or locations) of the peaks.

To find the values of these peaks, use the peak indices to get the values out of testdata_y. Then you can get the max.

%pylab inline
from scipy.signal import find_peaks

testdata = loadtxt("testdata.dat", usecols=(0,1))
testdata_x = testdata[100:200,0]
testdata_y = testdata[100:200,1]
plot(testdata_x, testdata_y)
show()

peaks = find_peaks(testdata_y)
peak_values = testdata_y[peaks[0]]
max_peak = max(peak_values)
Dominic D
  • 1,778
  • 2
  • 5
  • 12
  • Yeah I understood later - I was just too stressed to realise what was going on until I counted the data points later! Many thanks nonetheless! – vik1245 Feb 17 '20 at 01:25