Introduction to Data Compression

Welcome to this guide on Data Compression! Have you ever tried packing for a holiday and realised your suitcase simply will not close? What do you do? You roll your clothes tightly or use a vacuum-seal bag to squeeze out the trapped air. That is exactly what data compression does for computer files!

In digital technology, we deal with vast amounts of data every day—from high-definition streaming video and music to photos and text documents. Compression is the science of making these files smaller so they take up less space and travel faster across the internet.

Don't worry if computer file sizes and algorithms seem intimidating at first. By breaking the topic down step-by-step, you will master the key ideas needed for your CCEA AS Level exam in no time!


Why Do We Need Data Compression?

Why do computer scientists spend so much time making files smaller? Here are the four primary reasons:

Conserving Storage Space: Smaller files mean you can store more photos, apps, or songs on your hard drive, solid-state drive (SSD), or smartphone memory.

Faster Transmission Speeds: Smaller files can be downloaded, uploaded, and shared via email or messaging apps much faster because fewer bits need to be sent.

Reduced Bandwidth Consumption: Transmitting uncompressed data requires huge amounts of network bandwidth. Compressed data reduces network congestion and saves internet costs.

Smooth Real-Time Streaming: Services like Netflix, YouTube, and Spotify rely on compression so video and audio can stream steadily without constant buffering or freezing.

Key Takeaway: Compression reduces file size, which saves storage space, speeds up transfer times, and cuts down on bandwidth usage.


The Two Main Types of Compression

In digital technology, all compression methods fall into one of two categories: Lossy Compression and Lossless Compression. Choosing the right one depends on whether you can afford to lose any fine detail from your file.

1. Lossy Compression

Lossy compression reduces file size by permanently removing data that is deemed unnecessary or less noticeable to human senses (such as human vision or hearing).

How it works:

• In an audio file, it removes frequencies that the human ear cannot detect or quiet sounds that are masked by louder ones.

• In an image or video file, it simplifies subtle variations in colour or shades that the human eye barely notices.

Key Characteristics of Lossy Compression:

Irreversible: Once data is removed, it is gone forever. You cannot reconstruct the original uncompressed file.

High Compression Ratio: It achieves dramatically smaller file sizes compared to lossless methods.

Slight Loss in Quality: If compression is set too high, noticeable artefacts or blurring can appear.

Common Lossy File Formats:

JPEG / JPG (Images and photographs)

MP3 / AAC (Audio tracks)

MP4 (Video files)

2. Lossless Compression

Lossless compression reduces file size without losing a single piece of original data. When decompressed, the file is an exact, bit-for-bit replica of the original.

How it works:

Instead of discarding data, lossless algorithms identify repeating patterns or redundancy in the data and represent them more efficiently using shorthand codes or mathematical rules.

Key Characteristics of Lossless Compression:

Fully Reversible: The reconstructed file matches the original perfectly with zero loss of quality.

Lower Compression Ratio: File sizes are reduced, but not nearly as much as with lossy compression.

Essential for Exact Data: Must be used when even a single missing character or pixel would ruin the file.

Common Lossless File Formats:

PNG and GIF (Images, graphics, and logos)

FLAC and ALAC (High-fidelity audio)

ZIP, RAR, and 7z (Archive files containing software, databases, or text)

PDF and plain text documents (TXT, DOCX)

Memory Aid:

LOSSY = Loses some data permanently.

LOSSLESS = Leaves Every Single Symbol intact.


Choosing Between Lossy and Lossless

How do you decide which compression type to use? Ask yourself: "Will a minor change in data break the file?"

Use Lossless when: You are compressing program code, text documents, spreadsheets, medical scans, or system files. If a single number in a financial spreadsheet or a character in a program script changes, the entire file becomes incorrect or unusable.

Use Lossy when: You are streaming music, posting a photo to social media, or watching an online video. The human eye and ear cannot easily tell if subtle background details or frequencies are missing, making the massive reduction in file size well worth it.


How Compression Works: Basic Techniques

Let's look at two common methods used to compress digital data.

Technique 1: Run-Length Encoding (RLE)

Run-Length Encoding (RLE) is a simple form of lossless compression. It looks for repeated sequences of identical data values (called a "run") and replaces them with just two items: the count (frequency) and the value itself.

Example with Text:

Imagine uncompressed text containing repeated characters:

AAAAABBBCCCCCCDDDDDDDD

Instead of storing all \(22\) individual characters, RLE records the number of times each character repeats:

5A3B6C8D

• Original length: \(22\text{ characters}\)

• Compressed length: \(8\text{ characters}\)

• In this instance, RLE significantly reduces the number of characters needed!

Example with Simple Graphics:

Consider a row of black (B) and white (W) pixels in a simple monochrome icon:

W W W W W B B B W W W W

Using RLE, this row is stored simply as: 5W 3B 4W.

When is RLE ineffective?
If data has very little repetition (e.g., A B C D E F G), RLE might attempt to store it as 1A1B1C1D1E1F1G, which actually doubles the file size! Therefore, RLE is best suited for images with large flat blocks of colour or data with frequent identical repeats.

Technique 2: Dictionary-Based Compression

Another popular lossless technique searches for recurring groups of words or bit patterns across a file and builds a lookup table (dictionary).

Example:

Consider this sentence:
"The rain in Spain falls mainly in the plain."

Notice the repeated patterns like "ain" and "in ". The algorithm creates an index:

• Index \(1\) = "ain"

• Index \(2\) = " in "

It then replaces instances in the text with short numerical pointers. Because short pointer values take up far fewer bits than whole words or repeated phrases, the total file size drops noticeably.


Calculating Compression Ratios

In your exams, you may be asked to evaluate how effectively a file has been compressed.

1. Compression Ratio Formula:

\(\text{Compression Ratio} = \frac{\text{Uncompressed File Size}}{\text{Compressed File Size}}\)

For example, if an uncompressed video is \(100\text{ MB}\) and after compression it becomes \(10\text{ MB}\):

\(\text{Compression Ratio} = \frac{100\text{ MB}}{10\text{ MB}} = 10:1\)

2. Percentage Reduction Formula:

\(\text{Percentage Reduction} = \left(\frac{\text{Original Size} - \text{Compressed Size}}{\text{Original Size}}\right) \times 100\)

Using the same example:

\(\text{Percentage Reduction} = \left(\frac{100 - 10}{100}\right) \times 100 = 90\%\)


Common Mistakes to Avoid

Mistake 1: Stating that lossy compression can be reversed if you have the right software.
Correction: Lossy compression permanently discards data. It can never be restored to its original state.

Mistake 2: Thinking lossless compression reduces file sizes as much as lossy compression.
Correction: Lossless compression is constrained because every single bit must be preserved. Lossy compression achieves far smaller sizes.

Mistake 3: Confusing file formats.
Correction: Remember that JPEG, MP3, and MP4 are lossy, whereas PNG, ZIP, and FLAC are lossless.


Quick Review Quiz

Test your understanding with these quick check questions:

Q1: Why is lossy compression unsuitable for compressing program source code?
Answer: Program code requires exact syntax. Losing even a single character would cause compilation or execution errors.

Q2: State two advantages of compressing video files before streaming them online.
Answer: It reduces the bandwidth required to transmit the stream and allows faster loading with less buffering.

Q3: If a \(40\text{ MB}\) raw image file is compressed to \(8\text{ MB}\), what is the compression ratio?
Answer: \(\frac{40}{8} = 5:1\).