Home >Backend Development >Python Tutorial >How Does Prominence Help in Peak Detection in Python Using SciPy?

How Does Prominence Help in Peak Detection in Python Using SciPy?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-10-22 22:44:29384browse

How Does Prominence Help in Peak Detection in Python Using SciPy?

Peak Detection Algorithm in Python/SciPy

Detecting peaks in data is a common task in data analysis. For Python users, SciPy provides the scipy.signal.find_peaks function, tailored specifically for this purpose.

Choosing the Right Parameters

To effectively identify peaks, understanding the available parameters is crucial. While parameters like width, threshold, and distance offer some utility, the parameter that truly distinguishes true peaks from noise is prominence.

What is Prominence?

Prominence measures the height required to descend from a peak to any higher terrain. In other words, it indicates the peak's "importance" relative to surrounding data points.

Using Prominence for Peak Detection

Testing find_peaks using a frequency-varying sinusoid demonstrates the effectiveness of prominence. While other parameters struggle to account for varying peak widths or noise levels, prominence consistently identifies significant peaks.

Code Example

The following code snippet illustrates the use of find_peaks with different parameters:

<code class="python">import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks

x = np.sin(2*np.pi*(2**np.linspace(2,10,1000))*np.arange(1000)/48000) + np.random.normal(0, 1, 1000) * 0.15
peaks, _ = find_peaks(x, distance=20)
peaks2, _ = find_peaks(x, prominence=1)      # BEST!
peaks3, _ = find_peaks(x, width=20)
peaks4, _ = find_peaks(x, threshold=0.4)

plt.subplot(2, 2, 1)
plt.plot(peaks, x[peaks], "xr"); plt.plot(x); plt.legend(['distance'])
plt.subplot(2, 2, 2)
plt.plot(peaks2, x[peaks2], "ob"); plt.plot(x); plt.legend(['prominence'])
plt.subplot(2, 2, 3)
plt.plot(peaks3, x[peaks3], "vg"); plt.plot(x); plt.legend(['width'])
plt.subplot(2, 2, 4)
plt.plot(peaks4, x[peaks4], "xk"); plt.plot(x); plt.legend(['threshold'])
plt.show()</code>

The results show that prominence effectively identifies the significant peaks, even in the presence of noise. By combining parameters like prominence with others like distance or width, you can further refine peak detection in complex data.

The above is the detailed content of How Does Prominence Help in Peak Detection in Python Using SciPy?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn