0
0
SciPydata~20 mins

Peak finding (find_peaks) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Peak Finder Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Find peaks in a simple signal

What is the output of the following code that finds peaks in a signal array?

SciPy
import numpy as np
from scipy.signal import find_peaks

signal = np.array([0, 2, 1, 3, 0, 1, 2, 1, 0])
peaks, _ = find_peaks(signal)
print(peaks.tolist())
A[1, 3, 5, 6]
B[0, 3, 6]
C[2, 4, 7]
D[1, 3, 6]
Attempts:
2 left
💡 Hint

Peaks are points higher than their immediate neighbors.

data_output
intermediate
2:00remaining
Count peaks with minimum height

Given the signal array, how many peaks have a height of at least 3?

SciPy
import numpy as np
from scipy.signal import find_peaks

signal = np.array([1, 3, 7, 1, 2, 6, 0, 1])
peaks, properties = find_peaks(signal, height=3)
print(len(peaks))
A3
B2
C4
D1
Attempts:
2 left
💡 Hint

Check which peaks have values >= 3.

visualization
advanced
3:00remaining
Visualize peaks with minimum distance

Which plot correctly shows peaks found with a minimum distance of 3 between them?

SciPy
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks

signal = np.array([0, 2, 1, 3, 0, 1, 2, 1, 0])
peaks, _ = find_peaks(signal, distance=3)
plt.plot(signal)
plt.plot(peaks, signal[peaks], "x")
plt.show()
APlot with peaks marked at indices 0, 3, and 6
BPlot with peaks marked at indices 1, 3, and 6
CPlot with peaks marked at indices 1 and 6 only
DPlot with peaks marked at indices 3 and 6 only
Attempts:
2 left
💡 Hint

Minimum distance means peaks must be at least 3 indices apart.

🔧 Debug
advanced
2:00remaining
Identify error in peak finding code

What error does the following code produce?

SciPy
import numpy as np
from scipy.signal import find_peaks

signal = [1, 2, 3, 2, 1]
peaks, properties = find_peaks(signal, height='high')
AValueError: height must be a number or tuple of numbers
BNo error, returns peaks at indices [2]
CSyntaxError: invalid syntax
DTypeError: '>' not supported between instances of 'str' and 'int'
Attempts:
2 left
💡 Hint

Check the type of the height parameter.

🚀 Application
expert
3:00remaining
Find peaks with multiple conditions

Which option correctly finds peaks in the signal that have a height of at least 2 and a prominence of at least 1?

SciPy
import numpy as np
from scipy.signal import find_peaks

signal = np.array([0, 2, 1, 3, 0, 1, 2, 1, 0])
peaks, properties = find_peaks(signal, height=2, prominence=1)
print(peaks.tolist())
A[3, 6]
B[1, 3]
C[1, 3, 6]
D[1, 6]
Attempts:
2 left
💡 Hint

Prominence measures how much a peak stands out from its surroundings.