Python
SciPy
Peak-finding
Algorithm
Data Analysis

Peak-finding algorithm for Python/SciPy

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Peak finding in Python usually means locating local maxima in a one-dimensional signal. SciPy already provides a solid general-purpose solution through scipy.signal.find_peaks, so most of the real work is choosing the right filters such as prominence, distance, and height. In other words, the algorithmic question is often less about writing a peak detector from scratch and more about defining what should count as a peak in noisy data.

Start with scipy.signal.find_peaks

The basic API is simple: give it a numeric sequence, and it returns the peak indices.

python
1import numpy as np
2from scipy.signal import find_peaks
3
4signal = np.array([0, 1, 0, 2, 0, 1, 0])
5peaks, properties = find_peaks(signal)
6
7print(peaks)
8print(signal[peaks])

This prints the indices of local maxima. For a clean synthetic signal, that may already be enough.

Use Height and Distance to Filter Noise

Real signals often contain tiny local bumps that are not meaningful. find_peaks lets you filter them.

python
1import numpy as np
2from scipy.signal import find_peaks
3
4signal = np.array([0, 1, 0.8, 2.5, 0.2, 1.8, 0, 3.0, 0])
5peaks, properties = find_peaks(signal, height=1.5, distance=2)
6
7print(peaks)
8print(properties["peak_heights"])

Useful parameters include:

  • 'height for minimum peak value'
  • 'distance for minimum spacing between peaks'
  • 'prominence for how much a peak stands out from its surroundings'
  • 'width for filtering by peak shape'

These settings are often more important than the raw detection step.

Prominence Is Often the Best Real-World Filter

In noisy data, height alone can be misleading because a peak may be tall but still insignificant relative to the local baseline. Prominence is usually a better measure of whether a peak truly stands out.

python
1import numpy as np
2from scipy.signal import find_peaks
3
4signal = np.array([1.0, 1.1, 1.0, 2.0, 1.1, 1.0, 1.2, 3.0, 1.1])
5peaks, properties = find_peaks(signal, prominence=0.5)
6
7print(peaks)
8print(properties["prominences"])

When people say peak finding is "not working," the issue is often that they need prominence or distance constraints rather than a different algorithm.

Smooth the Signal First When Needed

If the data is very noisy, peak detection becomes unstable. A common workflow is to smooth the signal first and then run find_peaks on the smoothed result.

python
1import numpy as np
2from scipy.ndimage import gaussian_filter1d
3from scipy.signal import find_peaks
4
5raw_signal = np.array([0, 1.2, 0.9, 2.1, 1.8, 2.0, 0.7, 3.0, 2.8, 0.5])
6smoothed_signal = gaussian_filter1d(raw_signal, sigma=1)
7
8peaks, _ = find_peaks(smoothed_signal, prominence=0.3)
9print(peaks)

Smoothing can reduce false positives, but it can also blur sharp narrow peaks. That tradeoff is application-specific.

Inspect the Peak Properties

find_peaks can return useful properties beyond the positions themselves.

python
1import numpy as np
2from scipy.signal import find_peaks
3
4signal = np.array([0, 1, 0, 2, 0, 1.5, 0])
5peaks, properties = find_peaks(signal, prominence=0.5, width=1)
6
7print("indices:", peaks)
8print("prominences:", properties.get("prominences"))
9print("widths:", properties.get("widths"))

Those properties are often valuable for downstream filtering, plotting, or ranking detected peaks.

Plot the Result During Tuning

Peak-finding parameters are much easier to tune visually. Even a quick plot helps confirm whether the algorithm is identifying the peaks you actually care about.

python
1import matplotlib.pyplot as plt
2
3plt.plot(signal)
4plt.scatter(peaks, signal[peaks], color="red")
5plt.show()

This is especially useful when the same code works mathematically but disagrees with your domain expectations.

Common Pitfalls

  • Expecting raw local-max detection to work well on noisy signals without filtering.
  • Using only height thresholds when prominence would better capture meaningful peaks.
  • Ignoring minimum distance and getting many clustered detections for one broad peak.
  • Smoothing too aggressively and erasing the peaks you wanted to keep.
  • Treating peak-finding as one fixed algorithm instead of a parameter-tuning problem shaped by the data.

Summary

  • In SciPy, scipy.signal.find_peaks is the standard general-purpose tool for one-dimensional peak detection.
  • The most important work is usually choosing filters such as height, distance, prominence, and width.
  • Prominence is often more useful than raw height in noisy real-world data.
  • Smoothing can help, but it changes the signal and should be used deliberately.
  • Plotting detected peaks is often the fastest way to tune the algorithm correctly.

Course illustration
Course illustration

All Rights Reserved.