Bird
Raised Fist0
SciPydata~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the find_peaks function in scipy do?
easy
A. It finds local maxima (peaks) in a 1D array of data.
B. It sorts the data in ascending order.
C. It calculates the average value of the data.
D. It removes all negative values from the data.

Solution

  1. Step 1: Understand the purpose of find_peaks

    The function is designed to identify points in data where values are higher than their neighbors, called peaks.
  2. Step 2: Compare options with function behavior

    Only It finds local maxima (peaks) in a 1D array of data. correctly describes this behavior; others describe unrelated operations.
  3. Final Answer:

    It finds local maxima (peaks) in a 1D array of data. -> Option A
  4. Quick Check:

    find_peaks locates peaks = D [OK]
Hint: Remember: find_peaks finds highs, not sorting or averaging [OK]
Common Mistakes:
  • Confusing peak finding with sorting data
  • Thinking it calculates statistics like mean
  • Assuming it filters values instead of locating peaks
2. Which of the following is the correct way to import and use find_peaks from scipy.signal?
easy
A. import find_peaks from scipy.signal peaks = find_peaks(data)
B. from scipy.signal import find_peaks peaks = find_peaks(data)
C. from scipy import find_peaks peaks = find_peaks(data)
D. import scipy.signal.find_peaks peaks = find_peaks(data)

Solution

  1. Step 1: Recall correct import syntax in Python

    Python uses 'from module import function' to import specific functions.
  2. Step 2: Match syntax with options

    from scipy.signal import find_peaks peaks = find_peaks(data) uses correct syntax. Options A, C, and D have invalid import statements.
  3. Final Answer:

    from scipy.signal import find_peaks\npeaks = find_peaks(data) -> Option B
  4. Quick Check:

    Correct import syntax = B [OK]
Hint: Use 'from module import function' for specific imports [OK]
Common Mistakes:
  • Using 'import function from module' which is invalid
  • Trying to import from wrong module path
  • Using dot notation in import statement incorrectly
3. Given the code below, what will be the output of peaks?
import numpy as np
from scipy.signal import find_peaks

data = np.array([0, 2, 1, 3, 0, 1, 2, 1])
peaks, _ = find_peaks(data, height=2)
print(peaks)
medium
A. [3]
B. [1 3]
C. [1 3 6]
D. [2 5]

Solution

  1. Step 1: Identify peaks in the data array

    Data values: [0, 2, 1, 3, 0, 1, 2, 1]. Peaks are points higher than neighbors: indices 1 (2), 3 (3), and 6 (2).
  2. Step 2: Apply height=2 filter

    Peaks with height >= 2 are kept. Index 1 (2 >= 2), 3 (3 >= 2), 6 (2 >= 2), so indices 1, 3, 6 remain.
  3. Final Answer:

    [1 3 6] -> Option C
  4. Quick Check:

    Peaks with height >= 2 = [1 3 6] [OK]
Hint: Check peak values against height filter carefully [OK]
Common Mistakes:
  • Thinking height filter requires strictly greater than threshold (it's >=)
  • Misidentifying peak positions
  • Ignoring the height parameter
4. The following code is intended to find peaks at least 3 units apart, but it returns more peaks than expected. What is the error?
import numpy as np
from scipy.signal import find_peaks

data = np.array([1, 3, 2, 4, 3, 5, 2])
peaks = find_peaks(data, 3)
print(peaks)
medium
A. The distance parameter is not applied because it should be passed as a keyword argument.
B. The distance parameter is applied correctly; the issue is with the data array.
C. The code is missing parentheses around the print argument.
D. The find_peaks function does not support the distance parameter.

Solution

  1. Step 1: Check how distance parameter is passed

    In find_peaks, distance must be passed as a keyword argument, e.g., distance=3.
  2. Step 2: Analyze the code

    The code passes 3 positionally after data, which sets height=3 instead of distance=3. Thus distance is not applied, returning all local peaks [1,3,5] instead of filtered [1,5].
  3. Step 3: Verify output type

    find_peaks returns a tuple (indices, properties). Printing peaks directly prints the tuple, which may confuse.
  4. Final Answer:

    The distance parameter is not applied because it should be passed as a keyword argument. -> Option A
  5. Quick Check:

    distance must be keyword argument = C [OK]
Hint: Always pass distance as keyword argument: distance=number [OK]
Common Mistakes:
  • Passing distance as positional argument
  • Expecting find_peaks to return only indices
  • Misunderstanding output format of find_peaks
5. You have noisy sensor data and want to find peaks that are at least 5 units high and at least 10 samples apart. Which find_peaks call correctly applies these conditions?
import numpy as np
from scipy.signal import find_peaks

sensor_data = np.random.rand(100) * 10

# Which call is correct?
hard
A. peaks = find_peaks(sensor_data, height=(5, None), distance=10)
B. peaks = find_peaks(sensor_data, height=5, distance=10)
C. peaks, _ = find_peaks(sensor_data, min_height=5, min_distance=10)
D. peaks, _ = find_peaks(sensor_data, height=5, distance=10)

Solution

  1. Step 1: Understand parameter usage in find_peaks

    Height is specified with 'height=5' to find peaks at least 5 units high. Distance is specified with 'distance=10' to ensure peaks are at least 10 samples apart.
  2. Step 2: Evaluate each option

    peaks, _ = find_peaks(sensor_data, height=5, distance=10) correctly uses height and distance parameters and unpacks the tuple output. peaks = find_peaks(sensor_data, height=5, distance=10) misses unpacking. peaks, _ = find_peaks(sensor_data, min_height=5, min_distance=10) uses invalid parameter names. peaks = find_peaks(sensor_data, height=(5, None), distance=10) uses a tuple for height which is valid but unnecessary here.
  3. Final Answer:

    peaks, _ = find_peaks(sensor_data, height=5, distance=10) -> Option D
  4. Quick Check:

    Use height and distance keywords, unpack output = A [OK]
Hint: Use height= and distance= with unpacking for correct results [OK]
Common Mistakes:
  • Using wrong parameter names like min_height
  • Not unpacking the tuple returned by find_peaks
  • Passing height as a tuple unnecessarily
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard