Qu

find_peaks

Builtin index · Signal processing

result = find_peaks(x, [height=], [distance=], [prominence=], [width=], [rel_height=0.5])

Input: x, height, distance, prominence, width, rel_height · Output: result

What it is

x is a length-N vector. height (optional number): minimum peak value; distance (optional number, samples): minimum separation, enforced greedily by height so a close cluster keeps its tallest member; prominence (optional number): minimum rise above surrounding terrain (the real bounding-minima computation, not just local-maxima detection); width (optional number, samples): minimum width at rel_height (default 0.5, i.e. half-prominence) of the peak's prominence below its height, via linear interpolation of the crossing — matches scipy.signal.find_peaks's own algorithm. Kwarg names match SciPy; the original min_height/min_distance/min_prominence names are still accepted as aliases.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = find_peaks(x, [height=], [distance=], [prominence=], [width=], [rel_height=0.5])
x vectorx is a length-N vector.
height number, optionalheight (optional number): minimum peak value.
distance number, optionaldistance (optional number, samples): minimum separation, enforced greedily by height so a close cluster keeps its tallest member.
prominence number, optionalprominence (optional number): minimum rise above surrounding terrain (the real bounding-minima computation, not just local-maxima detection).
width number, optionalwidth (optional number, samples): minimum width at rel_height (default 0.5, i.e.
rel_height number, optional (default 0.5)width (optional number, samples): minimum width at rel_height (default 0.5, i.e.
result vector
Returns a vector of 0-indexed integer indices into x, one per surviving local maximum.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

  peaks = find_peaks(spectrum, min_height=0.1, min_distance=5)

from numerics-dsp

fs = 1000
n = 0:1:511
t = n / fs
x = sin(2*pi*20*t) + 0.2*randn(512, seed=4)

r = acf(x, max_lag=50)
below = find_peaks(-abs(r - 1/e))
print("acf[0] = {r[0]}, acf drops below 1/e near lag {below[0]}")

h_tone = hurst_exponent(x)
h_walk = hurst_exponent(cumsum(randn(512, seed=5)))
print("hurst (periodic+noise) = {h_tone}, hurst (random walk) = {h_walk}")
output
acf[0] = 1, acf drops below 1/e near lag 9
hurst (periodic+noise) = 0.71599, hurst (random walk) = 0.998318

from signal-processing

Fs = 1000
n = 0:1:1023
t = n / Fs
x = sin(2*pi*80*t) + 0.5*sin(2*pi*300*t)

X = fftc(x)
mag = abs(X)[0:512]                 # positive-frequency half

freqs = linspace(0, Fs/2, 512)
peaks = find_peaks(mag, min_height=max(mag)*0.2, min_distance=5)
print("tones at: {freqs[peaks]} Hz")
plot(freqs, mag)
xlabel("frequency (Hz)")
output
tones at: [80.234834, 300.391389] Hz
· plot: figure 2 (513 points)
0 50 100 150 200 250 300 350 400 450 500 -100 0 100 200 300 400 500 600 x=0.21, y=0 x=0.21, y=0 x=0.21, y=0 x=0.21, y=0 x=0.21, y=0 x=0.21, y=0 x=0.21, y=$10^{-9}$ x=0.21, y=$1.1\times10^{-9}$ x=0.21, y=$1.2\times10^{-9}$ x=0.21, y=$1.3\times10^{-9}$ x=0.21, y=$1.4\times10^{-9}$ x=0.21, y=$1.5\times10^{-9}$ x=0.21, y=$1.7\times10^{-9}$ x=0.21, y=$1.8\times10^{-9}$ x=0.21, y=$2\times10^{-9}$ x=0.21, y=$2.1\times10^{-9}$ x=0.21, y=$2.3\times10^{-9}$ x=0.21, y=$2.5\times10^{-9}$ x=0.21, y=$2.7\times10^{-9}$ x=0.21, y=$2.9\times10^{-9}$ x=0.21, y=$3.2\times10^{-9}$ x=0.21, y=$3.4\times10^{-9}$ x=0.21, y=$3.7\times10^{-9}$ x=0.21, y=$4\times10^{-9}$ x=0.21, y=$4.4\times10^{-9}$ x=0.21, y=$4.8\times10^{-9}$ x=0.21, y=$5.2\times10^{-9}$ x=0.21, y=$5.6\times10^{-9}$ x=0.21, y=$6.1\times10^{-9}$ x=0.21, y=$6.6\times10^{-9}$ x=0.21, y=$7.1\times10^{-9}$ x=0.21, y=$7.7\times10^{-9}$ x=0.21, y=$8.4\times10^{-9}$ x=0.21, y=$9.1\times10^{-9}$ x=0.21, y=$9.9\times10^{-9}$ x=0.21, y=$1.1\times10^{-8}$ x=0.21, y=$1.2\times10^{-8}$ x=0.21, y=$1.3\times10^{-8}$ x=0.21, y=$1.4\times10^{-8}$ x=0.21, y=$1.5\times10^{-8}$ x=0.21, y=$1.6\times10^{-8}$ x=0.21, y=$1.7\times10^{-8}$ x=0.21, y=$1.9\times10^{-8}$ x=0.21, y=$2\times10^{-8}$ x=0.21, y=$2.2\times10^{-8}$ x=0.21, y=$2.4\times10^{-8}$ x=0.21, y=$2.6\times10^{-8}$ x=0.21, y=$2.8\times10^{-8}$ x=0.21, y=$3.1\times10^{-8}$ x=0.21, y=$3.3\times10^{-8}$ x=0.21, y=$3.6\times10^{-8}$ x=0.21, y=$3.9\times10^{-8}$ x=0.21, y=$4.2\times10^{-8}$ x=0.21, y=$4.6\times10^{-8}$ x=0.21, y=$5\times10^{-8}$ x=0.21, y=$5.4\times10^{-8}$ x=0.21, y=$5.9\times10^{-8}$ x=0.21, y=$6.4\times10^{-8}$ x=0.21, y=$6.9\times10^{-8}$ x=0.21, y=$7.5\times10^{-8}$ x=0.21, y=$8.1\times10^{-8}$ x=0.21, y=$8.8\times10^{-8}$ x=0.21, y=$9.5\times10^{-8}$ x=0.21, y=$10^{-7}$ x=0.21, y=$1.1\times10^{-7}$ x=0.21, y=$1.2\times10^{-7}$ x=0.21, y=$1.3\times10^{-7}$ x=0.21, y=$1.4\times10^{-7}$ x=0.21, y=$1.5\times10^{-7}$ x=0.21, y=$1.7\times10^{-7}$ x=0.21, y=$1.8\times10^{-7}$ x=0.21, y=$2\times10^{-7}$ x=0.21, y=$2.1\times10^{-7}$ x=0.21, y=$2.3\times10^{-7}$ x=0.21, y=$2.5\times10^{-7}$ x=0.21, y=$2.7\times10^{-7}$ x=0.21, y=$3\times10^{-7}$ x=0.21, y=$3.2\times10^{-7}$ x=0.21, y=$3.5\times10^{-7}$ x=0.21, y=$3.8\times10^{-7}$ x=0.21, y=$4.1\times10^{-7}$ x=0.21, y=$4.4\times10^{-7}$ x=0.21, y=$4.8\times10^{-7}$ x=0.21, y=$5.2\times10^{-7}$ x=0.21, y=$5.7\times10^{-7}$ x=0.21, y=$6.1\times10^{-7}$ x=0.21, y=$6.7\times10^{-7}$ x=0.21, y=$7.2\times10^{-7}$ x=0.21, y=$7.8\times10^{-7}$ x=0.21, y=$8.5\times10^{-7}$ x=0.21, y=$9.2\times10^{-7}$ x=0.21, y=$10^{-6}$ x=0.21, y=$1.1\times10^{-6}$ x=0.21, y=$1.2\times10^{-6}$ x=0.21, y=$1.3\times10^{-6}$ x=0.21, y=$1.4\times10^{-6}$ x=0.21, y=$1.5\times10^{-6}$ x=0.21, y=$1.6\times10^{-6}$ x=0.21, y=$1.8\times10^{-6}$ x=0.21, y=$1.9\times10^{-6}$ x=0.21, y=$2.1\times10^{-6}$ x=0.21, y=$2.2\times10^{-6}$ x=0.21, y=$2.4\times10^{-6}$ x=0.21, y=$2.6\times10^{-6}$ x=0.21, y=$2.9\times10^{-6}$ x=0.21, y=$3.1\times10^{-6}$ x=0.21, y=$3.4\times10^{-6}$ x=0.21, y=$3.6\times10^{-6}$ x=0.21, y=$4\times10^{-6}$ x=0.21, y=$4.3\times10^{-6}$ x=0.21, y=$4.7\times10^{-6}$ x=0.21, y=$5\times10^{-6}$ x=0.21, y=$5.5\times10^{-6}$ x=0.21, y=$5.9\times10^{-6}$ x=0.21, y=$6.4\times10^{-6}$ x=0.21, y=$7\times10^{-6}$ x=0.21, y=$7.6\times10^{-6}$ x=0.21, y=$8.2\times10^{-6}$ x=0.21, y=$8.9\times10^{-6}$ x=0.21, y=$9.6\times10^{-6}$ x=0.21, y=$10^{-5}$ x=0.21, y=$1.1\times10^{-5}$ x=0.21, y=$1.2\times10^{-5}$ x=0.21, y=$1.3\times10^{-5}$ x=0.21, y=$1.4\times10^{-5}$ x=0.21, y=$1.6\times10^{-5}$ x=0.21, y=$1.7\times10^{-5}$ x=0.21, y=$1.8\times10^{-5}$ x=0.21, y=$2\times10^{-5}$ x=0.21, y=$2.2\times10^{-5}$ x=0.21, y=$2.3\times10^{-5}$ x=0.21, y=$2.5\times10^{-5}$ x=0.21, y=$2.8\times10^{-5}$ x=0.21, y=$3\times10^{-5}$ x=0.21, y=$3.2\times10^{-5}$ x=0.21, y=$3.5\times10^{-5}$ x=0.21, y=$3.8\times10^{-5}$ x=0.21, y=$4.1\times10^{-5}$ x=0.21, y=$4.5\times10^{-5}$ x=0.21, y=$4.9\times10^{-5}$ x=0.21, y=$5.3\times10^{-5}$ x=0.21, y=$5.7\times10^{-5}$ x=0.21, y=$6.2\times10^{-5}$ x=0.21, y=$6.7\times10^{-5}$ x=0.21, y=$7.3\times10^{-5}$ x=0.21, y=$7.9\times10^{-5}$ x=0.21, y=$8.6\times10^{-5}$ x=0.21, y=$9.3\times10^{-5}$ x=0.21, y=$10^{-4}$ x=0.21, y=$1.1\times10^{-4}$ x=0.21, y=$1.2\times10^{-4}$ x=0.21, y=$1.3\times10^{-4}$ x=0.21, y=$1.4\times10^{-4}$ x=0.21, y=$1.5\times10^{-4}$ x=0.21, y=$1.6\times10^{-4}$ x=0.21, y=$1.8\times10^{-4}$ x=0.21, y=$1.9\times10^{-4}$ x=0.21, y=$2.1\times10^{-4}$ x=0.21, y=$2.3\times10^{-4}$ x=0.21, y=$2.5\times10^{-4}$ x=0.21, y=$2.7\times10^{-4}$ x=0.21, y=$2.9\times10^{-4}$ x=0.21, y=$3.1\times10^{-4}$ x=0.21, y=$3.4\times10^{-4}$ x=0.21, y=$3.7\times10^{-4}$ x=0.21, y=$4\times10^{-4}$ x=0.21, y=$4.3\times10^{-4}$ x=0.21, y=$4.7\times10^{-4}$ x=0.21, y=$5.1\times10^{-4}$ x=0.21, y=$5.5\times10^{-4}$ x=0.21, y=$6\times10^{-4}$ x=0.21, y=$6.5\times10^{-4}$ x=0.21, y=$7\times10^{-4}$ x=0.21, y=$7.6\times10^{-4}$ x=0.21, y=$8.2\times10^{-4}$ x=0.21, y=$8.9\times10^{-4}$ x=0.21, y=$9.7\times10^{-4}$ x=0.21, y=0.001 x=0.21, y=0.001 x=0.21, y=0.001 x=0.21, y=0.001 x=0.21, y=0.001 x=0.21, y=0.002 x=0.21, y=0.002 x=0.21, y=0.002 x=0.21, y=0.002 x=0.21, y=0.002 x=0.209, y=0.002 x=0.209, y=0.002 x=0.209, y=0.003 x=0.209, y=0.003 x=0.209, y=0.003 x=0.209, y=0.003 x=0.209, y=0.003 x=0.209, y=0.004 x=0.208, y=0.004 x=0.208, y=0.004 x=0.208, y=0.004 x=0.207, y=0.004 x=0.207, y=0.005 frequency (Hz)

from signal-processing

fs = 1000
n = 0:1:1023
t = n / fs
x = sin(2*pi*100*t) + 0.05 * randn(1024, seed=1)

psd_w = welch(x, fs, nperseg=256)
f_w = linspace(0, fs/2, length(psd_w))
peak_w = find_peaks(psd_w, min_height=max(psd_w)*0.5)
print("welch peak near {f_w[peak_w[0]]} Hz")

psd_p = periodogram(x, fs)          # single-segment special case of welch

f_p = linspace(0, fs/2, length(psd_p))
peak_p = find_peaks(psd_p, min_height=max(psd_p)*0.5)
print("periodogram peak near {f_p[peak_p[0]]} Hz")
output
welch peak near 101.5625 Hz
periodogram peak near 99.609375 Hz

from signal-processing

How it works

find_peaks(x, [height=], [distance=], [prominence=], [width=], [rel_height=]) — indices of x's local maxima, optionally filtered by height, minimum separation (samples — enforced greedily by height, so a close cluster keeps its tallest member), prominence (how much a peak rises above its own surrounding terrain, not just its absolute height — numeric::transforms::find_peaks's own real bounding-minima computation, not a shortcut), and now width (the peak's width, in samples, at rel_height — default 0.5 — of its prominence below the peak, computed by linear interpolation around the crossing exactly as scipy.signal.peak_widths does; see numeric::transforms::peak_width's own doc comment). Kwarg names match scipy.signal.find_peaks (height/distance/prominence/width) — the original min_height/min_distance/min_prominence names are kept as accepted aliases so existing scripts keep working unchanged. Returns just the peak indices (a plain Vec, not SciPy's (peaks, properties) pair — Qu has no tuple-return for that shape here); findpeaks below is the enriched, multi-field alternative when prominence/width VALUES (not just filters) are wanted.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.