Peak-finding algorithm for Python/SciPy
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
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.
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.
Useful parameters include:
- '
heightfor minimum peak value' - '
distancefor minimum spacing between peaks' - '
prominencefor how much a peak stands out from its surroundings' - '
widthfor 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.
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.
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.
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.
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_peaksis 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.
Related reading

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.