Home  >  Article  >  Backend Development  >  How to Find Significant Peaks in Python Using SciPy\'s find_peaks Function?

How to Find Significant Peaks in Python Using SciPy\'s find_peaks Function?

DDD
DDDOriginal
2024-10-22 20:33:13268browse

How to Find Significant Peaks in Python Using SciPy's find_peaks Function?

Finding Peaks in Python/SciPy

Finding peaks in data is a common task in various fields, including signal processing, image analysis, and data analysis. Python provides several packages and functions for peak detection, including SciPy's scipy.signal.find_peaks function.

SciPy's Peak-Finding Algorithm

The find_peaks function takes a 1D array as input and returns the indices of the peaks. It employs a peak-finding algorithm that detects peaks based on several parameters:

  • width: Minimum separation between peaks in samples.
  • threshold: Minimum amplitude threshold for peak detection.
  • distance: Minimum distance between consecutive peaks.
  • prominence: Topographic prominence, which measures the relative height of a peak compared to its surroundings.

Prominence for Noise Rejection

The prominence parameter is particularly useful for distinguishing significant peaks from noise-induced peaks. Prominence is defined as the minimum height descent to get from the peak to any higher terrain. By setting a high prominence threshold, the algorithm can effectively filter out minor peaks caused by noise.

Example Usage

The following code demonstrates peak-finding in a noisy frequency-varying sinusoid using the find_peaks function:

<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_prominence, _ = find_peaks(x, prominence=1)

plt.plot(x)
plt.plot(peaks_prominence, x[peaks_prominence], "ob")
plt.legend(['Signal', 'Peaks (prominence)'])
plt.show()</code>

As demonstrated in the plot, the find_peaks function finds peaks with both high amplitude and prominence, effectively filtering out noise-induced peaks.

Other Peak-Finding Options

In addition to find_peaks, SciPy also provides other peak-finding functionality, such as peak_widths and argrelmax. These functions may be more suitable for specific applications or adjustments.

Conclusion

SciPy's scipy.signal.find_peaks function provides a robust and versatile solution for peak-finding in Python. Its adjustable parameters, including prominence, allow for customization to detect significant peaks in various types of data.

The above is the detailed content of How to Find Significant Peaks in Python Using SciPy\'s find_peaks Function?. 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