Lesson 58: Sampling sound
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version.
From sound wave to numbers
- Sound is a wave: the voltage from a microphone goes up and down smoothly.
- A computer cannot store a smooth wave. It samples it: it measures the height again and again, at fixed times.
- Each measurement is stored as a binary number.
Words for this lesson
| Word | Meaning |
|---|---|
| sample | one measurement of the wave's height |
| sample rate | how many samples are taken each second (Hz) |
| sample resolution | how many bits each sample has |
More or fewer bits?
| Resolution | Values | Result |
|---|---|---|
| 10 bits | 0 to 1023 | close to the real sound, bigger file |
| 2 bits | 0 to 3 | very rough sound, tiny file |
In the exam
- 0478/11/M/J/23 Q2(a): state what is meant by sample rate and sample resolution. Mark scheme: the sample rate is the number of samples taken in a second; the sample resolution is the number of bits per sample.
- 0478/12/O/N/23 Q5: a band records a song. (a) A benefit of a higher sample rate: the recording is more accurate, closer to the original. (b) A drawback: the file size increases. (c) Sample resolution: the number of bits per sample, which sets how many different amplitudes can be stored.
Difficulty 3/5 · 200 coins + a small bonus on first completion today
A microphone turns sound into a voltage that goes up and down. Recording sound means sampling: measuring the voltage again and again at fixed times. Take 8 samples, one every 100 ms: wait 100 ms, then store analogRead(A0) in samples[i]. The program prints them.
Click Run to see the output here.
Difficulty 4/5 · 280 coins + a small bonus on first completion today
Now store the same sound with a sample resolution of 2 bits. samples[i] >> 8 shifts the 10 bits right by 8, so only the top 2 bits are left: a value from 0 to 3. Print 2-bit: and the 8 small values, like the first line.
Click Run to see the output here.