3

I am just starting to use scipy's signal functions.

In the attached image, I am trying find the position of the co-ordinates A[n] where the curve intersects an arbitrary threshold (left and right of the detected peak). Using peak_widths gives the points B[n] where the B's are at some relative peak position. I am looking for the A[n] where it is at a fixed threshold.

How do I solve this? I can't add inline pictures yet as I am new to Stack exchange. Image attached. Thanks in advance.

Annotated Plot from Code below

x = np.linspace(0, 6 * np.pi, 1000)
x = np.sin(x) #+ 0.6 * np.sin(2.6 * x)
peaks, _ = find_peaks(x,height=0.5)
threshold = 0.3
results_half = peak_widths(x, peaks, rel_height=0.5)
results_half[0]  # widths
plt.plot(x)
plt.plot(peaks, x[peaks], "x",color='r')
plt.hlines(*results_half[1:], color="C2")
plt.axhline(threshold,color='green')
testsignal
  • 31
  • 1

1 Answers1

0

peak_width is used to calculate full-width-half-maximum "fwhm" (with rel_height=0.5) and it's kind (using different rel_heights). So it always looks for the "start" and "end" of the peak curve to calculate the width of that peak. (see the scipy example: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.peak_widths.html?highlight=peak_width)

That's why B1 and B2 are positioned at a different height to B3-B6. You can see that B1 and B2 are in the middle of the starting point (the lowest point of that peak signal) and the first peak (the highest point of that peak signal) whereas B3-B6 are poistioned in the middle of their lowest and highest points.

I guess what you are looking for are the intersection points between your curve and the line defined by your arbitrary threshold. Try something like this: Intersection of two graphs in Python, find the x value or you can use the prominence_data parameter as shown here: python scipy.signal.peak_widths --> absolute heigth? (fft -3dB damping)

fabod
  • 1
  • 1