I have used FFT algorithm to detect frequency of the sound(guitar pitch) and it works great when I play sinosoidal sound from the computer it will estimate it perfectly but it is not so perfect when i use guitar. How to better estimate the pitch frequency?
double[] spectrum = FourierTransform.Spectrum(ref sampleBuffer);//spectrum contains data from FFT
double frequency = indexOfMax(spectrum) * 16000 / 500; //sampling rate/FFT size
//indexOfMax just finds the index of the maximum element in the array
To generate sinosoidal waves i used: http://www.audiocheck.net/audiofrequencysignalgenerator_sinetone.php and fort this kind of waves just finding maximum from the spectrum array worked great, but I am looking for better way.
For guitar sounds, some possibilities for pitch detection/estimation include using the FFT result as the first step of computing the cepstrum for cepstral analysis, or for harmonic product spectrum analysis, or for doing a fast autocorrelation by IFFT(FFT() * conjugate(FFT())).
Automatic pitch determination for musical instruments is non-trivial.
Take a look at this post on the Cepstrum approach to the problem: Musical instrument Cepstrum