Try Embedded

Find a tone with a DFT

The Uno builds a test signal (a tone the knob tunes, a 120 Hz hum and noise), runs a 64-point discrete Fourier transform on it, and lights the LED bar at the…

Sensors · Advanced

In a real product: Machine vibration monitor (Industrial): A monitor finds the strongest vibration frequency of a motor to warn of a worn bearing.

A discrete Fourier transform (DFT) answers the question "which frequencies are in this signal, and how strong is each one?". Vibration monitors, guitar tuners and spectrum displays all compute one. The FFT is just a fast way of computing the same DFT.

Here the Uno makes its own test signal, so every part of the maths is in plain view. The signal is a tone whose frequency the knob sets, plus a weaker 120 Hz hum and some random noise. It is "sampled" at 1 kHz, 64 samples at a time. The sketch computes the DFT, prints the strongest frequencies, and lights the segment of the LED bar for the peak.

What you'll learn

  • What a DFT bin is: with 64 samples at 1000 Hz, each bin is 1000 / 64 = 15.6 Hz wide
  • Why only bins 0 to 32 mean anything (the Nyquist limit, half the sample rate)
  • Computing one bin: multiply the samples by a cosine and a sine of that frequency and add them up
  • A Hann window, and why it keeps a strong tone from spreading into every bin (leakage)
  • A sine table in place of sin(), so the inner loop needs only multiplications

How it works

makeSignal() fills x[64] with 64 samples, 1 ms apart: the tone at the knob's frequency (50 to 450 Hz), the hum at a third of its strength, and noise. Each sample is then multiplied by the Hann window, a curve that is 0 at both ends and 1 in the middle. Without it, a tone that doesn't fit a whole number of cycles into the 64 samples has a jump at the edges, and that jump smears energy over the whole spectrum.

For bin k, the DFT is the sum of x[n] × cos(2πkn/64) (the real part) and of x[n] × sin(2πkn/64) (the imaginary part), over all 64 samples. The magnitude is √(re² + im²). A sample that follows the bin's frequency adds up in step, while other frequencies cancel out. The angle 2πkn/64 only ever takes 64 values, so a 64-entry sine table gives every cosine and sine as SINE[(k * n) % 64] (the cosine is the sine a quarter turn later, 16 entries on).

The sketch finds the bin with the largest magnitude, above bin 1 (bin 0 is the average, DC). It prints its frequency and the strongest bins, and maps it onto the bar's 10 segments. 64 × 33 multiply-adds take the Uno about 75 ms. An FFT would need 64 × 6 butterflies, which is why everyone uses one for bigger sizes.

Try this

  • Turn the knob slowly and watch the peak step from bin to bin: that's the 15.6 Hz resolution.
  • Set the tone above 500 Hz (change map()'s top to 800). The peak comes back down: that is aliasing.
  • Remove the window (w = 1) and compare how many bins get energy from one tone.

sketch.ino

// A 64-point DFT on the Uno: a test signal (a tone the knob sets, a 120 Hz hum, noise) sampled at 1 kHz; the LED bar
// shows the strongest frequency.
#include <math.h>

const int N = 64;
const float RATE = 1000.0;               // samples a second
const int KNOB = A0, FIRST_LED = 2, LEDS = 10;

float x[N];
float SINE[N];                           // SINE[i] = sin(2 pi i / N)
float magnitude[N / 2 + 1];

void makeSignal(float toneHz) {
  for (int n = 0; n < N; n++) {
    float t = n / RATE;
    float s = sin(2 * M_PI * toneHz * t) + 0.33 * sin(2 * M_PI * 120 * t) + (random(-100, 101) / 400.0);
    float w = 0.5 - 0.5 * cos(2 * M_PI * n / (N - 1));   // Hann window
    x[n] = s * w;
  }
}

void dft() {
  for (int k = 0; k <= N / 2; k++) {
    float re = 0, im = 0;
    for (int n = 0; n < N; n++) {
      int i = (k * n) % N;
      re += x[n] * SINE[(i + N / 4) % N];   // cos is sin a quarter turn on
      im -= x[n] * SINE[i];
    }
    magnitude[k] = sqrt(re * re + im * im);
  }
}

void setup() {
  Serial.begin(115200);
  for (int i = 0; i < LEDS; i++) pinMode(FIRST_LED + i, OUTPUT);
  for (int i = 0; i < N; i++) SINE[i] = sin(2 * M_PI * i / N);
  Serial.print(F("Bin width "));
  Serial.print(RATE / N, 1);
  Serial.println(F(" Hz"));
}

void loop() {
  float toneHz = map(analogRead(KNOB), 0, 1023, 50, 450);
  makeSignal(toneHz);
  unsigned long start = micros();
  dft();
  unsigned long took = micros() - start;

  int peak = 2;
  for (int k = 2; k <= N / 2; k++) if (magnitude[k] > magnitude[peak]) peak = k;
  int second = peak == 2 ? 3 : 2;
  for (int k = 2; k <= N / 2; k++) if (k != peak && abs(k - peak) > 1 && magnitude[k] > magnitude[second]) second = k;

  Serial.print(F("tone "));
  Serial.print(toneHz, 0);
  Serial.print(F(" Hz -> peak bin "));
  Serial.print(peak);
  Serial.print(F(" ("));
  Serial.print(peak * RATE / N, 0);
  Serial.print(F(" Hz), next bin "));
  Serial.print(second);
  Serial.print(F(" ("));
  Serial.print(second * RATE / N, 0);
  Serial.print(F(" Hz), DFT "));
  Serial.print(took / 1000);
  Serial.println(F(" ms"));

  int lit = map(peak, 2, N / 2, 0, LEDS - 1);
  for (int i = 0; i < LEDS; i++) digitalWrite(FIRST_LED + i, i == lit ? HIGH : LOW);
  delay(300);
}

Lessons behind it