Data Storage · Cambridge syllabus 1.3 · Lesson 3 of 3
Data Compression
Compression makes a file smaller, so it needs less storage space, less bandwidth and less time to transmit. Lossy compression removes data permanently, for example by lowering the resolution or colour depth of an image or the sample rate or resolution of a sound. Lossless compression keeps all the data, so the original file can be rebuilt exactly; run length encoding (RLE) is one lossless method.
By the end, you can
- Understand the purpose of and need for data compression.
- Explain how compression reduces the bandwidth, storage space and transmission time needed.
- Understand how lossy compression permanently removes data.
- Give examples of lossy compression: reducing resolution, colour depth, sample rate or sample resolution.
- Understand how lossless compression reduces file size without permanent loss of data.
- Use run length encoding (RLE) as a lossless method.
- Choose lossy or lossless compression for a given file.
Why compress data
Compression makes a file smaller. Images, sound and video take up a great deal of space in their raw form, as the file-size calculations show, and a smaller file is cheaper to keep and quicker to send.
The syllabus names three effects. A compressed file needs less storage space, so more files fit on a device or in a cloud account. It needs less bandwidth, so it uses less of a network connection while it is sent. And it has a shorter transmission time, so it downloads or uploads faster.
Those effects matter most where space or speed is limited: sending photos from a phone over a mobile connection, streaming music, attaching a file to an email that has a size limit, or storing thousands of images on a website.
There are two ways to compress a file, and the choice between them depends on whether any of the data can be lost.
Worth remembering
- Compression exists to reduce the size of a file.
- Smaller files need less storage space and less bandwidth.
- Smaller files have a shorter transmission time.
Lossy and lossless compression
Lossy compression reduces the file size by permanently removing data. For an image this means reducing the resolution, so there are fewer pixels, or reducing the colour depth, so each pixel uses fewer bits. For a sound it means reducing the sample rate or the sample resolution. The removed data cannot be put back, so the quality falls, but the file can become much smaller.
Lossless compression reduces the file size without permanently losing any data, so the original file can be rebuilt exactly. One lossless method is run length encoding (RLE). It looks for runs of the same value and stores each run as the value and how many times it repeats.
For example, a row of pixels W W W W W B B W W W W can be stored by RLE as 5 W, 2 B, 4 W. That is six values instead of eleven, and reading them back gives exactly the same row. RLE works well when there are long runs, such as a simple logo with large areas of one colour, and badly when values change often, such as a photograph, where the encoded file can even be larger.
Choosing between them comes down to whether data may be lost. Text, program files and spreadsheets must use lossless compression, because losing a single character or bit would change their meaning. Photos, music and video can usually use lossy compression, because people do not notice small losses and the saving is much bigger.
Worth remembering
- Lossy removes data permanently: lower resolution, colour depth, sample rate or resolution.
- Lossless loses nothing; the original can be rebuilt exactly.
- RLE stores each run as a value and a count: W W W W B B becomes 4 W, 2 B.
- Text and programs need lossless; photos and music can use lossy.
Worked example
RLE stores a run of repeated values as the value and how many times it repeats. The pixel colours W W W W B B W W W are stored as 4 W, 2 B, 3 W: six values instead of nine.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
Lossless, because every bit of a program must be kept exactly: a program with data removed would not run correctly.
Check your method, units and explanation against it.
Marked practice
Set 1 of 4 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.