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)

2
  • 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. Commented Feb 16, 2020 at 23:04
  • The same is described in documentation Commented Feb 16, 2020 at 23:06

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

1 Comment

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!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.