The YIN algorithm is a pitch detection method that accurately estimates the fundamental frequency of a signal. Here's a step-by-step explanation of how the YIN algorithm works:
Step 1: Calculate the difference function
- Take an input buffer of audio samples and calculate the difference function.
- For each time lag τ (tau) from 1 to half the buffer size, calculate the difference between each pair of samples that are τ samples apart.
- Square these differences and sum them up for each τ. This gives you a new buffer (yinBuffer) of size halfBufferSize.
Step 2: Calculate the cumulative mean normalized difference function
- Initialize the first element of yinBuffer to 1.
- For each τ from 1 to halfBufferSize - 1, calculate the cumulative sum of the yinBuffer up to that τ.
- Divide the yinBuffer[τ] by the cumulative sum. This normalizes the difference function by the average of the differences up to that τ.
- If the cumulative sum is zero, set the yinBuffer[τ] to 1 to avoid division by zero.
Step 3: Find the first minimum below a threshold
- Set a threshold value (e.g., 0.1 or 0.15) which represents the allowed deviation from the average difference.
- Start from τ = 2 and search for the first τ where yinBuffer[τ] falls below the threshold.
- Once found, search forward until the next τ where yinBuffer[τ] starts increasing again. This τ represents the fundamental period of the signal.
Step 4: Parabolic interpolation
- If the found τ is at the edge of the yinBuffer (i.e., 0 or halfBufferSize - 1), return -1 as the pitch is undetermined.
- Otherwise, perform parabolic interpolation to refine the τ estimate:
- Take the yinBuffer values at τ-1, τ, and τ+1.
- Fit a parabola through these three points.
- If the parabola has a minimum, adjust τ to the location of that minimum. This gives a more precise estimate of the fundamental period.
Step 5: Calculate the fundamental frequency
- Divide the sample rate by the refined τ estimate.
- This gives you the fundamental frequency in Hz.
The YIN algorithm is robust to noise and can accurately detect pitches even in complex audio signals. The difference function helps identify the periodicity of the signal, while the cumulative mean normalization helps to reduce the impact of amplitude changes. The parabolic interpolation step further refines the pitch estimate to achieve sub-sample accuracy.
By following these steps, the YIN algorithm can determine the fundamental frequency of an audio signal with high precision.
Comments
Post a Comment