0
0
SciPydata~5 mins

Peak finding (find_peaks) in SciPy

Choose your learning style9 modes available
Introduction

We use peak finding to locate the highest points in data. This helps us understand important events or changes.

Finding the highest sales days in a month from daily sales data.
Detecting heartbeats from a noisy ECG signal.
Identifying the loudest moments in an audio recording.
Spotting the tallest waves in ocean wave height data.
Syntax
SciPy
scipy.signal.find_peaks(x, height=None, threshold=None, distance=None, prominence=None, width=None)

x is the data array where you want to find peaks.

You can add conditions like minimum height or distance between peaks to control the results.

Examples
Find all peaks in the data without any conditions.
SciPy
from scipy.signal import find_peaks

peaks, _ = find_peaks(data)
Find peaks that have a height of at least 5.
SciPy
peaks, _ = find_peaks(data, height=5)
Find peaks that are at least 3 points apart.
SciPy
peaks, _ = find_peaks(data, distance=3)
Sample Program

This code finds peaks in the array x where the peak height is at least 2. It prints the data, the positions of the peaks, and their heights.

SciPy
import numpy as np
from scipy.signal import find_peaks

# Create sample data with peaks
x = np.array([0, 2, 1, 3, 7, 1, 2, 6, 0, 1])

# Find peaks
peaks, properties = find_peaks(x, height=2)

print("Data:", x)
print("Peak indices:", peaks)
print("Peak heights:", properties['peak_heights'])
OutputSuccess
Important Notes

Peaks are points higher than their neighbors.

You can use prominence to find peaks that stand out more clearly.

Distance helps avoid detecting peaks that are too close together.

Summary

Use find_peaks to locate high points in data.

You can set conditions like height and distance to control which peaks are found.

This helps analyze patterns and important events in data.