Take the 2-minute tour ×
Signal Processing Stack Exchange is a question and answer site for practitioners of the art and science of signal, image and video processing. It's 100% free, no registration required.

I'm working on a simple web app that allows the user to tune his/her guitar. I'm a real beginner in signal processing, so don't judge too hard if my question is inappropriate.

So, I managed to get the fundamental frequency using a FFT algorithm and at this point the application is somehow functional. However, there is room for improvement, right now I send raw pcm to the FFT algorithm, but I was thinking that maybe there are some pre/post algorithms/filters that may improve the detection. Can you suggest any?

My main problem is that when it detects a certain frequency it shows that frequency for 1-2sec and then jumps to other random frequencies and comes back again and so on, even if the sound is continuous.

I'm also interested in any other type of optimization if one has experience with such things.

share|improve this question

2 Answers 2

up vote 13 down vote accepted

I'm guessing the other frequencies it gets are harmonics of the fundamental? Like you're playing 100 Hz and it picks out 200 Hz or 300 Hz instead? First, you should limit your search space to the frequencies that a guitar is likely to be. Find the highest fundamental you're likely to need and limit to that.

Autocorrelation will work better than FFT at finding the fundamental, if the fundamental is lower in amplitude than the harmonics (or missing altogether, but that's not an issue with guitar):

enter image description here

You can also try weighting the lower frequencies to emphasize the fundamental and minimize harmonics, or use a peak-picking algorithm like this and then just choose the lowest in frequency.

Also, you should be windowing your signal before applying the FFT. You just multiply it by a window function, which tapers off the beginning and end of the waveform to make the frequency spectrum cleaner. Then you get tall narrow spikes for frequency components instead of broad ones.

You can also use interpolation to get a more accurate peak. Take the log of the spectrum, then fit a parabola to the peak and the two neighboring points, and find the parabola's true peak. You might not need this much accuracy, though.

Here is my example Python code for all of this.

share|improve this answer
    
This is what I was looking for, very good answer, thank you! –  Valentin Radu Oct 13 '11 at 13:54
2  
Multiplying by a window function that is tapered will actually smear out any spectral lines in your signal, thereby making them broader. What it can buy you, though, is dynamic range, allowing you to identify, for example, a very low-power spectral line in the presence of a high-power interfering tone. –  Jason R Oct 13 '11 at 14:10
    
@JasonR given the fact that this is designed to work in an environment in which the probability of a high-power interfering tone(s) is really low, do you suggest that is better not to use a Hamming window? –  Valentin Radu Oct 13 '11 at 14:32
1  
I can confirm that using a Hamming window got me closer to my goal of keeping the readings steady. Right now, when I play an A4 I get 440 Hz most of the time and only very rare I get a close reading like 650 Hz or so. I'm guessing those are harmonics? Also, I couldn't help notice that for higher frequency the app works flawless and that for lower it starts to fail. Probably because I'm using FTT to detect the peak magnitude frequency bin and for lower frequencies thats not always the fundamental? –  Valentin Radu Oct 13 '11 at 15:53
1  
@mindnoise: 660 Hz is not a harmonic of 440 Hz, but it is a harmonic of 220 Hz, or a perfect fifth above 440. Could be another string resonating or distortion or something? It's a lot easier to figure out issues like this if you can plot the FFT and look at it. Yes, the low frequencies might be filtered and reduced relative to the higher ones, either by mechanical effects or by your analog circuitry. –  endolith Oct 13 '11 at 15:56

Pitch is not the same as peak magnitude frequency bin of an FFT. Pitch is a human psycho-acoustic phenomena. The pitch sound could have a missing or very weak fundamental (common in some voice, piano and guitar sounds) and/or lots of powerful overtones in its spectrum that overwhelm the pitch frequency (but still be heard as that pitch note by a human). So any FFT peak frequency detector (even including some windowing and interpolation) will not be a robust method of pitch estimation.

This stackoverflow question includes a list of some alternate methods of estimating pitch that might produce better results.

ADDED: If you are doing this for guitar sounds, note that the lowest guitar strings may actually produce slightly inharmonic overtones, making pitch estimation even more difficult, as the human ear may hear a pitch frequency more closely related to a sub-multiple of one of the overtones, rather than to the actual fundamental vibration frequency of the string.

ADDED #2: This gets 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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.