AP Computer Science Principles · Unit 2 lecture · Part 5 of 11 · 7 min

2.1 Analog data and samplingTurning smooth signals into numbers

Mr. Thompkins
The real world is smooth. A violin note slides, a sunset fades, a runner moves continuously. Computers only store separate numbers. So how does a song get into your phone? By taking snapshots, very fast.

Analog data have values that change smoothly over time, rather than jumping between separate steps. The pitch and volume of music, the colors of a painting, and the position of a sprinter are all analog. Digital data, the kind computers store, are separate, countable values.

To store analog data, a computer samples it: it measures the value of the analog signal at regular intervals. Each measurement is a sample, and each sample is stored as a binary number. The stored samples approximate the original. Using digital data to approximate real-world analog data is another example of abstraction: it keeps what matters and drops the rest.

0481216200246810time (ms)level
Sampling every 1 ms (blue): the samples follow the gray analog wave closely.
0481216200246810time (ms)level
Sampling every 4 ms (red): only six samples, and the wiggles are lost.
?Wait, what does "approximate" mean here?
An approximation is close but not exact. Between two samples the computer knows nothing, and each sample is rounded to a value it can store. So a digital copy is never identical to the analog original, only close.

Two choices control how good the copy is. The sampling rate is how often you measure: more samples per second catch faster changes. The bits per sample decide how finely each measurement is recorded: more bits, more possible levels, less rounding. Both improve the copy, and both make the file bigger. That trade-off, quality against size, is the theme of the next two sections.

?Wait, how many levels do 16 bits allow?
2¹⁶ = 65,536 different values. A CD-quality audio sample uses 16 bits, so each measurement can be any of 65,536 levels, which is far finer than the 11 levels in the graphs above.
Pause and think
A music app offers "standard" and "high" quality. High quality uses twice as many samples per second. Predict two things that change.
I thought about it. Show me
The recording follows the original sound more closely, especially quick, high sounds, and the file roughly doubles in size, because there are twice as many samples to store.
How big is a minute of music?
CD-quality audio records 44,100 samples per second, uses 16 bits per sample, and has 2 channels (left and right). How many bits and bytes does one minute take, before any compression?
  1. Samples in one minute, one channel: 44,100 × 60 = 2,646,000.
  2. Two channels double it, and each sample is 16 bits: 2,646,000 × 2 × 16 = 84,672,000 bits.
    Why?
    Multiply everything that "per" applies to: samples per second, seconds, channels, bits per sample.
  3. Divide by 8 bits per byte: 84,672,000 ÷ 8 = 10,584,000 bytes, about 10.6 megabytes.
    Why?
    That's why music files are compressed. You'll see how next.

Answer: 84,672,000 bits, or 10,584,000 bytes (about 10.6 MB), for one minute.

Stuck? Get a hint
  1. Start with samples per second times seconds.
  2. Then multiply by channels and by bits per sample.
  3. Bits to bytes: divide by 8.
Check yourself
Which change makes a digital recording of a sound a closer approximation of the original?
  1. AMeasuring the sound less often
  2. BMeasuring the sound more often
  3. CStoring each sample with fewer bits
  4. DUsing lossy compression on the file
Show the answer

B. A higher sampling rate captures changes in the signal more closely. Fewer bits per sample or lossy compression would make the copy less accurate, not more.

  • Analog data change smoothly. Digital data are separate values.
  • Sampling measures an analog signal at regular intervals; each sample is stored in bits.
  • More samples, or more bits per sample: closer approximation, bigger file.
  • Digital data approximating analog data is an example of abstraction.