vik1245
vik1245

Reputation: 556

Finding the maximum values of a set of local maxima using matplotlib and numpy

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)

Upvotes: 0

Views: 1018

Answers (1)

Dominic D
Dominic D

Reputation: 1808

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)

Upvotes: 1

Related Questions