12,064,539 members (55,378 online)
alternative version

289.8K views
219 bookmarked
Posted

FFT Guitar Tuner

, 10 Aug 2010 MIT
 Rate this:
Using a Fast Fourier Transform to calculate the fundamental frequency of the captured audio sound

Introduction

This article shows how to use a Fast Fourier Transform (FFT) algorithm to calculate the fundamental frequency of a captured audio sound. Also, we will see how to apply the algorithm to analyze live sound to build a simple guitar tuner: the code provides a solution to the problem of calculation of the fundamental frequency of the played pitch.

Background

The computer can capture live sound/music using a microphone that is connected to the sound card. Modern sound cards can capture digital signals. A digital signal is a set of quantized sound values that were taken in uniformly spaced times. The digital signal does not provide any information about frequencies that are present in the sound. To determine that, the data need to be analyzed.

The Short-Time Fourier Transform (STFT) makes representation of the phase and magnitude of the signal. The result of the STFT can be used to produce the spectrogram of the signal: the magnitude squared over time and frequencies. We will use a Fast Fourier Transform (FFT) to generate the spectrogram of the signal of short periods of time. After the spectrogram is calculated, the fundamental frequency can be determined by finding the index of the maximum value of the magnitude squared. The improved algorithm finds several such places, candidate frequency bins, with the magnitude squared in the top of the maximum values, and further analyzes them to verify the candidate fundamental frequencies by using the signal data.

When a note is played on a musical instrument, the sound waves are generated by strings, air, or the speaker - an instrument generates a musical note. One of the characteristics of a musical note is a pitch (fundamental frequency). Traditionally musical alphabet frequencies are divided by octaves, and then by semitones. An octave has 12 named pitches: C (prime), C#, D, D#, E, F, F#, G, G#, A, A#, and B. Octaves also have names: great, small, one-lined, two-lined, etc. The "standard pitch" (A one-lined or A4) has a fundamental frequency of its sound waves equals to 440 Hz. The frequencies of two neighboring notes are different by 21/12, and frequencies of the notes with the same name in two neighboring octaves are different by 2.

Table: Notes and Their Fundamental Frequencies
Note Name Traditional Octave Names (Scientific), Hz
Great (2) Small (3) One-lined (4) Two-lined (5)
C 65.4064 130.8128 261.6256 523.2511
C# 69.2957 138.5913 277.1826 554.3653
D 73.4162 146.8324 293.6648 587.3295
D# 77.7817 155.5635 311.1270 622.2540
E 82.4069 164.8138 329.6276 659.2551
F 87.3071 174.6141 349.2282 698.4565
F# 92.4986 184.9972 369.9944 739.9888
G 97.9989 195.9977 391.9954 783.9909
G# 103.8262 207.6523 415.3047 830.6094
A 110.0000 220.0000 440.0000 880.0000
A# 116.5409 233.0819 466.1638 932.3275
B 123.4708 246.9417 493.8833 987.7666

The typical (six string) guitar normally plays pitches of great through two-lined octaves. The pitches of the open strings (E2, A2, D3, G3, B3, and E4) are selected in the table in bold.

Using the Code

The solution contains three projects: the main windows application (FftGuitarTuner), the sound analysis library (SoundAnalysis), and the sound capture library (SoundCapture). The heart of the solution and the SoundAnalysis project is the FFT algorithm (see the `Calculate` method of the `SoundAnalysis.FftAlgorithm` class):

```// bit reversal
ComplexNumber[] data = new ComplexNumber[length];
for (int i = 0; i < x.Length; i++)
{
int j = ReverseBits(i, bitsInLength);
data[j] = new ComplexNumber(x[i]);
}

// Cooley-Tukey
for (int i = 0; i < bitsInLength; i++)
{
int m = 1 << i;
int n = m * 2;
double alpha = -(2 * Math.PI / n);

for (int k = 0; k < m; k++)
{
// e^(-2*pi/N*k)
ComplexNumber oddPartMultiplier =
new ComplexNumber(0, alpha * k).PoweredE();

for (int j = k; j < length; j += n)
{
ComplexNumber evenPart = data[j];
ComplexNumber oddPart = oddPartMultiplier * data[j + m];
data[j] = evenPart + oddPart;
data[j + m] = evenPart - oddPart;
}
}
}

// calculate spectrogram
double[] spectrogram = new double[length];
for (int i = 0; i < spectrogram.Length; i++)
{
spectrogram[i] = data[i].AbsPower2();
}```

The data for the algorithm is provided from the sound card capture buffer. The abstract `SoundCapture.SoundCaptureBase` utility class is an adapter for DirectSound's `Capture` and `CaptureBuffer` classes, that helps to encapsulate buffering and setting up the audio format parameters. The application requires Microsoft DirectX 9 runtime components for the live sound capture from the microphone.

Figure: Main Application Form

After the application is started, select the sound device and play a note. The application will capture the live sound and will calculate the current fundamental frequency of the signal. The information can be used to tune the guitar.

Points of Interest

To calculate the Fast Fourier Transform, the Cooley-Tukey algorithm was used. It gives good performance for the required task. To challenge the algorithm, the application analyses about 22,000 sample blocks in real time: the sound is captured at a 44,100 Hz rate and a 16 bits sample size, and the analysis is performed twice a second.

The sound analysis library can be used for tone, background noise, sound, or speech detection. Series of the spectrogram of the continued sound can be displayed as a 2D (or 3D) image to present it visually.

History

• 1st January, 2009: Initial version
• 2nd January, 2009: Added algorithm code snippet
• 3rd August, 2010: Corrected article typos; new frequency detection algorithm

Share

 Software Developer United States
No Biography provided

You may also be interested in...

 Re: I'm having problems using the VB .Net version, running on Windows 7 64-bit Member 826810726-Sep-11 11:41 Member 8268107 26-Sep-11 11:41
 Re: I'm having problems using the VB .Net version, running on Windows 7 64-bit Member 766644016-Dec-11 7:26 Member 7666440 16-Dec-11 7:26
 Re: I'm having problems using the VB .Net version, running on Windows 7 64-bit bmac20-Jul-13 7:05 bmac 20-Jul-13 7:05
 Is it easy to convert to VB. Net? TheJediMaster30-Jun-11 6:23 TheJediMaster 30-Jun-11 6:23
 Re: Is it easy to convert to VB. Net? notmasteryet30-Jun-11 15:25 notmasteryet 30-Jun-11 15:25
 Re: Is it easy to convert to VB. Net? TheJediMaster30-Jun-11 15:26 TheJediMaster 30-Jun-11 15:26
 ReverseBits function optimization Kvanttt15-May-11 4:37 Kvanttt 15-May-11 4:37
 System sound Pareen Vatani10-May-11 21:53 Pareen Vatani 10-May-11 21:53
 My vote of 5 P1l19r1m8-May-11 1:11 P1l19r1m 8-May-11 1:11
 Nice solution thanks for providing it and a ? Alasdair Macleod22-Jan-11 12:24 Alasdair Macleod 22-Jan-11 12:24
 Re: Nice solution thanks for providing it and a ? Alasdair Macleod25-Jan-11 1:14 Alasdair Macleod 25-Jan-11 1:14
 A Bug?. Given a 44100HZ wav, the FFT result repeat and shrink many times in above 11025HZ. jasonhh14-Aug-10 0:48 jasonhh 14-Aug-10 0:48
 Re: A Bug?. Given a 44100HZ wav, the FFT result repeat and shrink many times in above 11025HZ. notmasteryet14-Aug-10 3:51 notmasteryet 14-Aug-10 3:51
 Re: A Bug?. Given a 44100HZ wav, the FFT result repeat and shrink many times in above 11025HZ. jasonhh17-Aug-10 1:16 jasonhh 17-Aug-10 1:16
 Re: A Bug?. Given a 44100HZ wav, the FFT result repeat and shrink many times in above 11025HZ. notmasteryet17-Aug-10 15:00 notmasteryet 17-Aug-10 15:00
 Re: A Bug?. Given a 44100HZ wav, the FFT result repeat and shrink many times in above 11025HZ. jasonhh17-Aug-10 20:34 jasonhh 17-Aug-10 20:34
 My vote of 5 Joel Ivory Johnson10-Aug-10 18:04 Joel Ivory Johnson 10-Aug-10 18:04
 How do you compare fundamental frequencies? marluv23-Jul-10 5:04 marluv 23-Jul-10 5:04
 Re: How do you compare fundamental frequencies? notmasteryet8-Aug-10 15:23 notmasteryet 8-Aug-10 15:23
 Octave Problem John Parse13-Jul-10 21:55 John Parse 13-Jul-10 21:55
 Re: Octave Problem notmasteryet8-Aug-10 15:37 notmasteryet 8-Aug-10 15:37
 Cooley-Tukey PKultimPK28-Jun-10 5:32 PKultimPK 28-Jun-10 5:32
 Re: Cooley-Tukey notmasteryet8-Aug-10 17:26 notmasteryet 8-Aug-10 17:26
 C# to VB peter879-Jun-10 23:12 peter87 9-Jun-10 23:12
 Re: C# to VB notmasteryet9-Aug-10 14:14 notmasteryet 9-Aug-10 14:14
 Violin Zee-J20-Apr-10 19:37 Zee-J 20-Apr-10 19:37
 Re: Violin notmasteryet21-Apr-10 18:23 notmasteryet 21-Apr-10 18:23
 Re: Violin Zee-J21-Apr-10 20:51 Zee-J 21-Apr-10 20:51
 Re: Violin 5thWheel7-Sep-10 23:45 5thWheel 7-Sep-10 23:45
 Vb.net Krio889-Apr-10 4:15 Krio88 9-Apr-10 4:15
 Re: Vb.net peter879-Jun-10 23:16 peter87 9-Jun-10 23:16
 How do I FFT only part of spectrum? Bengt Thulin23-Mar-10 7:49 Bengt Thulin 23-Mar-10 7:49
 How to determine how long a note is held for? me8meme9me13-Mar-10 5:43 me8meme9me 13-Mar-10 5:43
 how about in VB.net? peter8712-Mar-10 0:57 peter87 12-Mar-10 0:57
 Re: how about in VB.net? me8meme9me13-Mar-10 5:59 me8meme9me 13-Mar-10 5:59
 language bintangtan7-Mar-10 19:01 bintangtan 7-Mar-10 19:01
 USB Input ? wijays@gmail.com4-Feb-10 10:52 wijays@gmail.com 4-Feb-10 10:52
 Problems running FFT Guitar Tuner on Windows 7... Hmmmmm Flemming Jensen217-Jan-10 2:30 Flemming Jensen2 17-Jan-10 2:30
 msg problem: is not a win32 valid application angelo carlt6-Jan-10 7:35 angelo carlt 6-Jan-10 7:35
 Re: msg problem: is not a win32 valid application notmasteryet6-Jan-10 17:25 notmasteryet 6-Jan-10 17:25
 Re: msg problem: is not a win32 valid application angelo carlt7-Jan-10 13:03 angelo carlt 7-Jan-10 13:03
 Re: msg problem: is not a win32 valid application daylightdj17-Mar-10 2:26 daylightdj 17-Mar-10 2:26
 Re: msg problem: is not a win32 valid application angelo carlt17-Mar-10 18:20 angelo carlt 17-Mar-10 18:20
 Re: msg problem: is not a win32 valid application daylightdj24-Mar-10 2:35 daylightdj 24-Mar-10 2:35
 pls help me. . . clareese6-Jan-10 2:34 clareese 6-Jan-10 2:34
 Re: pls help me. . . notmasteryet6-Jan-10 17:20 notmasteryet 6-Jan-10 17:20
 Re: pls help me. . . proximity33-Jul-10 3:44 proximity3 3-Jul-10 3:44
 high strings tuned rifffff1-Sep-09 5:35 rifffff 1-Sep-09 5:35
 Nice Job!! [modified] EricZ121-Jul-09 23:27 EricZ1 21-Jul-09 23:27
 Very usefull mofo10113-Jul-09 19:11 mofo101 13-Jul-09 19:11
 Last Visit: 31-Dec-99 19:00     Last Update: 6-Feb-16 9:00 Refresh « Prev123 Next »