Bird
Raised Fist0
Signal Processingdata~10 mins

Pitch detection algorithm in Signal Processing - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Pitch detection algorithm
Input audio signal
↓
Preprocessing: normalize, window
↓
Calculate autocorrelation
↓
Find peak in autocorrelation
↓
Estimate pitch frequency
↓
Output pitch value
The algorithm takes an audio signal, processes it, finds repeating patterns using autocorrelation, and estimates the pitch frequency from the strongest repeating pattern.
Execution Sample
Signal Processing
import numpy as np

def pitch_detect(signal, fs):
    corr = np.correlate(signal, signal, mode='full')
    corr = corr[len(corr)//2:]
    peak = np.argmax(corr[fs//500:fs//50]) + fs//500
    pitch = fs / peak
    return pitch
This code calculates the pitch of a signal by autocorrelation and finding the peak corresponding to the fundamental frequency.
Execution Table
StepActionIntermediate ValueResult
1Input signal array[0.1, 0.3, 0.5, 0.3, 0.1]Signal ready
2Calculate full autocorrelation[...large array...]Autocorrelation computed
3Take second half of autocorrelation[1.0, 0.6, 0.2, 0.1, 0.05]Positive lags autocorrelation
4Search peak between lags 16 and 160corr[16:160] = [0.2, 0.1, 0.05,...]Peak index found at lag 24 + offset = 40
5Calculate pitch = fs / peak lagfs=8000, peak=40Pitch = 200 Hz
6Return pitch value200 Hz
💡 Peak lag found at 40, pitch calculated as 8000/40 = 200 Hz, algorithm ends
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
signal[0.1, 0.3, 0.5, 0.3, 0.1][0.1, 0.3, 0.5, 0.3, 0.1][0.1, 0.3, 0.5, 0.3, 0.1][0.1, 0.3, 0.5, 0.3, 0.1][0.1, 0.3, 0.5, 0.3, 0.1][0.1, 0.3, 0.5, 0.3, 0.1]
corrNone[...full autocorr array...][1.0, 0.6, 0.2, 0.1, 0.05][1.0, 0.6, 0.2, 0.1, 0.05][1.0, 0.6, 0.2, 0.1, 0.05][1.0, 0.6, 0.2, 0.1, 0.05]
peakNoneNoneNone404040
pitchNoneNoneNoneNone200200
Key Moments - 3 Insights
Why do we take only the second half of the autocorrelation array?
Because autocorrelation is symmetric, the second half corresponds to positive time lags which represent delays in the signal useful for pitch detection, as shown in execution_table step 3.
Why do we search for the peak only between certain lag values (fs/500 to fs/50)?
This range corresponds to expected pitch frequencies (e.g., 50 Hz to 500 Hz). Searching outside this range avoids false peaks from very short or very long delays, as seen in execution_table step 4.
How does the peak lag relate to the pitch frequency?
Pitch frequency is the sampling rate divided by the lag where the autocorrelation peak occurs, because lag represents the period of the repeating signal pattern, as calculated in execution_table step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the peak lag found?
A40
B4
C8
D1
💡 Hint
Check the 'Result' column in step 4 of execution_table where peak index is given.
According to variable_tracker, what is the pitch value after step 5?
A8000
B0.125
C200
D8
💡 Hint
Look at the 'pitch' row under 'After Step 5' in variable_tracker.
If the sampling frequency fs was doubled, how would the pitch value change at step 5?
AIt would halve
BIt would double
CIt would stay the same
DIt would become zero
💡 Hint
Pitch = fs / peak lag, so increasing fs increases pitch proportionally (see execution_table step 5).
Concept Snapshot
Pitch detection algorithm:
- Input: audio signal and sampling rate
- Compute autocorrelation of signal
- Use positive lags only
- Find peak in expected lag range
- Pitch = sampling rate / peak lag
- Output pitch frequency in Hz
Full Transcript
This pitch detection algorithm starts by taking an audio signal and its sampling frequency. It calculates the autocorrelation of the signal to find repeating patterns. Because autocorrelation is symmetric, only the second half (positive lags) is used. The algorithm searches for the highest peak in a lag range corresponding to typical pitch frequencies. The lag where the peak occurs represents the period of the fundamental frequency. Dividing the sampling frequency by this lag gives the pitch frequency in Hertz. The algorithm then outputs this pitch value. Variables like the signal, autocorrelation array, peak lag, and pitch frequency change step-by-step as shown in the execution table and variable tracker. Key points include why only positive lags are used, why the search range is limited, and how lag relates to pitch frequency.

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