Back to Blog
Mathematical Foundations
Technical Deep Dive

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.

Dr. Sarah Chen, Audio Research
January 25, 2025
18 min read
Frequency spectrum visualization

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.

The Fourier Transform Equation

For a continuous signal x(t), the Fourier Transform X(f) is defined as:

X(f)=x(t)ei2πftdtX(f) = \int_{-\infty}^{\infty} x(t) e^{-i2\pi ft} dt

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:

X[k]=n=0N1x[n]ei2πkn/NX[k] = \sum_{n=0}^{N-1} x[n] e^{-i2\pi kn/N}

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.

FFT Implementation Example
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:

X(t,f)=x(τ)w(τt)ei2πfτdτX(t,f) = \int_{-\infty}^{\infty} x(\tau) w(\tau - t) e^{-i2\pi f\tau} d\tau
The Time-Frequency Trade-off

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:

Creating a Spectrogram
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:

m=2595log10(1+f700)m = 2595 \log_{10}(1 + \frac{f}{700})

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

Audio Compression

MP3 and AAC use Fourier analysis to identify and remove frequencies we can't hear, achieving 10:1 compression.

Auto-Tune

Real-time pitch correction analyzes frequency content and shifts it to the nearest musical note.

Music Separation

AI models use spectrograms to identify and isolate individual instruments from mixed recordings.

Beat Detection

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:

Real-time FFT with Web Audio API
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.

Next Steps

Ready to apply Fourier analysis to your own audio projects? Check out our interactive tools: