# Real-Time Human Pitch Detection

I'm trying to implement a singing game that will analise raw mic input and tell the player how good is he singing. That needs to be done in real-time.

I've come across a lot of threads asking the same question but I'm still quite not done with it, probably due to my lack of experience in the field and shallow math background. I've implemented an algorithm based on the DSPDimension website pitch shift's article: http://www.dspdimension.com/admin/pitch-shifting-using-the-ft/

I extract the true frequency and magnitude just like the article explains, but I don't know find the fundamental frequency with this. I've tried to get the bin with greatest magnitude but that only give me right results for higher pitch signals, it doesn't matter which oversampling factor I use I still get bad data for low freq signals. Is this approach completely wrong or am I in the right track but just missing something?

EDIT: I forgot to mention that I'm only interested in the pitch class, so it is ok if the fundamental is missing but I have a strong overtone in the sample.

EDIT2: Thanks to everyone, I just finished a version of the algorithm that's working like a charm. The low pitch estimation problem was due to my input test. When I sung the note it matched correctly. Also, I'm considering all harmonics now, not just the highest peak.

• Wikipedia has some information.
– Emre
Commented Mar 26, 2012 at 20:53

I've tried to get the bin with greatest magnitude but that only give me right results for higher pitch signals, it doesn't matter which oversampling factor I use I still get bad data for low freq signals.

That's because the harmonics are larger than the fundamental. Plot your spectrum and you'll see. A better method to find the true fundamental is autocorrelation. Then you're "sliding" the waveform past itself and finding delays at which the wave shape lines up with itself.

http://www.phon.ucl.ac.uk/courses/spsci/matlab/lect10.html

Do you really want them to sing the exact note, or is it ok if they sing an octave above or below depending on their voice register?

• You're right, I forgot to mention that I'm interested only in the pitch class. I'm using this website to test my tool: seventhstring.com/tuningfork/tuningfork.html. For the input of A(220Hz) it returns E(660Hz) as the found pitch class. I took a look at the sprectum and 220Hz is indeed there, but with a lesser magnitude than 660Hz. After filtering out values below a minimum magnitude and cap frequencies in my desired range, the sprectum I get from this have 4 peaks. [peak, mag] = [220, 0.0203], [618, 0.0142], [660, 0.0668], [703, 0.0497]. Commented Mar 27, 2012 at 12:33
• I just got me thinking that perhaps I should take the phase offset into account while computing the magnitude, just like I'm doing to get the true frequency. Does that make sense? What I mean is that, if I have a phase offset of roughly 90º for a bin, the "peak" would be at 0 magnitude wouldn't it? Commented Mar 27, 2012 at 12:39
• @elipedrl: So you're essentially writing a guitar tuner. :) As I understand, they low-pass filter to clean up the wave shape and then count peaks to get the pitch. electronicdesign.com/article/articles/… aboutmicrocontroller.blogspot.com/2008/04/… There are better ways, though, if you're going for accuracy rather than cheapness gist.github.com/255291 Commented Mar 27, 2012 at 14:00
• @elipedrl: The phase offset for a bin should be irrelevant to the pitch. Each bin is a complex number, and you're interested in the absolute value or magnitude of that number. en.wikipedia.org/wiki/Absolute_value#Complex_numbers Commented Mar 27, 2012 at 14:03
• and if you happen to have 2 shorter FFTs for some reason (latency, time quanta, etc.), a phase vocoder calculation is less computation than doing yet another longer FFT and interpolating that. Commented Mar 27, 2012 at 18:28

Yes, using a peak frequency estimator for pitch is wrong. Pitch is a psychoacoustic phenomena, so pitch detection or estimation is different from frequency estimation. There have been plenty of pitch estimation methods given in previous answers to similar questions here. There's more than 1 to choose from.

ADDED #1: Questions similar to this get asked so often that I wrote up a longer blog post on the topic: http://www.musingpaw.com/2012/04/musical-pitch-is-not-just-fft-frequency.html

• I updated the question with the information that I'm only interested in the pitch class. I really hope that FFT with a post processing is enough for this, I'm way behind my schedule and changing the approach would be awful for me. Commented Mar 27, 2012 at 12:46
• @elipedrl: FFT should work then. Getting several peaks and then smartly selecting one of them should be good enough. Remember the valid peaks will be close to (but not exactly) integer multiples of the fundamental, while spurious peaks will not. You have to avoid selecting spurious peaks and avoid selecting 3rd harmonic, etc. that aren't an octave away from the note you're looking for. Commented Mar 27, 2012 at 14:05
• It is possible, although perhaps unlikely, for no frequency peak to be at the musical pitch frequency. Some male vowels can be close to this, only high overtones left after filtering by the vowel formant. Commented Mar 27, 2012 at 16:00
• The Harmonic Product Spectrum method may be suitable for finding a LCD lowest common denominator estimate of a group of spectral peaks, by post processing the initial FFT results. Commented Mar 27, 2012 at 16:10