Fundamentals 8 min read

Unveiling Music’s Hidden Frequencies: Fourier Series Explained with Python

This article introduces Fourier series, explains how any musical note can be decomposed into its constituent frequencies, demonstrates generating a composite waveform with Python, computes its Fourier coefficients, visualizes the results, and discusses practical applications such as audio analysis, instrument classification, and digital music compression.

Model Perspective
Model Perspective
Model Perspective
Unveiling Music’s Hidden Frequencies: Fourier Series Explained with Python
When you sit at a piano and gently press a key, you hear a single, pure note, but behind that simplicity lies a rich world of frequencies; every note is actually a sum of multiple frequency components, a phenomenon explored using Fourier series.

Fourier Series

Fourier Series, invented by French mathematician Jean‑Baptiste Joseph Fourier, expresses a periodic function as an infinite sum of sine and cosine terms. Even non‑periodic functions can be treated as periodic by extending their domain.

Application in Music

Fourier series can be used to analyze the frequency components of a musical note. Although a note sounds single, it consists of a fundamental frequency and its harmonics. The fundamental determines the perceived pitch, while higher harmonics are integer multiples of that pitch.

Below we generate a simple musical note signal composed of three sine waves representing the fundamental and its first two harmonics, then compute its Fourier coefficients to identify the dominant frequencies.

<code>import numpy as np
import matplotlib.pyplot as plt

# 设置参数
fs = 44100  # 采样频率,这是一个常见的音频采样频率
T = 1.0    # 信号的总时间(秒)
t = np.linspace(0, T, int(T*fs), endpoint=False)  # 时域

# 基频
f0 = 440  # A4 音符的频率

# 生成信号,这是基频及其前两个谐波的组合
y = np.sin(2 * np.pi * f0 * t)           # 基频
y += 0.5 * np.sin(2 * np.pi * 2 * f0 * t)  # 第二个谐波
y += 0.3 * np.sin(2 * np.pi * 3 * f0 * t)  # 第三个谐波

# 绘制信号
plt.figure(figsize=(10,4))
plt.plot(t[:1000], y[:1000])  # 只显示前1000个点以便查看波形
plt.title('Generated Musical Note')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.grid(True)
plt.show()

# 返回生成的信号 y 和时间 t
y, t
</code>

The generated waveform combines the fundamental (A4) and its first two harmonics, forming a composite wave in the time domain.

Next we compute the first ten Fourier coefficients to identify the significant frequency components.

<code># 计算前10个Fourier系数
N = 10
a = np.zeros(N)
b = np.zeros(N)

for n in range(N):
    a[n] = (2/T) * np.trapz(y * np.cos(2 * np.pi * n * f0 * t), t)
    b[n] = (2/T) * np.trapz(y * np.sin(2 * np.pi * n * f0 * t), t)

# 绘制系数
plt.figure(figsize=(10,6))

plt.subplot(2,1,1)
plt.stem(range(N), a, use_line_collection=True)
plt.title('Cosine Coefficients $a_n$')
plt.grid(True)

plt.subplot(2,1,2)
plt.stem(range(N), b, use_line_collection=True)
plt.title('Sine Coefficients $b_n$')
plt.grid(True)

plt.tight_layout()
plt.show()

a, b
</code>

From the plots we observe:

Cosine coefficients are near zero, indicating minimal contribution from cosine terms.

Sine coefficients’ first three values are prominent (≈1, 0.5, 0.3), matching the amplitudes of the three sine waves used to create the note.

The dominant frequencies correspond to the fundamental, the second harmonic, and the third harmonic.

Analyzing musical components has several practical applications:

Understanding the spectral content of a music segment aids production and mixing.

Helps identify and classify different musical styles or instrument timbres.

Enables digital music compression (e.g., MP3) by removing less perceptible frequency components.

Behind every musical note lies a magical world of frequencies waiting to be explored; just as Fourier series reveals the complex structure of sound, every story or creative idea has hidden depth and charm.
PythonAudio Analysissignal processingMusicFourier series
Model Perspective
Written by

Model Perspective

Insights, knowledge, and enjoyment from a mathematical modeling researcher and educator. Hosted by Haihua Wang, a modeling instructor and author of "Clever Use of Chat for Mathematical Modeling", "Modeling: The Mathematics of Thinking", "Mathematical Modeling Practice: A Hands‑On Guide to Competitions", and co‑author of "Mathematical Modeling: Teaching Design and Cases".

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.