signal-processing
Peak Detection
Find local maxima above a threshold with findpeaks — heart-rate detection, spike counting, spectral peaks.
MATLAB's `findpeaks` returns peaks and locations as separate outputs; scipy's `find_peaks` returns only locations and you index the signal for values. Location indices are 0-based in scipy — useful when using them as numpy indices downstream, but careful when comparing to MATLAB's 1-based plot annotations.
MATLAB source12 lines
% Detect peaks in a noisy signal
t = 0:0.01:10;
x = sin(t) + 0.2*sin(3*t) + 0.1*randn(size(t));
[peaks, locs] = findpeaks(x, 'MinPeakHeight', 0.5, 'MinPeakDistance', 30);
figure
plot(t, x, 'b'); hold on;
plot(t(locs), peaks, 'rv', 'MarkerSize', 10);
xlabel('time'); ylabel('amplitude');
title(sprintf('Found %d peaks', length(peaks)));
grid on;Python output (converter-generated)19 lines · 2 flags
import numpy as np
import scipy.signal as signal
import matplotlib.pyplot as plt
# Detect peaks in a noisy signal
t = np.arange(0, 10 + 0.01, 0.01)
x = np.sin(t) + 0.2*np.sin(3*t) + 0.1*np.random.randn(t.shape)
peaks, locs = signal.find_peaks(x, 'MinPeakHeight', 0.5, 'MinPeakDistance', 30)
plt.figure()
plt.plot(t, x, 'b')
# hold on removed — matplotlib accumulates plots by default
plt.plot(t[locs - 1], peaks, 'rv', markersize=10)
plt.xlabel('time')
plt.ylabel('amplitude')
plt.title('Found %d peaks' % (np.max(peaks.shape)),)
plt.grid(True)
Converter flags (2)
- TOOLBOXLine 5: findpeaks → signal.find_peaks — MATLAB findpeaks returns [peaks, locs]. scipy.signal.find_peaks returns (peak_indices, properties). Get values with x[peaks_idx]. Use height= instead of MinPeakHeight.
- TOOLBOXLine 5: findpeaks → signal.find_peaks — returns (peaks_idx, properties) not (peaks_val, locs). Use x[peaks_idx] for values.
Implementation notes
scipy.signal.find_peaks uses keyword args instead of name-value pairs — the converter handles the rename automatically.Try it on your own MATLAB
Free for 50 lines. Same converter that produced the Python above.
More examples like this, once a week
New canonical conversions and release notes from the converter. One email, no spam.