How can I get only the width at the maximum?

Question:

I am trying to calculate the width at the maximum, but when using peak_widths it returns all the peak widths within the signal.

    from scipy.signal import chirp, find_peaks, peak_widths
    import matplotlib.pyplot as plt

    x = np.linspace(0, 6 * np.pi, 1000)
    y = np.sin(x) + 0.6 * np.sin(2.6 * x)
    peaks, _ = find_peaks(y)
    results_half = peak_widths(y, peaks, rel_height=0.5)
    results_half[0]
    plt.plot(y)
    plt.plot(peaks, y[peaks], "x")


    plt.hlines(*results_half[1:], color="C2")
    plt.show()

This is the example from scipy.signal.peak_widths.

I want to get only the width at the maximum.

Enter image description here

I want to use the index of the maximum peak location to find the FWHM of the maximum peak. How can I find the corresponding width of the maximum?

    i_max_peak = peaks[np.argmax(y[peaks])]
    y_max = y[i_max_peak]

I want to get the value of the width for the maximum peak.

Asked By: wosker4yan

||

Answers:

You are half way there, I think.

You already got the index of the maximum peak with [np.argmax(y[peaks])]. What’s left is to get the width from results_half[0].

So this, I believe, will give you what you want:

max_peak_loc = np.argmax(y[peaks])
widths = results_half[0]
widths[max_peak_loc]
Answered By: DS_UNI
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.