-
-
Notifications
You must be signed in to change notification settings - Fork 295
Simple Frequency Detection
Audio frequency detection is the process of identifying the pitch or dominant frequency of a sound signal. Three common methods used are Fast Fourier Transform (FFT), AutoCorrelation, and Zero-Crossing.
-
AutoCorrelation compares a signal with delayed versions of itself to detect repeating patterns, which correspond to pitch. It's effective for monophonic signals and works well in noisy environments, but can be computationally intensive.
-
Zero-Crossing counts how often the signal crosses the zero amplitude axis to estimate frequency. It is simple and fast but only reliable for clean, single-frequency signals and can be inaccurate with harmonics or noise.
-
FFT transforms a time-domain audio signal into its frequency components, making it ideal for identifying multiple frequencies or spectral content. It's accurate for complex signals but may require windowing and smoothing for real-time pitch tracking.
This chapter describes how to do the more lightweight AutoCorrelation and Zero-Crossing. Here is a simple example:
#include "AudioTools.h"
AudioInfo info(44100, 2, 16);
SineWaveGenerator<int16_t> sineWave;
GeneratedSoundStream<int16_t> sound(sineWave);
FrequencyDetectorAutoCorrelation out(1024);
// FrequencyDetectorAutoCorrelation out;
StreamCopy copier(out, sound, 10 * 1024);
// Arduino Setup
void setup(void) {
// Open Serial
Serial.begin(115200);
AudioToolsLogger.begin(Serial, AudioToolsLogLevel::Warning);
// start I2S
Serial.println("starting ...");
out.begin(info);
// Setup sine wave
sineWave.begin(info, 200);
Serial.println("started...");
}
// Arduino loop - copy sound to out
void loop() {
copier.copy();
Serial.print(out.frequency(0));
Serial.print(" ");
Serial.println(out.frequency(1));
}