Data to audio and back. Modulation / demodulation with source code
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Converting digital data into audio and then recovering it again is a classic modulation and demodulation problem. The idea is to map bits onto an audio waveform that can travel through speakers, microphones, or recorded files. A simple frequency-shift keying design is enough to demonstrate the full round trip in code.
Pick a Simple Modulation Scheme
For an introductory implementation, binary frequency-shift keying is practical:
- one frequency represents bit
0 - another frequency represents bit
1
Example mapping:
- '
0uses1200hertz' - '
1uses2200hertz'
Each bit is sent for a fixed duration. The receiver analyzes each chunk and decides which frequency is stronger.
Convert Bits into Audio Samples
The transmitter generates a sine wave for each bit interval.
This produces a NumPy array of audio samples that can be saved or played.
Save the Audio to a WAV File
Using a WAV container makes inspection and playback easier.
At this point, message.wav contains the modulated bitstream.
Demodulate by Measuring Energy
The receiver splits the signal into fixed-size bit windows and measures how strongly each target frequency is present.
This is a simple coherent detector. It is not industrial-grade, but it is enough to show the principle.
Convert Recovered Bits Back to Text
Once you have the bitstream, convert it back into bytes.
For a clean generated signal, the recovered text should match the original input.
Add Framing and Error Detection
Real systems need more than raw bits. Without framing, the receiver does not know where the message begins. Without error detection, noise can silently corrupt content.
Practical additions include:
- a preamble tone or bit pattern
- a length field
- a checksum or CRC
Those features are what separate a toy modem from a usable transport.
Real-World Constraints
Audio modulation becomes harder once you leave the ideal all-digital simulation. Real microphones and speakers introduce:
- noise
- clipping
- timing drift
- frequency response limits
That means a robust modem must handle synchronization, filtering, and threshold tuning. Simple frequency detection still teaches the fundamentals, but real deployments need more engineering.
Common Pitfalls
- Choosing bit durations too short for reliable frequency detection.
- Forgetting framing, so the receiver cannot align message boundaries.
- Using frequencies too close together for the sample rate and channel quality.
- Assuming a clean generated WAV behaves like a real acoustic path.
- Ignoring error detection when turning noisy analog signals back into bytes.
Summary
- Audio data transfer works by mapping bits onto sound wave properties.
- Binary frequency-shift keying is a simple modulation scheme for demonstrations.
- Modulation generates waveform chunks, one per bit interval.
- Demodulation compares energy at expected frequencies for each chunk.
- Real systems need framing, timing control, and error detection beyond the basic example.

