12,075,066 members (63,265 online)
alternative version

290.3K 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...

 First PrevNext
 Point FFT Member 1167218922-May-15 22:12 Member 11672189 22-May-15 22:12
 FFT Guitar Tuner not work in win7 64bit & visual studio 2010 lpln85211-Aug-14 22:00 lpln852 11-Aug-14 22:00
 Re: FFT Guitar Tuner not work in win7 64bit & visual studio 2010 Member 106723433-Jun-15 10:28 Member 10672343 3-Jun-15 10:28
 Project within a project Member 1053139926-Apr-14 7:07 Member 10531399 26-Apr-14 7:07
 My vote of 1 Fabrizio Stellato12-Jan-14 4:28 Fabrizio Stellato 12-Jan-14 4:28
 How hard it would be to redo this project for java ? YuriyKravets25-Nov-13 7:00 YuriyKravets 25-Nov-13 7:00
 Got it working on XP x64 under .NET 4.0 bmac20-Jul-13 6:54 bmac 20-Jul-13 6:54
 My vote of 5 Abdullatif M. Abu Al Rub2-Mar-13 1:31 Abdullatif M. Abu Al Rub 2-Mar-13 1:31
 My vote of 5 vard9912-Jan-13 14:06 vard99 12-Jan-13 14:06
 Building as 4.0 theney11-Dec-12 8:04 theney 11-Dec-12 8:04
 Re: Building as 4.0 Member 869236731-Jan-13 9:52 Member 8692367 31-Jan-13 9:52
 Re: Building as 4.0 bmac20-Jul-13 6:58 bmac 20-Jul-13 6:58
 Problem with Fft Algorithm Member 963903729-Nov-12 2:14 Member 9639037 29-Nov-12 2:14
 ? Windows 7 rspercy6528-Nov-12 5:19 rspercy65 28-Nov-12 5:19
 Re: ? Windows 7 lalitsrana3-Jan-13 21:46 lalitsrana 3-Jan-13 21:46
 Re: ? Windows 7 Member 869236731-Jan-13 9:41 Member 8692367 31-Jan-13 9:41
 Re: ? Windows 7 bmac20-Jul-13 6:58 bmac 20-Jul-13 6:58
 Request Help on using this code to measure bicycle spoke tesnion Member 934224119-Aug-12 16:37 Member 9342241 19-Aug-12 16:37
 Not working as expected ZeAleks24-May-12 10:39 ZeAleks 24-May-12 10:39
 My vote of 1 vartika sharma25-Apr-12 3:23 vartika sharma 25-Apr-12 3:23
 HELP please: How to modify this to work for 2 channels (stereo) Member 813676915-Feb-12 5:02 Member 8136769 15-Feb-12 5:02
 Re: HELP please: How to modify this to work for 2 channels (stereo) notmasteryet15-Feb-12 14:25 notmasteryet 15-Feb-12 14:25
 not working and converting to rpm torchie20-Nov-11 9:06 torchie 20-Nov-11 9:06
 Re: cannot see microphone no matter what bmac20-Jul-13 6:59 bmac 20-Jul-13 6:59
 How do I get the data for creating a dense spectrogram? David247-Oct-11 10:38 David24 7-Oct-11 10:38
 I'm picking up the audio from line 1 and line 2, but ... TheJediMaster30-Sep-11 14:00 TheJediMaster 30-Sep-11 14:00
 My vote of 5 AMCode26-Sep-11 4:33 AMCode 26-Sep-11 4:33
 How to get the sound of line 2? TheJediMaster27-Jul-11 6:36 TheJediMaster 27-Jul-11 6:36
 Guitar Tuner (macro and micro tuning) - The Dark Side of the Force. (please test) TheJediMaster16-Jul-11 11:10 TheJediMaster 16-Jul-11 11:10
 Re: Guitar Tuner (macro and micro tuning) - The Dark Side of the Force. (please test) fullcharged247-Feb-12 18:58 fullcharged24 7-Feb-12 18:58
 just one more thing TheJediMaster14-Jul-11 15:12 TheJediMaster 14-Jul-11 15:12
 Re: just one more thing notmasteryet14-Jul-11 15:57 notmasteryet 14-Jul-11 15:57
 Re: just one more thing TheJediMaster14-Jul-11 16:24 TheJediMaster 14-Jul-11 16:24
 Is there any advantage if I increase or decrease the value of BufferSeconds? TheJediMaster12-Jul-11 13:40 TheJediMaster 12-Jul-11 13:40
 Re: Is there any advantage if I increase or decrease the value of BufferSeconds? notmasteryet12-Jul-11 15:58 notmasteryet 12-Jul-11 15:58
 It depends on your hardware: smaller buffer gives less latency but more CPU usage. I recommend to experiment with NotifyPointsInSecond, increasing this amount will give you smaller amount of data that need to processed by the FFT.
 Re: Is there any advantage if I increase or decrease the value of BufferSeconds? TheJediMaster13-Jul-11 12:17 TheJediMaster 13-Jul-11 12:17
 What about win7 amersaw10-Jul-11 15:42 amersaw 10-Jul-11 15:42
 Re: What about win7 TheJediMaster11-Jul-11 11:44 TheJediMaster 11-Jul-11 11:44
 Re: What about win7 bmac20-Jul-13 7:02 bmac 20-Jul-13 7:02
 Here is how to display the name of the note and octave for the note. TheJediMaster8-Jul-11 4:43 TheJediMaster 8-Jul-11 4:43
 "frequencyTextBox" update TheJediMaster8-Jul-11 2:09 TheJediMaster 8-Jul-11 2:09
 Re: "frequencyTextBox" update TheJediMaster12-Jul-11 13:42 TheJediMaster 12-Jul-11 13:42
 Re: "frequencyTextBox" update notmasteryet12-Jul-11 16:00 notmasteryet 12-Jul-11 16:00
 I made some changes: Voice tuner TheJediMaster7-Jul-11 9:17 TheJediMaster 7-Jul-11 9:17
 Fundamental frequency algoritm Kvanttt1-Jul-11 9:46 Kvanttt 1-Jul-11 9:46
 I'm having problems using the VB .Net version, running on Windows 7 64-bit TheJediMaster30-Jun-11 15:24 TheJediMaster 30-Jun-11 15:24
 Re: I'm having problems using the VB .Net version, running on Windows 7 64-bit notmasteryet30-Jun-11 15:26 notmasteryet 30-Jun-11 15:26
 Re: I'm having problems using the VB .Net version, running on Windows 7 64-bit TheJediMaster5-Jul-11 12:23 TheJediMaster 5-Jul-11 12:23
 Last Visit: 31-Dec-99 19:00     Last Update: 11-Feb-16 15:57 Refresh 123 Next »