Welcome to Data Representation!

In this chapter, we are going to explore how computers take the messy, "curvy" world we live in (sounds, colors, and sights) and turn it into the only thing they understand: bit patterns (sequences of 0s and 1s). Whether you are an aspiring game dev or someone who just wants to pass their exams, understanding how a photo or a song is actually "stored" is the foundation of everything in Computer Science.

Don't worry if this seems a bit abstract at first! We will break it down into simple analogies to make it click.


1. Analogue vs. Digital

Before we can store data, we need to understand the difference between the "real world" and the "computer world."

Analogue Data and Signals

Analogue data is continuous. Imagine a slide on a playground; you can be at any height on that slide. Sound waves in the air are analogue—they vary constantly in frequency and amplitude.

Digital Data and Signals

Digital data is discrete. Imagine a set of stairs. You are either on one step or the next; you can't be "between" steps. Computers use digital data because they use transistors that are either on (1) or off (0).

How do we convert between them?

To bridge the gap, we use two special pieces of hardware:
1. ADC (Analogue to Digital Converter): This takes a continuous signal (like your voice into a microphone) and "samples" it to turn it into binary numbers.
2. DAC (Digital to Analogue Converter): This takes binary numbers and turns them back into a continuous signal (like playing music through a speaker).

Quick Review:
- Analogue = Continuous (like a slide).
- Digital = Discrete (like stairs).
- ADC = Real world to Computer.
- DAC = Computer to Real world.


2. Representing Images: Bitmapped Graphics

How does a computer store a picture of a cat? It uses a Bitmap.

The Basics

A Bitmap is an image made up of tiny dots called pixels (short for "picture elements"). Each pixel is assigned a binary pattern to represent its color.

Key Terms for Bitmaps

Resolution: This is often expressed as the number of dots per inch (DPI). The more pixels in a given area, the higher the resolution and the clearer the image.
Size in Pixels: The total dimensions of the image, calculated as Width in pixels × Height in pixels.
Colour Depth: The number of bits used to represent the color of a single pixel.
- If you use 1 bit per pixel, you can only have 2 colors (0=Black, 1=White).
- If you use 8 bits, you can have \( 2^8 = 256 \) colors.

Calculating Storage Requirements

To find out how big an image file will be (ignoring metadata), use this simple formula:
File Size (in bits) = Total Number of Pixels × Colour Depth

Example: An image is 100 pixels wide and 100 pixels high, with a color depth of 8 bits.
1. Total pixels: \( 100 \times 100 = 10,000 \)
2. File size: \( 10,000 \times 8 = 80,000 \) bits.
3. To get bytes, divide by 8: \( 80,000 / 8 = 10,000 \) bytes.

What is Metadata?

Metadata is "data about data." It is a small header at the start of the file that tells the computer how to display the bits. Typical metadata includes the image width, image height, and color depth. Without it, the computer would just see a long string of 1s and 0s and wouldn't know where one row of pixels ends and the next begins!

Key Takeaway: More pixels or more colors mean a better-looking image, but a much larger file size!


3. Representing Sound

To store sound, the computer "samples" the sound wave at regular intervals.

The Sampling Process

Sampling Rate: How many times per second the computer measures the amplitude of the sound wave. This is measured in Hertz (Hz).
Sample Resolution: The number of bits used to store each measurement (the amplitude). Higher resolution means the volume levels are more accurate.

The Nyquist Theorem

Don't let the name intimidate you! The Nyquist Theorem simply states that to capture a sound accurately, your sampling rate must be at least twice the highest frequency in the sound. Since humans can hear up to 20,000 Hz, most music is sampled at 44,100 Hz (CD quality) to ensure no detail is lost.

Calculating Sound File Size

Size (bits) = Sampling Rate × Sample Resolution × Duration (seconds)

Did you know?
Telephone calls have a very low sampling rate, which is why voices on the phone sound "thin" or "flat" compared to hearing someone in person!


4. MIDI (Musical Instrument Digital Interface)

MIDI is completely different from normal audio files like MP3s. It doesn't store the "sound" at all!

How MIDI Works

A MIDI file is a list of instructions or event messages that tell a digital instrument (like a keyboard or synthesizer) how to play a piece of music. Messages include:
- Which note to play.
- How hard to hit the key (velocity).
- When to stop playing the note.
- Which instrument sound to use.

Advantages of MIDI

1. Tiny File Size: Since it's just text instructions, the files are much smaller than recorded audio.
2. Easy to Edit: You can change a single note or change the whole song from a piano to a trumpet with one click.
3. No Background Noise: Since it's synthesized, there is no "hiss" or "crackles."

Analogy: A standard audio file is like a CD recording of a concert. A MIDI file is like the Sheet Music. The sheet music isn't the sound, but it tells the performer exactly what to do.


5. Data Compression

We compress files to save storage space and make them faster to download or send over the internet.

Lossy vs. Lossless

Lossless Compression: No data is lost. When you decompress the file, it is 100% identical to the original. This is vital for text files or computer programs.
Lossy Compression: Some data is permanently removed to save space. It discards things the human eye or ear can't easily perceive (like very high-pitched sounds). This is used for JPEGs and MP3s.

Two Lossless Methods you need to know:

1. Run Length Encoding (RLE): Instead of storing every single pixel, it stores the color and then how many times it repeats.
Example: Instead of "Red, Red, Red, Red, Red", it stores "5 Red". This works great for images with large areas of the same color (like a logo).
2. Dictionary-Based Methods: The computer builds a "dictionary" of frequently occurring patterns (like the word "the" in a book) and replaces them with a short binary code. It's like using shorthand in your notes!

Common Mistake to Avoid: Don't assume "Lossy" is always bad. While it loses quality, it allows us to stream high-definition video over the internet. Without lossy compression, YouTube and Netflix wouldn't work!


Quick Review Box

1. Bitmaps use pixels and color depth.
2. Sound uses sampling rate and sample resolution.
3. MIDI is a set of instructions, not recorded sound.
4. RLE is a lossless compression method that counts repeating data.
5. Metadata tells the computer how to interpret the bit patterns.