0
0
SciPydata~10 mins

Peak finding (find_peaks) in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Peak finding (find_peaks)
Input: 1D data array
Call find_peaks function
Analyze data points
Identify local maxima
Return indices of peaks
Use peaks for analysis or plotting
The function takes a list of numbers, checks each point to see if it is higher than its neighbors, and returns the positions of these peaks.
Execution Sample
SciPy
import numpy as np
from scipy.signal import find_peaks

data = np.array([1, 3, 2, 5, 4, 6, 1])
peaks, _ = find_peaks(data)
print(peaks)
This code finds the positions of peaks in a simple numeric array.
Execution Table
StepIndexData ValueNeighborsIs Peak?ActionPeaks Found So Far
101N/A (start)NoSkip (no left neighbor)[]
2131 (left), 2 (right)YesAdd index 1 to peaks[1]
3223 (left), 5 (right)NoSkip[1]
4352 (left), 4 (right)YesAdd index 3 to peaks[1, 3]
5445 (left), 6 (right)NoSkip[1, 3]
6564 (left), 1 (right)YesAdd index 5 to peaks[1, 3, 5]
761N/A (end)NoSkip (no right neighbor)[1, 3, 5]
💡 Reached end of data array; all peaks identified.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
peaks[][1][1, 3][1, 3, 5][1, 3, 5]
Key Moments - 3 Insights
Why is the first data point not considered a peak?
Because it has no left neighbor to compare with, so it cannot be a local maximum. See execution_table step 1.
Why does the function skip the last data point?
The last point has no right neighbor, so it cannot be a peak by definition. See execution_table step 7.
How does find_peaks decide if a point is a peak?
It checks if the point is greater than its immediate neighbors. If yes, it adds the index to peaks. See steps 2, 4, and 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'peaks' after step 4?
A[1, 3, 5]
B[1, 3]
C[1]
D[]
💡 Hint
Check the 'Peaks Found So Far' column at step 4.
At which index does the condition 'Is Peak?' become false after being true at index 3?
AIndex 5
BIndex 2
CIndex 4
DIndex 6
💡 Hint
Look at the 'Is Peak?' column in execution_table rows after step 4.
If the data array ended at index 5 instead of 6, how would the peaks list change?
A[1, 3]
B[1, 3, 5]
C[1, 3, 4]
D[3, 5]
💡 Hint
Check variable_tracker and note that index 5 is a peak before the last point.
Concept Snapshot
find_peaks(data)
- Input: 1D numeric array
- Output: indices of local maxima
- Checks if each point > neighbors
- Skips first and last points if no neighbors
- Useful for signal or data analysis
Full Transcript
This visual execution shows how the find_peaks function from scipy.signal works. It takes a list of numbers and checks each point to see if it is higher than its neighbors. The first and last points are skipped because they don't have two neighbors. When a point is higher than both neighbors, its index is added to the peaks list. The example data array is [1, 3, 2, 5, 4, 6, 1]. The function finds peaks at indices 1, 3, and 5. The execution table tracks each step, showing the data value, neighbors, if it is a peak, and the peaks found so far. The variable tracker shows how the peaks list grows. Key moments clarify why edges are skipped and how peaks are identified. The quiz tests understanding of the steps and results. This helps beginners see exactly how peak finding works step-by-step.