Detecting peaks in signals is a must-have technique for anyone working with sensor data, biomedical signals, vibration analysis, or any periodic measurement.

Peaks often correspond to important events – heartbeats, local maxima, machinery faults, or cycles in experimental data. In Python, you can detect and analyze peaks quickly and flexibly with scipy.signal.find_peaks. Here’s how to do it right and get results you can trust.

How to Find Peaks in Python

Use scipy.signal.find_peaks to identify all local maxima in a 1D array. You can filter out noise and irrelevant peaks by setting parameters like minimum height, minimum distance between peaks, and more. This function returns the indices of peaks and lets you analyze their properties in detail.

Example: Detecting peaks in a noisy sine wave

import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks

# Create sample data
np.random.seed(0)
x = np.linspace(0, 6*np.pi, 1000)
signal = np.sin(x) + 0.3*np.random.randn(len(x))

# Detect peaks
peaks, properties = find_peaks(signal, height=0.8, distance=50)

# Plot the results
plt.figure(figsize=(10,4))
plt.plot(x, signal, label="Signal")
plt.plot(x[peaks], signal[peaks], 'ro', label="Peaks")
plt.legend()
plt.xlabel('x')
plt.title('Peak Detection Example')
plt.tight_layout()
plt.show()

  • height=0.8 filters out small peaks, keeping only prominent ones.
  • distance=50 ensures that peaks are at least 50 samples apart.

Choosing the Right Parameters

  • height: Use this to ignore peaks below a threshold. You can pass a number or a tuple (min, max) to restrict the range.
  • distance: Minimum number of samples between detected peaks. Useful for rejecting false multiple detections in wide peaks or closely spaced noise.
  • prominence: Find peaks that stand out clearly from the surroundings, regardless of absolute height. This is often more robust to baseline drift.
  • width: Enforce that only peaks with a certain width (at half-height) are detected. Useful when you expect broad peaks.

Example: Find only very prominent peaks

peaks, properties = find_peaks(signal, prominence=1)

Example: Find peaks with width at least 10

peaks, properties = find_peaks(signal, width=10)

Analyzing Peak Properties

The properties dictionary returned by find_peaks can include:

  • peak_heights: Height of each peak.
  • left_bases, right_bases: Points where the peak rises from and falls back to the baseline.
  • widths, prominences: Quantitative peak characteristics.

You can compute these properties after initial peak detection using scipy.signal.peak_widths or scipy.signal.peak_prominences.

Example: Get peak widths

from scipy.signal import peak_widths

widths, width_heights, left_ips, right_ips = peak_widths(signal, peaks, rel_height=0.5)

Best Practices and Practical Tips

  • Always plot your results. Visual inspection is key. Peaks can be missed if parameters are too strict, or noise can be counted as peaks if too loose.
  • Smooth your signal first. If data is very noisy, apply a filter (see previous article on filtering with scipy.signal) before peak detection.
  • Handle edge cases. Peaks near the edges can be sensitive to noise or windowing. Be careful interpreting them.
  • Combine parameters. Don’t rely only on height; combine height, distance, and prominence for robust detection.

Why Am I Missing Peaks?

  • Your height or prominence is set too high, try lowering them.
  • The signal is too noisy, apply a filter or moving average.
  • distance is larger than your expected period.
  • Peaks are wide and may not reach the threshold in the middle—adjust width parameter or smooth further.

Key Arguments for scipy.signal.find_peaks

Parameter Use Case Example Value
height Minimum/maximum peak value 0.5 or (0.5, 2.0)
distance Minimum samples between peaks 50
prominence How much the peak stands out 1
width Minimum width at half-height 10

Takeaway

scipy.signal.find_peaks gives you powerful, flexible peak detection for any 1D signal. Tune parameters to match your real data and always plot for sanity check. Combine with previous steps like filtering for clean, accurate results.

Share.
Leave A Reply