Unit 1: Digital information · Converting analog data to binary

Article

Sampling sound, pictures and other analog data

7 min read

A singer's voice rises and falls smoothly, with no steps and no gaps. A computer cannot store something smooth. It can only store separate numbers. So how does a song get onto your phone?

The answer: the computer measures the sound again and again, and keeps only the measurements.

Analog and digital

Analog data changes smoothly. Between any two values there are more values. The temperature of a room, the loudness of a sound and the brightness of a light are all analog.

Digital data is made of separate steps, stored as bits. To put analog data in a computer, we must change it to digital. This is analog-to-digital conversion. It has two parts: take measurements, then write each measurement as a binary number. Here is the whole journey of a sound:

From a sound wave to bits

  1. Smooth sound

    no steps and no gaps

    measure again and again
  2. Samples

    e.g. 2.3, 5.8, 9.6

    round each one
  3. Whole numbers

    2, 6, 10

    write in binary
  4. Bits

    0010 0110 1010

The copy is close but not perfect. The values between measurements are lost, and rounding changes each value a little.

Sampling sound

Sampling means measuring an analog signal at regular times. Each measurement is a sample. The table shows a sound measured once every second. Each true value is rounded to the nearest whole number, and the whole number is stored in 4 bits.

Time (s)True valueStored numberStored bits
02.320010
15.860110
29.6101010
311.2111011
48.791001
54.140100

Here is the stored sound as a picture. Each bar is the size of one stored number.

time 0 | ##           2
time 1 | ######       6
time 2 | ##########   10
time 3 | ###########  11
time 4 | #########    9
time 5 | ####         4

Two settings decide how close the copy is to the original:

  • The sample rate is how many samples are taken each second. A higher rate catches more of the changes between measurements. Real recordings take thousands of samples a second.
  • The bit depth is how many bits each sample uses. With 4 bits there are 16 possible levels. With 8 bits there are 256. More levels mean less rounding.

A higher sample rate and a higher bit depth make a better copy, but they also make the file bigger. The copy is never perfect, because the values between samples are lost and every sample is rounded.

Check your understanding

A phone measures a sound 8,000 times each second. What does the number 8,000 describe?

Pictures are samples too

A photo is analog light. To store it, a camera measures the light at a grid of points. Each point is a pixel, and the grid is the picture.

Each pixel stores three numbers, one each for the amount of Red, Green and Blue light. This is RGB. If each number uses 8 bits, it can be from 0 to 255.

ColourRedGreenBlue
Red25500
Green02550
Blue00255
Black000

Worked example

A tiny picture is 2 pixels wide and 2 pixels tall, and each pixel uses 3 numbers of 8 bits.

  1. The picture has 2 × 2 = 4 pixels.
  2. One pixel uses 3 × 8 = 24 bits.
  3. The whole picture uses 4 × 24 = 96 bits. That is 96 / 8 = 12 bytes.

A real photo has millions of pixels, so it needs millions of times more.

Check your understanding

A picture stores each pixel as three numbers (red, green and blue), and each number uses 4 bits. How many bits does one pixel use?

Common mistakes

  • Thinking digital copies are perfect. Sampling throws away what happens between the samples, and rounding changes each value a little.
  • Mixing sample rate and bit depth. The rate is how often you measure. The depth is how many bits each measurement gets.
  • Counting levels as the number of bits. 4 bits give 16 levels, not 4.
  • Forgetting the three colour numbers. One pixel is not one number. It is three.

On the exam

Expect questions that ask what changes when you sample more often or use more bits per sample: the copy is closer to the original, and the file is bigger. Be ready to say that digital data is an approximation of analog data. You may also need to multiply: samples, bits per sample and time, or pixels and bits per pixel.

Key words

Analog: data that changes smoothly and has no separate steps.

Digital: data stored as separate steps, which are bits.

Sampling: measuring an analog signal at regular times.

Sample rate: the number of samples taken each second.

Bit depth: the number of bits used for each sample.

Pixel: one point of a picture, stored as numbers.

RGB: a way to store a colour as an amount of red, green and blue.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.