Bird
Raised Fist0
Signal Processingdata~20 mins

Pitch detection algorithm in Signal Processing - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
Pitch Detection Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
Output of autocorrelation pitch detection snippet
Given the following Python code snippet that computes the autocorrelation of a signal segment to detect pitch, what is the output array?
Signal Processing
import numpy as np
signal = np.array([0, 1, 0, -1, 0, 1, 0, -1])
autocorr = np.correlate(signal, signal, mode='full')
result = autocorr[autocorr.size // 2:]
print(result)
A[4 0 -2 0 1 0 0 0]
B[4 0 -3 0 2 0 -1 0]
C[4 0 -2 0 0 0 0 0]
D[4 0 -3 0 1 0 -1 0]
Attempts:
2 left
๐Ÿ’ก Hint
Recall that autocorrelation at lag 0 is the sum of squares of the signal values.
โ“ data_output
intermediate
1:00remaining
Number of detected pitch periods in a signal
A pitch detection algorithm segments a 1-second audio sampled at 8000 Hz into frames of 80 samples each. If the algorithm detects a pitch period every frame, how many pitch periods are detected in total?
A100
B160
C125
D80
Attempts:
2 left
๐Ÿ’ก Hint
Divide total samples by frame size to find number of frames.
โ“ visualization
advanced
3:00remaining
Identify correct pitch contour plot
Which plot correctly shows a pitch contour that rises linearly from 100 Hz to 200 Hz over 1 second?
Signal Processing
import matplotlib.pyplot as plt
import numpy as np

time = np.linspace(0, 1, 100)
contour_A = 100 + 100 * time
contour_B = 200 - 100 * time
contour_C = 100 * np.sin(2 * np.pi * time) + 150
contour_D = np.full_like(time, 150)

plt.figure(figsize=(8, 4))
plt.plot(time, contour_A, label='A')
plt.plot(time, contour_B, label='B')
plt.plot(time, contour_C, label='C')
plt.plot(time, contour_D, label='D')
plt.legend()
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
plt.title('Pitch Contours')
plt.show()
APlot B: frequency decreases linearly from 200 to 100 Hz
BPlot C: frequency oscillates sinusoidally around 150 Hz
CPlot A: frequency increases linearly from 100 to 200 Hz
DPlot D: frequency remains constant at 150 Hz
Attempts:
2 left
๐Ÿ’ก Hint
Look for a straight line increasing from 100 to 200 Hz.
๐Ÿง  Conceptual
advanced
1:30remaining
Effect of window size on pitch detection accuracy
How does increasing the window size in a pitch detection algorithm typically affect the accuracy and time resolution of pitch estimation?
ADecreases accuracy and decreases time resolution
BIncreases accuracy and increases time resolution
CDecreases accuracy but increases time resolution
DIncreases accuracy but decreases time resolution
Attempts:
2 left
๐Ÿ’ก Hint
Think about the trade-off between frequency precision and temporal precision.
๐Ÿ”ง Debug
expert
2:00remaining
Identify error in pitch detection code snippet
What error will the following Python code raise when trying to detect pitch using zero crossing rate?
Signal Processing
import numpy as np
signal = np.array([1, -1, 1, -1, 1])
zero_crossings = np.where(np.diff(np.sign(signal)))[0]
pitch_period = 1 / zero_crossings
print(pitch_period)
ARuntimeWarning: divide by zero encountered in true_divide
BTypeError
CZeroDivisionError
DNo error, prints pitch periods
Attempts:
2 left
๐Ÿ’ก Hint
Check what happens if zero_crossings contains zero values.

Practice

(1/5)
1. What is the main goal of a pitch detection algorithm in signal processing?
easy
A. To find the main frequency or tone in a sound signal
B. To remove noise from a sound signal
C. To increase the volume of a sound signal
D. To convert sound into text

Solution

  1. Step 1: Understand pitch detection purpose

    Pitch detection algorithms analyze sound to find the fundamental frequency or main tone.
  2. Step 2: Compare options to definition

    Only To find the main frequency or tone in a sound signal describes finding the main frequency, which matches pitch detection's goal.
  3. Final Answer:

    To find the main frequency or tone in a sound signal -> Option A
  4. Quick Check:

    Pitch detection = main frequency [OK]
Hint: Pitch detection finds the main tone frequency [OK]
Common Mistakes:
  • Confusing pitch detection with noise removal
  • Thinking pitch detection changes volume
  • Mixing pitch detection with speech-to-text
2. Which of the following is the correct Python syntax to compute the autocorrelation of a signal array x using numpy?
easy
A. np.autocorr(x)
B. np.correlate(x, x, mode='full')
C. np.correlation(x, x)
D. np.correlate(x)

Solution

  1. Step 1: Recall numpy autocorrelation syntax

    Autocorrelation uses np.correlate with the same array twice and mode='full' for full result.
  2. Step 2: Check options for correct function and parameters

    Only np.correlate(x, x, mode='full') uses np.correlate(x, x, mode='full'), which is correct syntax.
  3. Final Answer:

    np.correlate(x, x, mode='full') -> Option B
  4. Quick Check:

    Autocorrelation = np.correlate(x, x, 'full') [OK]
Hint: Use np.correlate(x, x, mode='full') for autocorrelation [OK]
Common Mistakes:
  • Using non-existent function np.autocorr
  • Missing second argument in np.correlate
  • Wrong function name like np.correlation
3. Given the following Python code snippet for pitch detection using autocorrelation, what is the output of pitch_period?
import numpy as np
x = np.array([0, 1, 0, -1, 0, 1, 0, -1])
correlation = np.correlate(x, x, mode='full')
mid = len(correlation) // 2
pitch_period = np.argmax(correlation[mid+1:]) + 1
print(pitch_period)
medium
A. 2
B. 3
C. 1
D. 4

Solution

  1. Step 1: Understand autocorrelation output length and midpoint

    Input length is 8, so correlation length is 15, midpoint at index 7.
  2. Step 2: Find max correlation lag after midpoint

    Correlation peaks at lag 4 (pattern repeats every 4 samples), so np.argmax(correlation[8:]) + 1 = 4.
  3. Final Answer:

    4 -> Option D
  4. Quick Check:

    Pitch period = 4 samples [OK]
Hint: Pitch period = lag with highest autocorrelation after zero lag [OK]
Common Mistakes:
  • Choosing zero lag as pitch period
  • Off-by-one error in indexing
  • Misunderstanding correlation midpoint
4. The following code attempts to find the pitch period but gives incorrect results. What is the main error?
import numpy as np
x = np.array([0, 1, 0, -1, 0, 1, 0, -1])
correlation = np.correlate(x, x, mode='full')
pitch_period = np.argmax(correlation)  # Incorrect line
print(pitch_period)
medium
A. It should use np.min instead of np.argmax
B. It uses the wrong input array for correlation
C. It finds the max correlation over full range including negative lags
D. It misses normalizing the correlation

Solution

  1. Step 1: Analyze np.argmax usage on full correlation

    Using np.argmax on full correlation finds max at zero lag (center), not pitch period lag.
  2. Step 2: Correct approach is to find max after midpoint

    Pitch period is lag > 0 with max correlation, so search only after midpoint.
  3. Final Answer:

    It finds the max correlation over full range including negative lags -> Option C
  4. Quick Check:

    Max correlation at zero lag is not pitch period [OK]
Hint: Search max correlation only after zero lag index [OK]
Common Mistakes:
  • Taking max correlation at zero lag as pitch
  • Ignoring lag indexing after midpoint
  • Confusing normalization with lag selection
5. You have a recorded audio signal sampled at 8000 Hz. Using autocorrelation, you find the pitch period is 160 samples. What is the pitch frequency in Hz? How would you calculate it?
hard
A. 50 Hz, calculated as sample_rate / pitch_period
B. 160 Hz, calculated as pitch_period / sample_rate
C. 8000 Hz, the sample rate itself
D. 0.02 Hz, calculated as pitch_period * sample_rate

Solution

  1. Step 1: Understand pitch frequency formula

    Pitch frequency = sample rate divided by pitch period (in samples).
  2. Step 2: Calculate pitch frequency

    Pitch frequency = 8000 / 160 = 50 Hz.
  3. Final Answer:

    50 Hz, calculated as sample_rate / pitch_period -> Option A
  4. Quick Check:

    Frequency = 8000 / 160 = 50 Hz [OK]
Hint: Pitch frequency = sample rate รท pitch period [OK]
Common Mistakes:
  • Dividing pitch period by sample rate instead
  • Using sample rate as frequency directly
  • Multiplying pitch period by sample rate
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