― Visualizing and Hearing Math with Python (Google Colab)
Author: Math & Sound Engineering Lab
Date: November 13, 2025
1. Overview
This project converts trigonometric identities into audible sine-wave tones, allowing students and engineers to “hear” the difference between formulas such as the addition theorem, double-angle, or half-angle formulas.
Using NumPy, matplotlib, and soundfile, the program synthesizes short tones whose waveforms directly reflect the structure of each trigonometric equation.
All code runs natively in Google Colab without any external dependencies.
2. Concept
Every trigonometric formula represents a relationship between waves.
For instance:
| Formula | Mathematical Meaning | Acoustic Analogy |
|---|---|---|
| sin(A + B) | Interference between two signals | Beat tone |
| sin(A − B) | Phase difference | Slight detuning |
| sin 2θ | Frequency doubling | 2× harmonic |
| sin(θ⁄2) | Frequency halving | One octave below |
| sin A cos B | Product–sum mixing | AM-type modulation |
| sin A + sin B | Sum–product blending | Polyphonic beating |
By mapping these formulas to time-varying sine waves, mathematics becomes directly perceivable through sound.
3. Implementation (Python)
# Program Name: trig_allformulas_sineplayer_colab_en_singleton.py
# Overview: Sequential playback of all trigonometric formula tones + one pure sine reference tone.
!pip install numpy scipy soundfile matplotlib --quiet
import numpy as np, soundfile as sf, matplotlib.pyplot as plt, IPython.display as ipd, re
SR, DURATION, AMP, BASE_FREQ = 44100, 2.0, 0.4, 440.0
def safe_filename(name):
return re.sub(r'[^A-Za-z0-9_\-]', '_', name)
def formula_sine(name, func, freq_factor):
t = np.linspace(0, DURATION, int(SR*DURATION))
A = 2*np.pi*BASE_FREQ*freq_factor[0]*t
B = 2*np.pi*BASE_FREQ*freq_factor[1]*t
y = AMP*func(A,B,t)/np.max(np.abs(func(A,B,t)))
fname = safe_filename(name)+".wav"
sf.write(fname, y, SR)
print(f"▶ {name}")
plt.figure(figsize=(8,1.8)); plt.plot(t[:2000], y[:2000]); plt.title(name)
plt.xlabel("Time [s]"); plt.ylabel("Amplitude"); plt.show()
ipd.display(ipd.Audio(fname, rate=SR))
formulas = {
"Addition Formula sin(A+B)": (lambda A,B,t: np.sin(A+B), (1.0,1.5)),
"Subtraction Formula sin(A-B)": (lambda A,B,t: np.sin(A-B), (1.0,0.75)),
"Double Angle Formula sin(2θ)": (lambda A,B,t: np.sin(2*A), (0.5,1.0)),
"Half Angle Formula sin(θ/2)": (lambda A,B,t: np.sin(A/2), (2.0,1.0)),
"Product-to-Sum Formula": (lambda A,B,t: (np.sin(A+B)+np.sin(A-B))/2, (1.0,1.25)),
"Sum-to-Product Formula": (lambda A,B,t: 2*np.sin((A+B)/2)*np.cos((A-B)/2), (1.0,1.5))
}
for name,(func,factors) in formulas.items():
formula_sine(name, func, factors)
print("▶ Pure Sine Reference Tone (A4 = 440 Hz)")
t = np.linspace(0, DURATION, int(SR*DURATION))
y = AMP*np.sin(2*np.pi*BASE_FREQ*t)
sf.write("Pure_Sine_440Hz.wav", y, SR)
plt.figure(figsize=(8,1.8)); plt.plot(t[:2000], y[:2000])
plt.title("Pure Sine Reference (440 Hz)"); plt.xlabel("Time [s]"); plt.ylabel("Amplitude"); plt.show()
ipd.display(ipd.Audio("Pure_Sine_440Hz.wav", rate=SR))
4. How It Works
-
Wave Generation
Each formula defines a mathematical transformation of sine wavesAandB.
Different frequency ratios (1.0,1.5, etc.) simulate harmonic intervals such as octaves or fifths. -
Normalization & Output
The synthesized waveform is normalized to ±0.4 amplitude and written as a 44.1 kHz WAV file. -
Visualization
A short time window (2 000 samples) is plotted to show the oscillatory pattern of each equation. -
Playback
IPython.display.Audio()embeds a real-time player in Colab so users can listen directly.
5. Results
Running the cell in Colab plays six successive tones:
- Addition Formula → Complex beat
- Subtraction Formula → Slightly detuned oscillation
- Double Angle → Higher frequency (2× pitch)
- Half Angle → Lower pitch (½ frequency)
- Product-to-Sum → Mixed modulated tone
- Sum-to-Product → Rich amplitude-modulated pattern
- Pure Sine (440 Hz) → Reference tone for comparison
Each sound corresponds to the mathematical structure of the equation—an audible demonstration of trigonometric synthesis.
6. Educational Significance
- Mathematics + Music Integration: Demonstrates that trigonometric identities underpin musical acoustics and signal theory.
- STEAM Education: Bridges abstract formulas with experiential sound.
- Signal Processing Insight: Visualizes frequency addition, subtraction, and modulation using simple math.
7. Possible Extensions
-
Spectrogram Visualization
Addlibrosa.display.specshow()to visualize frequency content in time. -
MIDI Export
Convert sine tones into MIDI notes for use in DAWs. -
Interactive GUI
Allow real-time formula switching and pitch selection withipywidgets. -
Polyphonic Combination
Mix multiple formulas simultaneously to explore harmonic interference.
Conclusion
This experiment shows that trigonometric formulas are not just symbols—they are sound generators.
By turning math into music, learners can intuitively grasp how addition, subtraction, and modulation shape the world of waves, audio, and beyond.