Welcome to Data Representation!
Ever wondered how a computer, which only understands 1s and 0s, can show you a high-definition movie or play your favorite song? In this chapter, we explore how real-world data like images and sound are converted into bit patterns that a computer can process. Don't worry if some of the math looks intimidating at first—we'll break it down step-by-step!
1. Analogue vs. Digital
Before we store data, we need to understand what kind of data it is. In the real world, most things are analogue. This means they are continuous, with infinite possible values. Think of a traditional dimmer switch; you can slide it to any brightness level. Digital data is discrete, meaning it has specific, separate values (like an on/off switch).
Analogue to Digital Conversion (ADC)
Since computers can't store infinite "analogue" waves, they use an Analogue to Digital Converter (ADC). This device takes "samples" of the analogue signal at regular intervals and converts them into binary values. You’ll find ADCs in analogue sensors (like a thermometer) and microphones.
To play the sound back, we use a Digital to Analogue Converter (DAC). This turns the binary bit patterns back into a continuous signal that speakers can use to move air and create sound.
Quick Review:
• Analogue: Continuous (the real world).
• Digital: Discrete/Binary (the computer).
• ADC: Analogue input → Binary output.
• DAC: Binary input → Analogue output.
2. Representing Images
There are two main ways computers represent images: Bitmaps and Vectors.
Bitmapped Graphics
A bitmap image is like a piece of graph paper where every tiny square (a pixel) is filled with a specific color.
Key Terms for Bitmaps:
• Resolution: The number of pixels per inch (often called dots per inch or DPI). The higher the resolution, the clearer the image.
• Size in Pixels: Calculated as \( \text{width} \times \text{height} \).
• Colour Depth: The number of bits used to represent the color of a single pixel. If you use 1 bit, you can only have 2 colors (black and white). If you use 8 bits, you can have \( 2^8 = 256 \) colors.
Calculating Storage Requirements:
To find out how big a bitmap file is (ignoring metadata), use this formula:
\( \text{Storage Requirement} = \text{Width in pixels} \times \text{Height in pixels} \times \text{Colour Depth} \)
Example: An image is 100x100 pixels with a color depth of 8 bits.
\( 100 \times 100 \times 8 = 80,000 \text{ bits} \). To get bytes, divide by 8: \( 10,000 \text{ bytes} \).
Vector Graphics
Instead of storing every pixel, Vector graphics store a list of objects. Each object (like a circle, line, or rectangle) has properties like its position, thickness, and color.
Analogy: A bitmap is like a photo of a cake; a vector is the recipe to bake the cake.
Bitmaps vs. Vectors:
• Bitmaps are best for photographs where every pixel might be a different color. They get "pixelated" (blurry) when you zoom in.
• Vectors are best for logos, fonts, and icons. They can be scaled up to the size of a skyscraper without losing any quality, and their file size is usually much smaller!
Did you know? Bitmap files also contain Metadata. This is "data about data"—extra info like the image width, height, color depth, and the date it was taken.
3. Representing Sound
To turn a sound wave into numbers, we "sample" its amplitude (height) at specific points in time.
Sampling Principles
• Sampling Rate: How many samples we take per second, measured in Hertz (Hz).
• Sample Resolution: How many bits we use to store the value of each sample. More bits mean we can record the "height" of the wave more accurately.
The Nyquist Theorem: To record a sound accurately, the sampling rate must be at least double the highest frequency in the sound.
\( \text{Sampling Rate} \ge 2 \times \text{Highest Frequency} \)
Calculating Sound File Size:
\( \text{Size in bits} = \text{Sampling Rate} \times \text{Sample Resolution} \times \text{Seconds} \)
Musical Instrument Digital Interface (MIDI)
MIDI is not a recording of sound! It is a set of event messages (instructions) that tell a digital instrument what to play. It stores things like "Note On," "Note Off," "Pitch," and "Volume."
Advantages of MIDI:
• Files are tiny (perfect for slow internet or old devices).
• Easy to edit (you can change a piano note to a guitar note instantly).
• No background noise is recorded.
4. Data Compression
Files like 4K videos are too big to send easily, so we use compression to shrink them.
Lossy vs. Lossless
• Lossy Compression: Permanently removes data that humans probably won't notice (like very high-pitched sounds or subtle color changes). Example: JPEG, MP3.
• Lossless Compression: Shrinks the file without losing any data. When you "unzip" it, it is identical to the original. Example: PNG, ZIP.
Lossless Techniques
1. Run Length Encoding (RLE): Instead of storing "AAAAA", it stores "5A". This works great for images with big blocks of the same color.
2. Dictionary-based methods: The computer builds a "dictionary" of common patterns. Instead of storing the whole pattern again, it just stores a short reference number to the dictionary.
Common Mistake: Students often think Lossy compression is always "bad." It’s actually great for streaming video or music because it makes the files small enough to send over the internet in real-time!
5. Encryption
Encryption is the process of scrambling data so that only someone with the correct key can read it. The original message is plaintext, and the scrambled message is ciphertext.
The Caesar Cipher
This is a simple "shift" cipher. For example, with a shift of 3, 'A' becomes 'D'.
Why it's weak: It is very easy to crack using frequency analysis (counting how often letters appear) or just trying all 25 possible shifts (brute force).
The Vernam Cipher (One-Time Pad)
The Vernam cipher is the only cipher that offers perfect security, provided these rules are followed:
• The key must be truly random.
• The key must be at least as long as the plaintext.
• The key must only be used once (hence "one-time pad").
In the Vernam cipher, the plaintext and the key are combined using the XOR logical operation. Because the key is random, the resulting ciphertext is also completely random, making it impossible to crack without the key.
Key Takeaway:
While the Caesar cipher depends on the secrecy of the algorithm (which is easy to guess), the Vernam cipher depends on computational security and the randomness of the key.