Fourier Transform in Music: Decomposing Sound into Frequencies
Understanding how the Fourier Transform reveals the hidden frequency structure of music, enabling everything from compression to AI-powered analysis.

The Mathematical Magic of Fourier
Jean-Baptiste Joseph Fourier's revolutionary insight was that any periodic signal can be decomposed into a sum of simple sine and cosine waves. This mathematical principle, now known as the Fourier Transform, is the foundation of modern digital audio processing.
For a continuous signal x(t), the Fourier Transform X(f) is defined as:
Where f is frequency, t is time, and e^(-i2πft) represents complex exponentials that encode both magnitude and phase.
From Theory to Practice: The DFT and FFT
In digital systems, we work with discrete samples rather than continuous signals. This requires the Discrete Fourier Transform (DFT), which operates on a finite sequence of samples:
The DFT is computationally expensive with O(N²) complexity. The Fast Fourier Transform (FFT) algorithm reduces this to O(N log N), making real-time audio processing feasible.
1// Simple radix-2 FFT implementation
2function fft(signal) {
3 const N = signal.length;
4
5 // Base case
6 if (N <= 1) return signal;
7
8 // Divide
9 const even = fft(signal.filter((_, i) => i % 2 === 0));
10 const odd = fft(signal.filter((_, i) => i % 2 === 1));
11
12 // Conquer
13 const result = new Array(N);
14 for (let k = 0; k < N/2; k++) {
15 const t = Math.exp(-2 * Math.PI * 1j * k / N) * odd[k];
16 result[k] = even[k] + t;
17 result[k + N/2] = even[k] - t;
18 }
19
20 return result;
21}
The Short-Time Fourier Transform (STFT)
Music is non-stationary - its frequency content changes over time. The STFT addresses this by applying the Fourier Transform to short, overlapping windows of the signal:
The STFT faces a fundamental limitation known as the Heisenberg-Gabor Uncertainty Principle:
- Long windows: High frequency resolution, poor time resolution
- Short windows: High time resolution, poor frequency resolution
- Typical compromise: 20-50ms windows with 50% overlap
The Spectrogram: Visualizing Music's Structure
The magnitude of the STFT gives us the spectrogram - a 2D representation where time is on the x-axis, frequency on the y-axis, and intensity represents amplitude:
1// Generate spectrogram using Web Audio API and Canvas
2async function createSpectrogram(audioBuffer) {
3 const sampleRate = audioBuffer.sampleRate;
4 const channelData = audioBuffer.getChannelData(0);
5
6 // STFT parameters
7 const windowSize = 2048;
8 const hopSize = 512;
9 const window = createHannWindow(windowSize);
10
11 const spectrogram = [];
12
13 for (let i = 0; i <= channelData.length - windowSize; i += hopSize) {
14 // Extract and window the frame
15 const frame = channelData.slice(i, i + windowSize);
16 const windowedFrame = frame.map((s, j) => s * window[j]);
17
18 // Compute FFT
19 const spectrum = fft(windowedFrame);
20
21 // Take magnitude and convert to dB
22 const magnitude = spectrum.map(c =>
23 20 * Math.log10(Math.abs(c) + 1e-10)
24 );
25
26 spectrogram.push(magnitude.slice(0, windowSize/2));
27 }
28
29 return spectrogram;
30}
The Mel Scale: Modeling Human Perception
Human hearing is logarithmic - we perceive equal ratios of frequencies as equal intervals. The mel scale approximates this perception:
Mel spectrograms are the standard input for modern AI music models because they better represent how we actually hear music.
Applications in Modern Music Technology
MP3 and AAC use Fourier analysis to identify and remove frequencies we can't hear, achieving 10:1 compression.
Real-time pitch correction analyzes frequency content and shifts it to the nearest musical note.
AI models use spectrograms to identify and isolate individual instruments from mixed recordings.
Analyzing rhythmic patterns in the frequency domain enables automatic BPM detection and synchronization.
WebAssembly Implementation
Modern browsers can perform Fourier analysis in real-time using WebAssembly. Here's how to integrate it into a web application:
1// Set up Web Audio API analyzer
2const audioContext = new AudioContext();
3const analyser = audioContext.createAnalyser();
4analyser.fftSize = 2048;
5
6const bufferLength = analyser.frequencyBinCount;
7const dataArray = new Uint8Array(bufferLength);
8
9// Connect audio source
10navigator.mediaDevices.getUserMedia({ audio: true })
11 .then(stream => {
12 const source = audioContext.createMediaStreamSource(stream);
13 source.connect(analyser);
14
15 // Animation loop for real-time visualization
16 function draw() {
17 requestAnimationFrame(draw);
18
19 // Get frequency data
20 analyser.getByteFrequencyData(dataArray);
21
22 // Visualize on canvas
23 for (let i = 0; i < bufferLength; i++) {
24 const barHeight = dataArray[i];
25 // Draw frequency bar
26 canvasContext.fillRect(i * barWidth,
27 canvas.height - barHeight,
28 barWidth,
29 barHeight);
30 }
31 }
32
33 draw();
34 });
Conclusion
The Fourier Transform is the mathematical bridge between time and frequency domains, enabling virtually all modern audio processing. From streaming music to AI-powered composition, this 200-year-old mathematical technique continues to shape how we create, analyze, and experience music.