Arduino library · Data and time

arduinoFFT: the frequency spectrum of a sampled signal

Yes. You can use the arduinoFFT library online in Mokxi with nothing to install, and the sketch runs in your browser. The example below, "Audio spectrum analyzer", runs on the ESP32 DevKit V1.

Built in on Mokxi: #include <arduinoFFT.h> and it builds, with nothing to install. Free, and no account needed.

What arduinoFFT does

arduinoFFT by Enrique Condes turns a block of samples taken at a steady rate into a spectrum: how much of each frequency the signal holds. People use it to find the pitch of a note picked up by a microphone, drive a spectrum analyzer on an LED matrix, or watch a motor’s vibration. You fill an array of real values, set the imaginary array to zero, and the library does the transform in place.

The steps are always the same: apply a window so the ends of the block do not smear energy across the spectrum, compute the transform, turn the complex results into magnitudes, then read them. Each output bin i stands for i times the sampling rate over the number of samples, and majorPeak() interpolates between bins to give the strongest frequency in hertz.

Version 2 changed the interface. It is a template, ArduinoFFT<double> or ArduinoFFT<float>, built with the arrays, the sample count and the sampling rate, so the calls take no arguments beyond the window and direction. Old sketches written for version 1 use the arduinoFFT class with capitalized methods and pass the arrays every time; Mokxi keeps both, and the example runs both on the same 1000 Hz tone sampled 8000 times a second. The block size must be a power of two, and on an Uno 64 or 128 samples is about as far as the RAM goes.

Library Manager namearduinoFFT
AuthorEnrique Condes
Include#include <arduinoFFT.h>
Boardsevery board Mokxi compiles for but ATtiny85
In the simulatorSimulated: it runs against the part the way it does on the bench.

Install it

It builds on every board Mokxi compiles for but ATtiny85.

In Mokxi it is built in: #include <arduinoFFT.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "arduinoFFT" by Enrique Condes from the Arduino IDE’s Library Manager (Sketch, Include Library, Manage Libraries), or add arduinoFFT to lib_deps in PlatformIO. Mokxi follows the calls of release 2.0.4, so a sketch written here builds there too.

The calls you will use

In the order a sketch meets them.

ArduinoFFT<double> FFT(vReal, vImag, samples, samplingFrequency)

The version 2 object, tied to your two arrays, the number of samples (a power of two) and the rate they were taken at in hertz.

FFT.windowing(FFTWindow::Hamming, FFTDirection::Forward)

Weights the samples so the block’s edges fade out. Hamming suits most uses; Rectangle means no window at all.

FFT.compute(FFTDirection::Forward)

Runs the transform in place. Afterward vReal and vImag hold the complex result, not your samples.

FFT.complexToMagnitude()

Replaces the first half of vReal with the size of each frequency bin. Only bins 0 to samples / 2 mean anything.

FFT.majorPeak()

The strongest frequency in hertz, interpolated between bins, so it can land closer than one bin’s width.

FFT.dcRemoval()

Subtracts the average from the samples before the window, so the offset of an analogRead() centered at 512 does not swamp bin 0 and its neighbors.

legacy.MajorPeak(vReal, samples, samplingFrequency)

The version 1 form on an arduinoFFT object: every method is capitalized and takes the arrays as arguments.

Example: Audio spectrum analyzer

On the ESP32 DevKit V1. It runs in the browser exactly as printed.

sketch.ino · ESP32 DevKit V1
// Audio spectrum analyzer: an INMP441 I2S microphone, an FFT and dancing bars.
//
//   INMP441   SCK -> GPIO 19   WS -> GPIO 18   SD -> GPIO 5
//             VDD -> 3V3   GND -> GND   L/R -> GND (left slot)
//   SSD1306   SDA -> GPIO 21   SCL -> GPIO 22   VCC -> 3V3   GND -> GND
//
// # From sound to bars
//
// A microphone gives you a number for the air pressure many thousands of times
// a second. That is a waveform: you can see how LOUD it is, but not what
// notes are in it. The Fast Fourier Transform (FFT) takes a short chunk of the
// waveform and answers a different question: how much of each pitch is in it?
// A pure 1000 Hz tone becomes one tall spike at 1000 Hz. A voice becomes a
// handful of spikes (the pitch and its overtones). Drawing the answer as a row
// of bars, left for bass and right for treble, is the classic spectrum
// analyzer.
//
// # The numbers
//
//   sample rate         16000 per second
//   chunk               256 samples, which is 16 ms of sound
//   frequency per bin   16000 / 256 = 62.5 Hz
//   highest pitch       half the sample rate, 8000 Hz (Nyquist)
//
// A chunk of 256 samples gives 128 bins from 0 to 8000 Hz. That is too many
// for a screen 128 pixels wide, and our ears hear pitch on a log scale (each
// octave is double the frequency), so the sketch folds the bins into 16 bars
// whose widths grow: the left bars hold one bin each, the right bars twenty.
//
// Each bar shows the loudest bin in its band, in decibels below a full-scale
// sine (dBFS). Decibels squash the huge range between a whisper and a shout
// into something a 54 pixel bar can show. Bars jump up at once and drift down
// slowly, and a dot at the top of each one falls even slower, the peak hold
// every hi-fi has.
//
// The INMP441 sends 24-bit samples in 32-bit slots, left aligned, so shifting
// the sample right by 8 gives the number. The first quarter second after power
// up is zeros while the microphone settles, as its data sheet says.
//
// Pick the microphone's sound (tone, noise or voice) and drag its pitch and
// level to watch the bars move.

#include <driver/i2s.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <arduinoFFT.h>
#include <math.h>

#define I2S_PORT I2S_NUM_0
#define I2S_SCK 19
#define I2S_WS 18
#define I2S_SD 5

const int kRate = 16000;
const int kSamples = 256;
const int kBars = 16;

// The first FFT bin of each bar, then one past the last. Bin i is i x 62.5 Hz.
const int kEdges[kBars + 1] = {1, 2, 3, 4, 6, 8, 10, 13, 17, 22, 28, 36, 46, 59, 76, 98, 128};

// The bars span -85 dBFS (the room) up to -25 dBFS (loud and close).
const float kFloorDb = -85.0f;
const float kCeilDb = -25.0f;
const int kTop = 10;     // the first row of the bar area
const int kHeight = 54;  // bar area height in pixels

Adafruit_SSD1306 display(128, 64, &Wire, -1);

int32_t samples[kSamples];
float vReal[kSamples];
float vImag[kSamples];
ArduinoFFT<float> FFT = ArduinoFFT<float>(vReal, vImag, kSamples, kRate);

float level[kBars];  // each bar's height, 0 to kHeight
float peak[kBars];   // each bar's falling dot
unsigned long lastReport = 0;

void setupMicrophone() {
  // Start from all zeros and set what we need, so any field this driver version
  // adds later is simply left at its default.
  i2s_config_t config = {};
  config.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX);
  config.sample_rate = kRate;
  config.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT;
  config.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT;  // L/R tied to GND
  config.communication_format = I2S_COMM_FORMAT_STAND_I2S;
  config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1;
  config.dma_buf_count = 4;
  config.dma_buf_len = 256;

  i2s_pin_config_t pins = {};
  pins.mck_io_num = I2S_PIN_NO_CHANGE;
  pins.bck_io_num = I2S_SCK;
  pins.ws_io_num = I2S_WS;
  pins.data_out_num = I2S_PIN_NO_CHANGE;
  pins.data_in_num = I2S_SD;

  i2s_driver_install(I2S_PORT, &config, 0, NULL);
  i2s_set_pin(I2S_PORT, &pins);
  i2s_zero_dma_buffer(I2S_PORT);
}

// One chunk of sound in, one set of bar heights out. Returns the loudest pitch.
float listen() {
  size_t got = 0;
  i2s_read(I2S_PORT, samples, sizeof(samples), &got, portMAX_DELAY);
  int n = got / sizeof(int32_t);
  if (n < kSamples) return 0;

  // The 24-bit sample, scaled so a full-scale sine has amplitude 1.0.
  float mean = 0;
  for (int i = 0; i < kSamples; i++) {
    vReal[i] = (float)(samples[i] >> 8) / 8388607.0f;
    mean += vReal[i];
  }
  mean /= kSamples;
  for (int i = 0; i < kSamples; i++) {
    vReal[i] -= mean;  // take out any steady offset
    vImag[i] = 0;
  }

  // A window fades the chunk in and out so its hard edges do not smear the
  // spectrum, then the transform, then the size of each bin.
  FFT.windowing(FFTWindow::Hann, FFTDirection::Forward);
  FFT.compute(FFTDirection::Forward);
  FFT.complexToMagnitude();
  float pitch = FFT.majorPeak();

  for (int b = 0; b < kBars; b++) {
    float strongest = 0;
    for (int bin = kEdges[b]; bin < kEdges[b + 1]; bin++) {
      if (vReal[bin] > strongest) strongest = vReal[bin];
    }
    // Hann halves a sine's size and a bin holds half of it again: x 4 / N.
    float amplitude = strongest * 4.0f / kSamples;
    float db = amplitude > 1e-9f ? 20.0f * log10f(amplitude) : -180.0f;
    float h = (db - kFloorDb) / (kCeilDb - kFloorDb) * kHeight;
    if (h < 0) h = 0;
    if (h > kHeight) h = kHeight;

The sketch is cut short here; the editor has every line of it.

Open it in the editor, wired and ready to run · Audio spectrum analyzer: the project page

Common errors and how to fix them

'FFTWindow' has not been declared, or no matching function for call to arduinoFFT::Windowing

The sketch mixes the two versions. Code with FFT.Windowing(vReal, ...) is version 1 and wants the arduinoFFT class; code with FFTWindow:: is version 2 and wants ArduinoFFT<double>. Pick one form for the whole sketch.

The peak frequency is wrong by a large, fixed amount

The sampling rate you told the library is not the rate you sampled at. Time the sampling loop with micros() and wait for each sample period, rather than calling analogRead() as fast as it goes and guessing.

The sketch resets or prints garbage with 256 samples on an Uno

Two arrays of 256 doubles take 2 KB, all of the Uno’s RAM. Use 64 or 128 samples, ArduinoFFT<float>, or a board with more memory such as an ESP32.

Questions and answers

Why does version 1 give 1015.9 Hz where version 2 gives 1000?

Version 1’s MajorPeak divides by samples minus one when it converts the bin to hertz. Version 2 fixed that, so its answer lands on the true frequency.

What is the highest frequency I can measure?

Half the sampling rate. Sampling at 8000 Hz shows frequencies up to 4000 Hz, and anything above that folds back as a false lower peak.

Try arduinoFFT without installing anything

Open the example, change the sketch, and keep your version in a free account.