← All Base Form Math projects
BM-21NOT STARTEDBASE FORM · CALCULUSMATHPYTHON

Guitar tuner

Record a note, run your own FFT, and tell the player which string is off and by how much.

EST.7 h
REWARD+100 XP
AFTERJez Swanson

ML-ARENA BENCHMARKS

YOUR NUMBER VS THE INDUSTRY'S
METRICINDUSTRYMINE
FFT output; detected pitch
GOAL · FFT within 1e-9; pitch within 1 Hz
numpy.fft · labelled clips—
no results yet
YOUR RESULTS

Your recorded results appear here.

Brief

A tuner listens to a note and says which string you played and whether it is sharp or flat. To do that it needs to know which frequencies are inside the sound. The Fourier transform answers exactly that question. In this project you write it yourself, first slowly, then fast, and turn it into a working tuner.

The discrete Fourier transform takes NN samples xnx_n and gives one complex number per frequency: Xk=∑n=0N−1xne−2πikn/NX_k = \sum_{n=0}^{N-1} x_n e^{-2\pi i k n / N}. Written out, that is just a matrix times a vector, so your first version is a matrix multiply. It costs N2N^2 operations. The fast Fourier transform (FFT) gets the same answer in Nlog⁡2NN \log_2 N by splitting the samples into even and odd halves and reusing the two smaller transforms.

Output kk corresponds to the frequency kfs/Nk f_s / N, where fsf_s is the sample rate. At 44,100 samples per second and NN = 44,100, the bins are 1 Hz apart. Half a second of audio gives 2 Hz bins, so you will need to estimate between bins to reach 1 Hz accuracy. Standard tuning is E2 82.41 Hz, A2 110.00, D3 146.83, G3 196.00, B3 246.94 and E4 329.63. The distance in cents is 1200log⁡2(f/fref)1200 \log_2(f / f_{\text{ref}}); 100 cents is one semitone.

VISUALFig. 1
waveform (3 periods, 27 ms) magnitude spectrum |X(f)| 110 220 330 440 550 Hz f0 tallest peak = 2 × f0
A plucked A string in time and in frequency. The peaks sit at whole-number multiples of 110 Hz, and here the second one is the tallest. Why it's here: Objective 4 trips most people up: the tallest peak is not always the pitch.
HINT · OBJECTIVE 2
Stuck? Hints stay hidden until you ask.
HINT · OBJECTIVE 4
Stuck? Hints stay hidden until you ask.

DATA · your recordings

Objectives

0 / 5 CLEARED

Read-only. Log in to update progress.

VICTORY CONDITIONS

  1. Your FFT matches np.fft.fft within 1e-9 (largest absolute difference) on random inputs up to 65,536 samples. Record the error with `python tools/mlarena.py record BM-21 "<max error>"`.
  2. Detected pitch is within 1 Hz of the label on every labelled clip, including the low E string.
  3. No np.fft call anywhere in the tuner itself; NumPy's FFT appears only in your tests.

Side quests

Make it live. Read the microphone in short blocks with the sounddevice library and update the reading a few times per second.
Test the convolution theorem. Multiply two spectra, transform back, and compare with np.convolve.
Add an inverse FFT and use it to remove a hum at 50 or 60 Hz from a recording.

Terms in plain words

Discrete Fourier transform (DFT)
Turns N samples in time into N complex numbers, one per frequency, that say how much of each frequency the signal contains.
FFT
A fast way to compute the exact same DFT. It splits the signal into even and odd samples again and again, so the cost drops from N² to N log N.
Frequency bin
Output k of the DFT stands for the frequency k × (sample rate) / N. The bins are evenly spaced, so a pitch between two bins has to be estimated.
Window
A smooth curve, such as the Hann window, multiplied into the clip so it fades in and out. It stops the hard edges of the clip smearing the spectrum.
Harmonic
A plucked string vibrates at its pitch f0 and also at 2·f0, 3·f0 and so on. These whole-number multiples are its harmonics.