Pitch Detection by Harmonics

Matlab. This is my pitch detection algorithm. It takes mono wav. file as an input, and checks its fundamental and next three harmonics. Firstly, a threshold value is determined after finding the maximum peak by FFT. Based on magnitude spectrum in log scale, it chooses %70 of  max peak as a threshold value to filter. It can be changed if needed. The algorithm replaces the  amplitude values with zeros under the threshold. Then it determines the bandwidth value for the bands. After finding max peaks for each band, it converts those values from frequency bin to frequency Hz. Finally, it checks consistency for harmonics with the fundamental frequency  in the range of %5 (higher or lower) and takes the mean value in the frequency list. If the harmonic is away from the fundamental, the algorithm excludes it.

It is basically designed for single note pitches. When the sound is not clear, complex, or does not have any clear body in the musical frequency range, it gives not detectable error. If an orchestral segment is used and some notes are dominant there (particularly low range), it might give very low frequency as a result.

I used many mono files to see how it works. It works very well in general. As an example, I would like to use a strings fragment here. It finds frequencies in order (the first one is fundamental): approximately 1161.09, 1164.62, 1161.17, 1178. The final value is 1166.22 Hz. This violin ensemble played D6 in this segment.

[1] Pitch Frequency List, The University of Virginia

[2] Spectral Estimation, Matlab Documentation