Click here to Skip to main content
14,241,762 members

FFT Guitar Tuner

Rate this:
4.95 (76 votes)
Please Sign up or sign in to vote.
4.95 (76 votes)
10 Aug 2010MIT
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 NameTraditional Octave Names (Scientific), Hz
Great (2)Small (3)One-lined (4)Two-lined (5)
C65.4064130.8128261.6256523.2511
C#69.2957138.5913277.1826554.3653
D73.4162146.8324293.6648587.3295
D#77.7817155.5635311.1270622.2540
E82.4069164.8138329.6276659.2551
F87.3071174.6141349.2282698.4565
F#92.4986184.9972369.9944739.9888
G97.9989195.9977391.9954783.9909
G#103.8262207.6523415.3047830.6094
A110.0000220.0000440.0000880.0000
A#116.5409233.0819466.1638932.3275
B123.4708246.9417493.8833987.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.

Main Application Form

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.

References

  1. "Musical Note", Wikipedia
  2. "Short-Time Fourier Transform", Wikipedia
  3. "Fast Fourier Transform", Wikipedia
  4. "Cooley-Tukey FFT Algorithm", Wikipedia

History

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

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

notmasteryet
Software Developer
United States United States
No Biography provided

Comments and Discussions

 
Generallanguage Pin
bintangtan7-Mar-10 18:01
memberbintangtan7-Mar-10 18:01 
QuestionUSB Input ? Pin
FaradayComplex4-Feb-10 9:52
memberFaradayComplex4-Feb-10 9:52 
GeneralProblems running FFT Guitar Tuner on Windows 7... Hmmmmm Pin
Flemming Jensen217-Jan-10 1:30
memberFlemming Jensen217-Jan-10 1:30 
Questionmsg problem: is not a win32 valid application Pin
angelo carlt6-Jan-10 6:35
memberangelo carlt6-Jan-10 6:35 
AnswerRe: msg problem: is not a win32 valid application Pin
notmasteryet6-Jan-10 16:25
membernotmasteryet6-Jan-10 16:25 
QuestionRe: msg problem: is not a win32 valid application Pin
angelo carlt7-Jan-10 12:03
memberangelo carlt7-Jan-10 12:03 
AnswerRe: msg problem: is not a win32 valid application Pin
daylightdj17-Mar-10 1:26
memberdaylightdj17-Mar-10 1:26 
GeneralRe: msg problem: is not a win32 valid application Pin
angelo carlt17-Mar-10 17:20
memberangelo carlt17-Mar-10 17:20 
Hey! HI!!!

I found a solution to me!! I hope that it can help you guys, too!!

Fisrt of all i changed the CPU from Any to x86 in VS2008.
Then i went to:
-Open your VS2008 project
-In Menu go to Debug->Exceptions...
-"Manage Debbugin assistant" and find "LoaderLock"
-Undo the selection to this item
-Re-compile the project

That works to me! I hope would help you guys , thanks!!!
GeneralRe: msg problem: is not a win32 valid application Pin
daylightdj24-Mar-10 1:35
memberdaylightdj24-Mar-10 1:35 
Generalpls help me. . . Pin
clareese6-Jan-10 1:34
memberclareese6-Jan-10 1:34 
GeneralRe: pls help me. . . Pin
notmasteryet6-Jan-10 16:20
membernotmasteryet6-Jan-10 16:20 
GeneralRe: pls help me. . . Pin
proximity33-Jul-10 2:44
memberproximity33-Jul-10 2:44 
Generalhigh strings tuned Pin
rifffff1-Sep-09 4:35
memberrifffff1-Sep-09 4:35 
QuestionNice Job!! [modified] Pin
ezcodez21-Jul-09 22:27
memberezcodez21-Jul-09 22:27 
QuestionVery usefull Pin
squiso13-Jul-09 18:11
membersquiso13-Jul-09 18:11 
AnswerRe: Very usefull Pin
notmasteryet14-Jul-09 17:31
membernotmasteryet14-Jul-09 17:31 
GeneralInverse transform Pin
rafaelverisys7-Jul-09 5:27
memberrafaelverisys7-Jul-09 5:27 
GeneralRe: Inverse transform Pin
notmasteryet7-Jul-09 12:41
membernotmasteryet7-Jul-09 12:41 
GeneralRe: Inverse transform Pin
rafaelverisys8-Jul-09 7:05
memberrafaelverisys8-Jul-09 7:05 
QuestionRunning Vista and DirectX 10 Pin
stzahi25-Jun-09 18:17
memberstzahi25-Jun-09 18:17 
AnswerRe: Running Vista and DirectX 10 Pin
bmac20-Jul-13 6:10
memberbmac20-Jul-13 6:10 
Generalguitar tuner Pin
Edson Stedile24-Jun-09 10:02
memberEdson Stedile24-Jun-09 10:02 
QuestionWhere are the sources? Pin
soulconst11-May-09 6:46
membersoulconst11-May-09 6:46 
Questionanalysis of a file Pin
azerty1622-Apr-09 3:19
memberazerty1622-Apr-09 3:19 
AnswerRe: analysis of a file [modified] Pin
notmasteryet24-Apr-09 15:23
membernotmasteryet24-Apr-09 15:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Article
Posted 2 Jan 2009

Stats

423.5K views
27.6K downloads
226 bookmarked