Year 9 · Audio-Visual · Part A: Storing information

Compression

In one line

Compression stores the same information in fewer bits. Lossless gets every bit back. Lossy throws away detail you probably won't notice.

1

Explain why we compress, and the difference between lossless and lossy.

2

Encode and decode with run-length encoding (RLE).

3

Build a Huffman tree, read the codes off it, and count the bits saved compared with ASCII.

4

Work out a compression ratio and the percentage saved, and choose lossy or lossless for a job.

LosslessSmaller file; the exact original comes back.
LossyMuch smaller file; some detail is gone for ever.
RLERun-length encoding: "how many, then what".
Huffman codingCommon characters get short codes.
Compression ratioOriginal size ÷ new size.

Why compress?

Raw pictures and sounds are huge. In Topics 5 and 6 you worked out the sizes. Compression makes files smaller, so they take less space and travel faster.

A 12 MP photo, raw36 MB
As a JPEG≈ 3 MB
A 3-minute song, CD31.8 MB
As an MP3≈ 3 MB
  1. Storage. A phone holds about 10 times more photos and songs.

  2. Bandwidth: faster downloads and streaming. A connection carries a fixed number of bits each second. Fewer bits = less waiting.

  3. Less mobile data and lower cost. Mobile plans charge by the gigabyte. Companies pay for every byte they store and send.

  4. Some things only work compressed. Raw Full HD video at 30 frames a second is about 1.5 gigabits every second. Streaming it raw is impossible for almost every home connection.

By hand: how big is one second of raw HD video?

  1. One Full HD frame: 1920 × 1080 = 2,073,600 pixels.

  2. × 24 bits per pixel = 49,766,400 bits per frame.

  3. × 30 frames a second = 1,492,992,000 bits ≈ 1.5 billion bits per second (1,500 Mbps).

  4. A typical home connection is 50–100 Mbps. Streaming services send HD at about 5 Mbps. That's about 300 times smaller, all thanks to compression.

Lossless vs lossy

There are two families of compression. Lossless rewrites the data more cleverly, and gets back every single bit. Lossy throws away detail for ever, to make the file much smaller.

Each square is a piece of information. Lossless packs all of it more tightly and unpacks it exactly. Lossy drops some pieces (faded) to pack even tighter. Unpacking can't bring them back.
LosslessLossy
What happensWrites repetition and patterns more cleverlyRemoves detail for ever
Original back?Yes, exactly, bit for bitNo: a close copy
Size savingMedium (often 2–3 times smaller)Large (often 10 times smaller or more)
MethodsRLE, Huffman coding, ZIPJPEG, MP3, AAC, MP4 (H.264)
Used forText, code, spreadsheets, PNG, ZIP, FLACPhotos, music, video, streaming
!Watch out: when lossy is never OK

Text and code. One wrong bit can change a letter, a number or an instruction. "Pay £100" could become "Pay £900". A program with one changed bit may crash.

Also: medical scans (a missing detail could be a tumour), legal documents, bank records, and any file you will edit and save again and again (each lossy save loses more).

By hand: ratio and percentage saved

A 48-bit message is compressed to 9 bits.

  1. Ratio: old ÷ new = 48 ÷ 9 = 5.33. It's 5.33 times smaller. Write it "5.33 : 1".

  2. Fraction left: new ÷ old = 9 ÷ 48 = 0.1875. So 18.75% of the file is left.

  3. Fraction saved: 1 − 0.1875 = 0.8125.

  4. Percentage saved: 0.8125 × 100 = 81.25%.

  5. Shortcut: saved bits ÷ old bits × 100. (48 − 9) ÷ 48 × 100 = 39 ÷ 48 × 100 = 81.25%. Same answer.

Run-length encoding (RLE)

RLE is lossless. A run is the same thing repeated in a row. Instead of writing it out every time, write how many, then what.

  1. Start with AAAABBBCCD (10 characters).

  2. Four A's in a row → 4A.

  3. Three B's → 3B.

  4. Two C's → 2C.

  5. One D → 1D.

  6. Result: 4A3B2C1D. 8 characters instead of 10. Ratio 10 ÷ 8 = 1.25. Saved 2 ÷ 10 = 20%.

!Watch out

ABCD becomes 1A1B1C1D: twice as long. RLE only helps when things repeat next to each other. "BANANA" has three A's but never two in a row, so RLE makes it worse.

Decoding: turn it back

  1. Read 3W2B5W in pairs: (3, W) (2, B) (5, W).

  2. Write each letter that many times: WWW · BB · WWWWW.

  3. Join them: WWWBBWWWWW. Exactly the original: that's what lossless means.

★Exams

AQA 8525 asks you to RLE-encode data as frequency/data pairs, e.g. 4 A 3 B, or for a bitmap 8 0 3 1. OCR J277 only expects you to know lossy vs lossless and their uses, not RLE or Huffman by name.

RLE on a black-and-white image

Remember 1-bit images from Topic 5: 1 = black, 0 = white. Pictures often have long runs of the same colour. RLE stores each run as a count and a colour.

Each run takes 6 bits: 5 bits for the count (up to 31) and 1 bit for the colour. 3 runs × 6 = 18 bits, instead of 24.
▶

Paint a row, then compress it

Tap pixels to switch them between white and black. Each run costs 6 bits (5 for the count, 1 for the colour).

?Why?

Why is RLE good for cartoons and screenshots but bad for photos? Cartoons have big areas of exactly the same colour: long runs. In a photo, neighbouring pixels are almost always slightly different, so most runs are only 1 pixel long.

Huffman coding: build the tree

Huffman coding is lossless. ASCII gives every character 8 bits. Huffman gives common characters short codes and rare ones longer codes. It builds a tree to decide the codes.

The rule never changes: take the two smallest, join them, put the total back. Stop when one tree is left. Then label every left branch 0 and every right branch 1.
  1. Count each letter in BANANA: A = 3, N = 2, B = 1. This is the frequency table.

  2. Sort smallest first: B(1), N(2), A(3).

  3. Join the two smallest: B(1) + N(2) → a new node worth 3. Put it back in the list: A(3), node(3).

  4. Join again: A(3) + node(3) → 6. Only one tree is left, and 6 is the length of the word. Done.

  5. Label branches: left = 0, right = 1.

  6. Read each code by walking from the top to the letter: A = 0, B = 10, N = 11.

  7. Encode: B A N A N A → 10 0 11 0 11 0 → 100110110 = 9 bits. 8-bit ASCII needs 6 × 8 = 48 bits.

?Why?

How does the decoder know where each code ends? No code is the start of another one (0, 10, 11). This is called a prefix code. Read left to right, and the first code that matches is always right. 1 → not a code yet. 10 → B! Then 0 → A! And so on.

!Watch out

When two totals are equal, you may pick either. Different choices give different codes, but the same total number of bits. Exam mark schemes accept any correct tree.

Build a Huffman tree for any word

Type a word or a short sentence. Step through the merges one at a time, then read the codes off the tree.

▶

Huffman builder

The list, smallest first (highlighted = the two joined next; dashed = a tree built so far)

How many bits did Huffman save?

To count the total, you don't need to write out the whole bit string. For each letter: how many times it appears × the length of its code. Add them up.

By hand: from a code table

LetterAppears (n)CodeLength (L)n × L
E5015
T31026
A211036
Z111133
  1. Multiply each row: 5 × 1 = 5, 3 × 2 = 6, 2 × 3 = 6, 1 × 3 = 3.

  2. Add them: 5 + 6 + 6 + 3 = 20 bits with Huffman.

  3. Total letters: 5 + 3 + 2 + 1 = 11. In 8-bit ASCII: 11 × 8 = 88 bits.

  4. Saved: 88 − 20 = 68 bits. Percentage: 68 ÷ 88 × 100 = 77.3%. Ratio: 88 ÷ 20 = 4.4.

!Watch out

Real files must also store the tree (or the code table), so the decoder can rebuild the codes. For a short word that table can cost more than it saves. Huffman pays off on long files. Exam questions usually ignore the table.

★Exams

AQA 8525 asks you to interpret a Huffman tree, write codes from it, and compare the bits with ASCII (7-bit or 8-bit: check the question). Building a tree from scratch is good practice and helps you understand it.

How lossy decides what to throw away

Lossy compression uses perceptual coding: it studies what human eyes and ears notice, and drops what they don't. The file shrinks a lot, and most people can't tell.

All three throw away information your senses would mostly miss. The trick is choosing what to lose.
  1. JPEG (photos): eyes see fine detail in brightness much better than fine detail in colour. So JPEG keeps brightness for every pixel, but stores colour once for each small block. It also rounds off tiny, fine changes in each 8 × 8 block.

  2. MP3 (music): it removes sounds too high for most people, and quiet sounds hidden under louder ones at the same moment (called masking).

  3. Video (MP4): most of each frame is the same as the one before. So it stores a full frame now and then, and in between stores only what moved or changed.

▶

Lossy demo: squash a picture

A colourful picture drawn by maths. Choose how much to throw away. The right-hand view shows what was lost, made 4 times brighter so you can see it.

Original: 24 bits per pixel
Compressed
What was lost (×4)
Levels per value (quantisation: round every value to fewer steps)
Colour detail (brightness is always kept for every pixel)
Original
Compressed
Ratio
Saved
!Watch out

Press "256 levels" and "Every pixel" after squashing hard. You get the original back only because the toy redraws it from the maths. A real JPEG has no maths to redraw from: the lost detail is gone for ever. Saving a JPEG again and again loses a little more each time ("generation loss").

Choosing: lossy or lossless?

Ask one question: would losing a tiny bit of detail matter? If yes, use lossless. If no, lossy gives a much smaller file.

JobChooseWhy
Holiday photos on social mediaLossy (JPEG)Nobody notices, uploads are fast
Streaming music or videoLossy (AAC, MP4)Must fit through the connection
A Python program sent by emailLossless (ZIP)One wrong bit can break it
A school report or contractLosslessEvery word and number must be exact
An X-ray or MRI scanLosslessA lost detail could be missed by the doctor
A logo or screenshot with flat coloursLossless (PNG)Lossy makes blurry "fringes"; lossless compresses flat colours well anyway
Master recording in a music studioLossless (FLAC, WAV)It will be edited many times
e.g.Example

A ZIP file is lossless. It uses ideas like RLE and Huffman together, plus a trick that replaces repeated phrases with a pointer back to where they first appeared. Text files often shrink to a third of their size.

Common mix-ups

The mistakes nearly everyone makes the first time. Read the left side, then the right.

People think

Compression always makes a file smaller.

Actually

Not always. RLE on ABCD doubles it. Zipping a JPEG barely helps: it's already compressed.

People think

Lossless means it doesn't get smaller.

Actually

It gets smaller and comes back exactly. It just can't shrink as much as lossy.

People think

You can unzip a JPEG back to the original photo.

Actually

Lossy detail is gone for ever. Decompressing gives a close copy, never the original.

People think

In Huffman, the most common letter gets the longest code.

Actually

The shortest. It's used most, so short codes save the most bits.

People think

In a Huffman tree you join the two biggest.

Actually

Always join the two smallest. Rare letters end up deep in the tree, with long codes.

People think

RLE counts every A in the text, like Huffman.

Actually

RLE only counts letters in a row. BANANA has no runs at all.

People think

Ratio and percentage saved are the same number.

Actually

48 → 12 bits: ratio = 4 (4 : 1), saved = 75%.

Check yourself

Quick questions first. Then endless practice: a wrong answer shows you the working, step by step.

[ {"q":"RLE of WWWWBB is…","o":["4W2B","W4B2","6WB"],"a":0,"w":"Four W's, then two B's: how many, then what."}, {"q":"Which is lossy?","o":["ZIP","PNG","JPEG"],"a":2,"w":"JPEG throws detail away. ZIP and PNG are lossless."}, {"q":"In Huffman coding, the most common letter gets…","o":["The shortest code","The longest code","An 8-bit code"],"a":0,"w":"Common letters are used a lot, so short codes save the most."}, {"q":"When building a Huffman tree, at each step you join…","o":["The two biggest","The two smallest","The first two letters"],"a":1,"w":"Join the two smallest totals, put the new total back, repeat."}, {"q":"A 200 KB file compresses to 50 KB. The percentage saved is…","o":["25%","75%","4%"],"a":1,"w":"(1 − 50 ÷ 200) × 100 = (1 − 0.25) × 100 = 75%. The ratio is 4 : 1."}, {"q":"You need to send a Python program. Which compression?","o":["Lossy, it's smaller","Lossless, every bit must be exact"],"a":1,"w":"One changed bit could break the program."}, {"q":"Codes: A = 0, B = 10, C = 11. How many bits for AABC?","o":["4","6","32"],"a":1,"w":"A (1) + A (1) + B (2) + C (2) = 6 bits."}, {"q":"MP3 saves space mainly by…","o":["Removing sounds people can't hear well","Writing runs of samples as pairs","Sorting the samples"],"a":0,"w":"Perceptual coding: very high sounds, and quiet sounds masked by loud ones, are dropped."} ]