Sound sampling explained

How sound is stored as numbers for GCSE Computer Science: sampling, sample rate, bit depth and working out file size. See a sensor sampled fast and slow, watch bit depth turn a signal into a staircase, hear what a low sample rate does to a tune, and try exam-style questions.

Guidefree, runs in your browser

Sound is a wave. A computer can only store numbers. To record sound, it measures the wave many times a second and stores each measurement as a binary number. This is called sampling. Two settings decide how good the recording is and how big the file is: the sample rate and the bit depth. This page explains both, shows you how to work out the size of a sound file, and ends with exam-style questions and answers.

The BugBot robot on this page cannot record sound. But its distance sensor gives a number that changes over time, like a sound wave does, and a program samples it in the same way. Each demo below is a real program. You can change the numbers and press Run. The chart under the robot shows what was sampled.

Sound is a wave

When something makes a sound, it vibrates. The vibration pushes the air, and the air pressure rises and falls in a wave. Two things describe the wave:

  • Amplitude is how tall the wave is. A taller wave is a louder sound.
  • Frequency is how many waves pass each second. It is measured in hertz (Hz). A higher frequency is a higher note. The A that orchestras tune to is 440 Hz.

A sound wave is analogue. It changes smoothly, and it has a value at every moment. A computer is digital. It stores whole numbers in binary. So a microphone turns the wave into a changing voltage, and a chip called an analogue-to-digital converter measures that voltage again and again.

Sampling

A sample is one measurement of the height of the wave at one moment. Sampling means taking samples at regular intervals, and storing each one as a binary number. Here are eight samples of a wave, one every millisecond, each stored with 3 bits:

time (ms)    0    1    2    3    4    5    6    7
sample       4    6    7    6    4    2    1    2
in binary  100  110  111  110  100  010  001  010

The file holds only the binary numbers. To play the sound back, the computer turns them back into a voltage for a speaker. Anything the wave did between two samples is lost.

Sample rate

The sample rate is the number of samples taken each second. It is measured in hertz: 1 Hz is one sample a second.

The time between two samples is the sample interval. It is 1 ÷ the sample rate. A sample every 0.05 seconds is 20 samples a second, a sample rate of 20 Hz. A sample every 0.5 seconds is 2 Hz.

Here the robot turns on the spot between four boxes. Its distance sensor measures how far away the nearest thing in front of it is. The program takes a sample every 0.05 s and plots it. It also keeps one sample in every ten and plots those as a second line. That is the same signal, sampled every 0.5 s.

The fast line, 20 samples a second, dips 7 times as the robot turns past the boxes. The slow line, 2 samples a second, shows only 5 of the dips and misses the box at about 3 seconds completely.
The program
from bugbot import *
connect()

# change EVERY and press Run
EVERY = 10       # the slow line keeps 1 sample in this many

slow = "every " + str(round(EVERY * 0.05, 2)) + " s"
drive(0, 0, 60)                  # turn on the spot
for i in range(160):             # 8 seconds
    d = distance()               # one sample
    plot("every 0.05 s", d)
    if i % EVERY == 0:
        plot(slow, d)
    wait(0.05)
stop()
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

The fast line is a good copy of the signal. Each dip is a box passing in front of the robot. The slow line has a sample only every 0.5 s. The chart joins its dots with straight lines, so it cuts straight across whatever happened in between. Its dips are too shallow, and one box is not there at all. Try EVERY = 4, a sample every 0.2 s, for a sample rate in between.

A higher sample rate:

  • follows the wave more closely, so the recording sounds more like the original;
  • can record higher notes;
  • makes the file bigger, because there are more samples to store.

A phone call uses 8,000 samples a second, which is enough for speech. A CD uses 44,100.

Bit depth

Each sample is stored as a binary number with a fixed number of bits. That number of bits is the bit depth. Some exam boards call it the sample resolution.

The bit depth decides how many different values a sample can have. These values are called levels:

number of levels = 2 to the power of the bit depth
Bit depth Levels
1 2
2 4
3 8
4 16
8 256
16 65,536

Every sample is rounded to the nearest level. With only a few levels, the rounding is rough and the stored wave turns into a staircase.

Here the robot drives towards a wall and back, taking a sample every 0.05 s. Each reading from 0 to 60 cm is stored twice: once with 2 bits and once with 4 bits. The chart shows the reading, and what each stored copy plays back.

The robot drives from 61 cm to 9 cm from the wall and back. With 2 bits every sample is stored as 0, 20, 40 or 60 cm. With 4 bits the steps are 4 cm apart and follow the reading closely.
The program
from bugbot import *
connect()

# change these two and press Run
LOW = 2          # bits per sample, for the rough copy
HIGH = 4         # bits per sample, for the better copy

TOP = 60         # the biggest distance stored, in cm

def store(cm, bits):
    # the nearest of 2 ** bits levels, 0 to levels - 1
    levels = 2 ** bits
    cm = min(max(cm, 0), TOP)
    return round(cm / TOP * (levels - 1))

def play_back(level, bits):
    # turn a stored level back into cm
    return level * TOP / (2 ** bits - 1)

for i in range(120):             # 6 seconds
    if i < 60:
        drive(100, 0)            # towards the wall
    else:
        drive(-100, 0)           # and back
    d = distance()
    a = store(d, LOW)
    b = store(d, HIGH)
    plot("reading", d)
    plot(str(LOW) + " bits", play_back(a, LOW))
    plot(str(HIGH) + " bits", play_back(b, HIGH))
    if i % 20 == 0:
        print(d, "cm is stored as",
              format(a, "0" + str(LOW) + "b"), "or",
              format(b, "0" + str(HIGH) + "b"))
    wait(0.05)
stop()
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

With 2 bits there are only four levels: 00, 01, 10 and 11. They stand for 0, 20, 40 and 60 cm. So a reading of 28 cm is stored as 01, and it plays back as 20 cm. With 4 bits there are 16 levels, 4 cm apart, and the staircase stays close to the reading. Change HIGH to 6 and there are 64 levels, less than 1 cm apart. The steps are then too small to see.

A higher bit depth:

  • stores each sample more accurately, so the recording sounds closer to the original;
  • makes the file bigger, because every sample takes more bits.

CD audio uses 16 bits, so each sample is one of 65,536 levels.

Quality and file size

Change Quality File size
Higher sample rate better: closer to the original, higher notes kept bigger
Lower sample rate worse: detail between samples is lost smaller
Higher bit depth better: each sample is rounded less bigger
Lower bit depth worse: the sound is rough smaller
Longer recording the same bigger

The file size goes up in step with each setting. Double the sample rate and the file doubles. Double the bit depth and it doubles. A stereo recording has two channels, left and right, so it is twice the size of the same recording in mono.

A tune sampled too slowly

The robot has a small speaker called a piezo. tone(262, 0.4) plays a note of 262 Hz for 0.4 seconds. This program plays a short tune. Then it works out what a recording of the tune would play back if it took only 700 samples a second. There is no microphone on the robot, so the program samples a perfect wave of each note, counts the waves the samples show, and plays that note.

The piezo plays C, E, G and high C: 262, 330, 392 and 523 Hz. Sampled at 700 Hz, the recording plays back 262, 330, 308 and 177 Hz, so the tune goes up and then falls.
The program
from bugbot import *
import math
connect()

# change RATE and press Run
RATE = 700                    # samples per second
TUNE = [262, 330, 392, 523]   # C, E, G, high C, in Hz

def recorded(note, rate):
    # sample one second of the note's wave
    samples = []
    for i in range(rate + 1):
        t = i / rate
        samples.append(math.cos(2 * math.pi * note * t))
    # count the waves the samples show: one
    # each time they cross the middle going up
    waves = 0
    for i in range(1, len(samples)):
        if samples[i - 1] < 0 <= samples[i]:
            waves = waves + 1
    return waves

line = "sampled at " + str(RATE) + " Hz"
for note in TUNE:                 # the tune
    plot("the tune", note)
    tone(note, 0.4)
    plot("the tune", note)
wait(0.5)

heard = []
for note in TUNE:                 # the recording
    back = recorded(note, RATE)
    heard.append(back)
    plot(line, back)
    if back >= 100:
        tone(back, 0.4)
    else:
        wait(0.4)                 # too low for the piezo
    plot(line, back)
print("the tune:    ", TUNE)
print("played back: ", heard)
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

C and E come back right. G and high C come back as lower notes that nobody played. This is called aliasing. When the samples are too far apart, they cannot follow a fast wave, and they join up into a slower one instead. To record a sound properly, the sample rate must be at least twice the highest frequency in it. At 700 samples a second, that means nothing above 350 Hz. People can hear up to about 20,000 Hz, and that is why CDs use 44,100 samples a second. Try RATE = 800 and only the high C goes wrong. Try RATE = 8000, the rate a phone uses, and all four notes come back right.

How to work out the file size of a sound

file size in bits = sample rate × bit depth × length in seconds

For stereo, multiply by 2 as well, one for each channel. Then change the units:

bits  ÷ 8     = bytes
bytes ÷ 1,000 = kilobytes (kB)
kB    ÷ 1,000 = megabytes (MB)

Our lessons use 1,000 bytes in a kilobyte. Some exam boards use 1,024 instead (see the last question on this page). Use the one your board uses, and use it all the way through.

Worked example 1. A phone call is recorded at 8,000 Hz with a bit depth of 8, in mono, for 10 seconds. How big is the file in kB?

bits  = 8,000 × 8 × 10 = 640,000 bits
bytes = 640,000 ÷ 8    = 80,000 bytes
kB    = 80,000 ÷ 1,000 = 80 kB

Worked example 2. One minute of CD audio: 44,100 Hz, 16 bits, stereo. How big is it in MB?

seconds = 1 minute             = 60 s
bits    = 44,100 × 16 × 60 × 2 = 84,672,000 bits
bytes   = 84,672,000 ÷ 8       = 10,584,000 bytes
kB      = 10,584,000 ÷ 1,000   = 10,584 kB
MB      = 10,584 ÷ 1,000       = 10.584 MB, about 10.6 MB

A three-minute song is about 32 MB. That is why music is usually compressed.

Counting the bits

The formula is only counting. This program records the distance sensor as a string of 1s and 0s: 20 samples a second, 6 bits each, for 5 seconds. Then it counts the bits and checks the count against the formula.

100 samples of 6 bits each: the program counts 600 bits, the same as 20 × 6 × 5 from the formula. That is 75 bytes.
The program
from bugbot import *
connect()

# change these three and press Run
RATE = 20        # samples per second
BITS = 6         # bits per sample
SECONDS = 5

TOP = 60         # the biggest distance stored, in cm
levels = 2 ** BITS
recording = ""   # the recording, as 1s and 0s

drive(0, 0, 60)                  # turn past the boxes
for i in range(RATE * SECONDS):
    d = min(distance(), TOP)
    level = round(d / TOP * (levels - 1))
    recording = recording + format(level, "0" + str(BITS) + "b")
    plot("bits stored", len(recording))
    plot("distance, cm", d)
    wait(1 / RATE)
stop()

print("the start:", recording[:30])
print("bits counted:", len(recording))
print("formula:", RATE, "x", BITS, "x", SECONDS, "=",
      RATE * BITS * SECONDS, "bits")
print("bytes:", len(recording) / 8)
print("kB:", len(recording) / 8 / 1000)
Put this demo on your own site

Paste it into a school website, Moodle, Google Sites or a blog. More options on the embed page.

The "bits stored" line is straight. Every sample adds the same number of bits, so the size grows at the same rate the whole time. Set RATE = 2 and BITS = 2 and it ends at 20 bits. Real sound needs far more. Five seconds of CD-quality sound in mono is 44,100 × 16 × 5 = 3,528,000 bits, which is 441,000 bytes, or 441 kB.

Mistakes that lose marks

  • Forgetting to divide by 8. The formula gives bits. Bytes are bits ÷ 8.
  • Using minutes. The formula needs seconds. 2 minutes is 120 seconds.
  • Using kHz as it is. 44.1 kHz is 44,100 Hz.
  • Forgetting stereo. Stereo has 2 channels, so multiply by 2.
  • Multiplying by 2 instead of using a power. 5 bits give 2 to the power 5 = 32 levels, not 10.
  • Mixing 1,000 and 1,024 in the same answer.
  • Saying a higher sample rate makes the sound louder. Loudness is the amplitude. Sample rate and bit depth change how accurate the recording is.
  • Not showing working. Write the formula, then the numbers. You can get marks for the method even if the arithmetic slips.

Practice questions

Use 1,000 bytes in a kilobyte. Answers are below.

Question 1: a robot's voice

A robot stores a 4 second spoken message. It is recorded in mono at a sample rate of 16,000 Hz with a bit depth of 8. Calculate the file size in kilobytes. Show your working.

Question 2: levels

(a) How many levels can a sample have with a bit depth of 5?

(b) A sound engineer wants at least 1,000 levels. What is the smallest bit depth she can use?

Question 3: a doorbell in stereo

A doorbell sound is 2 seconds long. It is recorded in stereo at 22,050 Hz with a bit depth of 16. Calculate the file size in kilobytes.

Question 4: working backwards

A sound file is 240 kB. It was recorded in mono at 8,000 Hz with a bit depth of 8. How many seconds of sound does it hold?

Question 5: swap the settings

Aisha records a song. She then records it again with half the sample rate and double the bit depth.

(a) What happens to the file size? Explain why.

(b) Give one way the new recording is better and one way it is worse.

Question 6: a sensor log

A robot takes a distance reading every 0.25 seconds and stores each one with 6 bits. It records for 2 minutes. How many bytes does the log take?

Answers

Question 1. 64 kB.

bits  = 16,000 × 8 × 4 = 512,000 bits
bytes = 512,000 ÷ 8    = 64,000 bytes
kB    = 64,000 ÷ 1,000 = 64 kB

Question 2. (a) 2 to the power 5 = 32 levels. (b) 10 bits. 9 bits give 512 levels, which is too few. 10 bits give 1,024.

Question 3. 176.4 kB.

bits  = 22,050 × 16 × 2 × 2 = 1,411,200 bits
bytes = 1,411,200 ÷ 8       = 176,400 bytes
kB    = 176,400 ÷ 1,000     = 176.4 kB

Question 4. 30 seconds. Work backwards: turn the size into bits, then divide by the bits used each second.

bytes           = 240 × 1,000        = 240,000 bytes
bits            = 240,000 × 8        = 1,920,000 bits
bits per second = 8,000 × 8          = 64,000 bits
seconds         = 1,920,000 ÷ 64,000 = 30 s

Question 5. (a) The file size stays the same. The size is sample rate × bit depth × time. Halving one number and doubling another leaves the answer the same. (b) Better: each sample is stored more accurately, because there are more levels to round to. Worse: there are half as many samples each second, so detail between the samples is lost and the highest notes may not be recorded.

Question 6. 360 bytes. A reading every 0.25 s is 4 readings a second, so the sample rate is 4 Hz. 2 minutes is 120 seconds.

sample rate = 1 ÷ 0.25    = 4 Hz
bits        = 4 × 6 × 120 = 2,880 bits
bytes       = 2,880 ÷ 8   = 360 bytes

Questions

What is sound sampling?

Sampling is how a computer turns a sound wave into numbers. It measures the height of the wave at regular intervals. Each measurement is called a sample and is stored as a binary number. Played back one after another, the samples rebuild the wave.

What is sample rate?

The number of samples taken each second, measured in hertz (Hz). A sample rate of 44,100 Hz means 44,100 samples every second. A higher sample rate gives a recording closer to the original sound, and a bigger file.

What is bit depth?

The number of bits used to store each sample. It decides how many levels a sample can have: 2 to the power of the bit depth. 8 bits give 256 levels and 16 bits give 65,536. AQA calls it the sample resolution.

How do you calculate the file size of a sound file?

Multiply the sample rate by the bit depth by the length in seconds. That gives the size in bits. For stereo, multiply by 2 as well. Divide by 8 to get bytes, then by 1,000 for kilobytes and by 1,000 again for megabytes. For example, 8,000 Hz × 8 bits × 10 s = 640,000 bits = 80,000 bytes = 80 kB.

How does sample rate affect sound quality and file size?

More samples each second follow the wave more closely and can record higher notes, so the quality is better. But there are more samples to store, so the file is bigger. Double the sample rate and the file size doubles.

Why does a higher bit depth give better quality?

Each sample is rounded to the nearest level. More bits give more levels, closer together, so each sample is rounded less and the stored wave is closer to the real one. With very few bits the wave turns into a rough staircase, like the 2-bit line on this page.

What is the difference between sample rate and bit depth?

Sample rate is how often the wave is measured: how many samples each second. Bit depth is how accurately each measurement is stored: how many bits each sample uses. Raising either one improves quality and makes the file bigger.

What sample rate and bit depth does a CD use?

44,100 samples a second, 16 bits per sample, in stereo. One minute of CD audio is about 10.6 MB before it is compressed.

Why is a sound file bigger in stereo?

A stereo recording stores two channels, one for the left speaker and one for the right. Each channel has its own samples, so there are twice as many to store.

What is aliasing?

When a sound is sampled too slowly, the samples cannot keep up with a fast wave, and they join up into a slower wave that was never there. A high note comes back as a lower one. To avoid it, the sample rate must be at least twice the highest frequency in the sound. This is called the Nyquist theorem. It is A level content, but it explains why CDs use 44,100 samples a second.

Is sound sampling on the GCSE Computer Science specification?

Yes, on all three main boards. OCR J277 covers how sound is sampled and stored, and how sample rate, bit depth and duration affect quality and file size (1.2.4). AQA 8525 covers sampling rate and sample resolution, and asks you to calculate file size in bits as sampling rate × sample resolution × seconds (3.3.7). Pearson Edexcel 1CP2 covers how sound is stored in binary and the effect of sample rate and bit depth (2.2.3). The units are in OCR 1.2.3, AQA 3.3.3 and Edexcel 2.3.1. OCR accepts 1,000 or 1,024 bytes in a kilobyte if you are consistent, AQA uses 1,000 for a kilobyte, and Edexcel uses 1,024 bytes in a kibibyte. Our GCSE lesson F8.8 Sound covers all of this, with F8.1 Bits, bytes and units for the units. At A level, A7.8 Sound and MIDI adds the Nyquist theorem and MIDI. U1.5 Seeing the signal, from our university course, shows how to read charts like the ones on this page.

Learn it step by step

These lessons build the same ideas one at a time, each with tasks the simulator marks.

  1. F8.8 Sound Data representation, GCSE
  2. A7.8 Sound and MIDI Data representation, A level
  3. U1.5 Seeing the signal The robot as a system, University
Open the lessons