<p>I have a sample held in a buffer from DirectX. It's a sample of a note played and captured from an instrument. How do I analyse the frequency of the sample (like a guitar tuner does)? I believe FFTs are involved, but I have no pointers to HOWTOs.</p>

[{'answer_id': 65318, 'author': 'Daniel Papasian', 'author_id': 7548, 'author_profile': 'https://Stackoverflow.com/users/7548', 'pm_score': 3, 'selected': False, 'text': "<p>FFTs (Fast-Fourier Transforms) would indeed be involved.  FFTs allow you to approximate any analog signal with a sum of simple sine waves of fixed frequencies and varying amplitudes.  What you'll essentially be doing is taking a sample and decomposing it into amplitude->frequency pairs, and then taking the frequency that corresponds to the highest amplitude.</p>\n\n<p>Hopefully another SO reader can fill the gaps I'm leaving between the theory and the code!  </p>\n"}, {'answer_id': 65331, 'author': 'Serafina Brocious', 'author_id': 4977, 'author_profile': 'https://Stackoverflow.com/users/4977', 'pm_score': 1, 'selected': False, 'text': '<p>Apply a <a href="http://en.wikipedia.org/wiki/Discrete_Fourier_transform" rel="nofollow noreferrer">DFT</a> and then derive the fundamental frequency from the results.  Googling around for DFT information will give you the information you need -- I\'d link you to some, but they differ greatly in expectations of math knowledge.</p>\n\n<p>Good luck.</p>\n'}, {'answer_id': 65459, 'author': 'Mendelt', 'author_id': 3320, 'author_profile': 'https://Stackoverflow.com/users/3320', 'pm_score': 3, 'selected': False, 'text': "<p>Guitar tuners don't use FFT's or DFT's. Usually they just count zero crossings. You might not get the fundamental frequency because some waveforms have more zero crossings than others but you can usually get a multiple of the fundamental frequency that way. That's enough to get the note although you might be one or more octaves off.</p>\n\n<p>Low pass filtering before counting zero crossings can usually get rid of the excess zero crossings. Tuning the low pass filter requires some knowlegde of the range of frequency you want to detect though</p>\n"}, {'answer_id': 77946, 'author': 'AShelly', 'author_id': 10396, 'author_profile': 'https://Stackoverflow.com/users/10396', 'pm_score': 2, 'selected': False, 'text': "<p>A little more specifically: </p>\n\n<p>If you start with the raw PCM in an input array, what you basically have is a graph of wave amplitude vs time.Doing a FFT will transform that to a frequency histogram for frequencies from 0 to 1/2 the input sampling rate.  The value of each entry in the result array will be the 'strength' of the corresponding sub-frequency.</p>\n\n<p>So to find the root frequency given an input array of size N sampled at S samples/second:</p>\n\n<pre><code>FFT(N, input, output);\nmax = max_i = 0;\nfor(i=0;i&lt;N;i++)\n  if (output[i]&gt;max) max_i = i;\nroot = S/2.0 * max_i/N ;\n</code></pre>\n"}, {'answer_id': 99515, 'author': 'moltenform', 'author_id': 18506, 'author_profile': 'https://Stackoverflow.com/users/18506', 'pm_score': 4, 'selected': False, 'text': '<p>There are also other algorithms that are time-based, not frequency based.\nAutocorrelation is a relatively simple algorithm for pitch detection.\nReference: <a href="http://cnx.org/content/m11714/latest/" rel="noreferrer">http://cnx.org/content/m11714/latest/</a></p>\n\n<p>I have written c# implementations of autocorrelation and other algorithms that are readable. Check out <a href="http://code.google.com/p/yaalp/" rel="noreferrer">http://code.google.com/p/yaalp/</a>.</p>\n\n<p><a href="http://code.google.com/p/yaalp/source/browse/#svn/trunk/csaudio/WaveAudio/WaveAudio" rel="noreferrer">http://code.google.com/p/yaalp/source/browse/#svn/trunk/csaudio/WaveAudio/WaveAudio</a>\nLists the files, and PitchDetection.cs is the one you want.</p>\n\n<p>(The project is GPL; so understand the terms if you use the code).</p>\n'}, {'answer_id': 1127679, 'author': 'endolith', 'author_id': 125507, 'author_profile': 'https://Stackoverflow.com/users/125507', 'pm_score': 5, 'selected': False, 'text': '<p>The FFT can help you figure out where the frequency is, but it can\'t tell you <em>exactly</em> what the frequency is.  Each point in the FFT is a &quot;bin&quot; of frequencies, so if there\'s a peak in your FFT, all you know is that the frequency you want is somewhere within that bin, or range of frequencies.</p>\n<p>If you want it really accurate, you need a long FFT with a high resolution and lots of bins (= lots of memory and lots of computation).  You can also guess the true peak from a low-resolution FFT using <a href="https://ccrma.stanford.edu/%7Ejos/sasp/Quadratic_Interpolation_Spectral_Peaks.html" rel="nofollow noreferrer">quadratic interpolation</a> on the log-scaled spectrum, which works surprisingly well.</p>\n<p>If computational cost is most important, you can try to get the signal into a form in which you can count zero crossings, and then the more you count, the more accurate your measurement.</p>\n<p>None of these will work if the <a href="http://en.wikipedia.org/wiki/Missing_fundamental" rel="nofollow noreferrer">fundamental is missing</a>, though.  :)</p>\n<p>I\'ve outlined a few different algorithms <a href="https://gist.github.com/endolith/255291" rel="nofollow noreferrer">here</a>, and the interpolated FFT is usually the most accurate (though this only works <a href="https://www.flickr.com/photos/omegatron/4180172061/" rel="nofollow noreferrer">when the fundamental is the strongest harmonic</a> - otherwise you need to be smarter about finding it), with zero-crossings a close second (though this only works <a href="https://www.flickr.com/photos/omegatron/4180888094/" rel="nofollow noreferrer">for waveforms with one crossing per cycle</a>).  Neither of these conditions is typical.</p>\n<p>Keep in mind that the partials above the fundamental frequency are <strong>not</strong> perfect harmonics in many instruments, like piano or guitar.  Each partial is <a href="https://www.flickr.com/photos/omegatron/4166565758/" rel="nofollow noreferrer">actually a little bit out of tune</a>, or <a href="http://en.wikipedia.org/wiki/Inharmonicity" rel="nofollow noreferrer"><em>inharmonic</em></a>.  So the higher-frequency peaks in the FFT will not be exactly on the integer multiples of the fundamental, and the wave shape will change slightly from one cycle to the next, which throws off autocorrelation.</p>\n<p>To get a really accurate frequency reading, I\'d say to use the autocorrelation to guess the fundamental, then find the true peak using quadratic interpolation.  (You can do the autocorrelation in the frequency domain to save CPU cycles.)  There are a lot of gotchas, and the right method to use really depends on your application.</p>\n'}, {'answer_id': 1131215, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': "<p>Retrieval of fundamental frequencies in a PCM audio signal is a difficult task, and there would be a lot to talk about it...</p>\n\n<p>Anyway, usually time-based method are not suitable for polyphonic signals, because a complex wave given by the sum of different harmonic components due to multiple fundamental frequencies has a zero-crossing rate which depends only from the lowest frequency component...\nAlso in the frequency domain the FFT is not the most suitable method, since frequency spacing between notes follow an exponential scale, not linear. This means that a constant frequency resolution, used in the FFT method, may be insufficient to resolve lower frequency notes if the size of the analysis window in the time domain is not large enough.</p>\n\n<p>A more suitable method would be a constant-Q transform, which is DFT applied after a process of low-pass filtering and decimation by 2 (i.e. halving each step the sampling frequency) of the signal, in order to obtain different subbands with different frequency resolution. In this way the calculation of DFT is optimized. The trouble is that also time resolution is variable, and increases for the lower subbands...</p>\n\n<p>Finally, if we are trying to estimate the fundamental frequency of a single note, FFT/DFT methods are ok. Things change for a polyphonic context, in which partials of different sounds overlap and sum/cancel their amplitude depending from their phase difference, and so a single spectral peak could belong to different harmonic contents (belonging to different notes). Correlation in this case don't give good results...</p>\n"}]
['https://Stackoverflow.com/questions/65268', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']