Python

Statistics

Peak detection

When we work with data, especially in signal processing, we often want to find peaks. A peak is a value in the data that is:

  • higher than its neigbors (a local maximum)
  • isolated (it stands out and there aren't many other similar values around it)

A peak doesn't have to be the highest value in the entire data - just the highest in its small section.

How to detect peaks?

One common method to detect peaks is to search for a zero-crossing of the differences between data points. For that, we first calculate the differences between data points. Than we look for places where the sign of the differences changes from positive to negative. These points indicate local maxima.

Peak detection in Python

Python's library scipy provides helpful functions like find_peaks and peak_prominences to detect peaks.

The function find_peaks helps to identify local maxima.

The function peak_prominences is used to measure how "tall" each peak is compared to the surrounding data.

Let's look at the following example that finds the biggest peak, based on its relative value.

from scipy.signal import find_peaks, peak_prominences
import numpy as np

data = np.array([1, 3, 7, 6, 2, 4, 9, 8, 5])

peaks, _ = find_peaks(data) # find peak indexes

# find how "tall" the peaks are
prominences = peak_prominences(data, peaks)[0]

peaks_info = sorted(zip(peaks, prominences), key=lambda x: x[1], reverse=True)

index, rel_height = peaks_info[0]
print("Peak value:", data[index])
print("Peak index:", index)
print("Prominence:", rel_height)

The example shows how to identify peaks using the find_peaks() method and then sort them by their relative height based on the peak_prominences().

By detecting peaks, we can find important moments in the data like e.g. seismic activity monitoring or heart rate analysis.