4

How do i get frequency using FFT? What's the right procedure and codes?

finnw
  • 45,253
  • 22
  • 134
  • 212
airuslee
  • 41
  • 1
  • 3

5 Answers5

4

Pitch detection typically involves measuring the interval between harmonics in the power spectrum. The power spectrum is obtained form the FFT by taking the magnitude of the first N/2 bins (sqrt(re^2 + im^2)). However there are more sophisticated techniques for pitch detection, such as cepstral analysis, where we take the FFT of the log of the power spectrum, in order to identify periodicity in the spectral peaks.

Paul R
  • 195,989
  • 32
  • 353
  • 519
3

A sustained note of a musical instrument is a periodic signal, and our friend Fourier (the second "F" in "FFT") tells us that any periodic signal can be constructed by adding a set of sine waves (generally with different amplitudes, frequencies, and phases). The fundamental is the lowest frequency component and it corresponds to pitch; the remaining components are overtones and are multiples of the fundamental's frequency. It is the relative mixture of fundamental and overtones that determines timbre, or the character of an instrument. A clarinet and a trumpet playing in unison sound "in tune" because they share the same fundamental frequency, however, they are individually identifiable because of their differing timbre (overtone mixture).

For your problem, you could sample the trumpet over a time window, calculate the FFT (which decomposes the sequence of samples into its constituent digital frequencies), and then assert that the pitch is the frequency of the bin with the greatest magnitude. If you desire, this could then be trivially quantized to the nearest musical half step, like E flat. (Lookup FFT on Wikipedia if you don't understand the relationship between the sampling frequency and the resultant frequency bins, or if you don't understand the detriment of having too low a sampling frequency.) This will probably meet your needs because the fundamental component usually has greater energy than any other component. The longer the window, the greater the pitch accuracy because the bin centers will become more closely spaced in frequency. However, if the window is so long that the trumpet is changing its pitch appreciably over the duration of the window, then the technique's effectiveness will break down considerably.

seanmac7577
  • 158
  • 6
  • Hi! Can you clarify what you mean by: 'this could then be trivially quantized to the nearest musical half step, like E flat.' Wouldnt the frequency->pitch conversion be more like where the resulting peak frequency is closest to a pitch's fundamental frequency? – user488792 Feb 19 '11 at 02:20
  • I think the precision of the FFT would not be great enough for usable tuning. Pitch detection *maybe*, but even that would need quite the sampling rate or buffer length (with the drawback you mention). Quefrency analysis (yes, this spelling) might help though. You basically measure the "periodicity" between peaks of the FFT. See Paur R's answer. – Gauthier May 28 '13 at 06:14
0

Here is a short blog article on non-parametric techniques to estimating the PSD (power spectral density) along with some more detailed links. This might get you started in estimating the PSD - and then finding the pitch.

Paul
  • 5,308
  • 1
  • 17
  • 18
0

DansTuner is my open source project to solve this problem. I am in fact a trumpet player. It has pitch detection code lifted from Audacity.

dfrankow
  • 16,533
  • 35
  • 121
  • 177
  • Your code repository is down. Will you please post to GitHub? – S Meaden Oct 09 '16 at 12:44
  • @SMeaden I fixed the link on the project page to the source code, which is in a Subversion repo: https://sourceforge.net/p/danstuner/code/HEAD/tree/. Perhaps that is enough. – dfrankow Oct 11 '16 at 02:19
0

ia added this org.apache.commons.math.transform.FastFourierTransforme package to the project and its works perfectly

Eugine
  • 31
  • 1