Core Principles of Data Storage and File Compression | 数据存储与文件压缩的核心原理

📚 Core Principles of Data Storage and File Compression | 数据存储与文件压缩的核心原理

Data storage and file compression are foundational topics in computer science. Every file, whether it is a text document, an image, an audio recording, or a video, is ultimately stored as a sequence of binary digits (bits). Understanding how data is represented, how storage capacity is measured, and how compression algorithms reduce file size is essential for any computer science student aiming for success in the CIE examinations.

数据存储与文件压缩是计算机科学中的基础课题。无论是文本文档、图像、音频录制,还是视频文件,最终都以二进制数字(比特)序列的形式存储。理解数据如何表示、存储容量如何测量,以及压缩算法如何减小文件大小,对于任何希望在CIE考试中取得优异成绩的计算机科学学生来说都至关重要。


1. Binary Representation of Data | 数据的二进制表示

Computers use the binary system because they are built from electronic switches that have two stable states: on (1) and off (0). A single binary digit is called a bit, and it is the smallest unit of data in a computer. Eight bits form one byte, which can represent 256 different values (2⁸ = 256).

计算机使用二进制系统,因为它们由具有两种稳定状态的电子开关构成:开(1)和关(0)。单个二进制数字称为比特(bit),是计算机中最小的数据单位。八个比特组成一个字节(byte),可以表示256种不同的值(2⁸ = 256)。

  • Bit: A single 0 or 1. | 比特:一个单独的0或1。

  • Byte: 8 bits, the standard unit for measuring file size. | 字节:8个比特,是衡量文件大小的标准单位。

  • Nibble: 4 bits, equivalent to one hexadecimal digit. | 半字节:4个比特,相当于一个十六进制数字。

All data — numbers, text, images, sound and video — must be converted into binary patterns before it can be processed or stored by a computer. The process of converting real-world information into binary is called encoding.

所有数据——数字、文本、图像、声音和视频——在计算机处理和存储之前,都必须转换为二进制模式。将现实世界信息转换为二进制的这一过程称为编码。


2. Storage Units: From Bits to Terabytes | 存储单位:从比特到太字节

Storage capacity is measured using a hierarchy of units. In the CIE syllabus, students must be able to convert between these units and understand the relationship between them.

存储容量通过单位层级来衡量。CIE教学大纲要求学生能够在这些单位之间进行换算,并理解它们之间的关系。

Unit 单位 Size 大小 Approximate Value 近似值
Bit 比特 1
Byte 字节 8 bits 1 character 1个字符
Kilobyte (KB) 千字节 1024 bytes 约1000字节;半页文本
Megabyte (MB) 兆字节 1024 KB 约100万字节;一本短篇小说
Gigabyte (GB) 吉字节 1024 MB 约10亿字节;一部高清电影
Terabyte (TB) 太字节 1024 GB 约1万亿字节;大型数据库

A common misconception is that 1 KB equals exactly 1000 bytes. In computer science, the binary system defines 1 KB as 2¹⁰ = 1024 bytes. However, some storage manufacturers use decimal values of 1000. CIE expects candidates to use 1024 in calculations unless otherwise stated.

一个常见误解是1 KB恰好等于1000字节。在计算机科学中,二进制体系定义1 KB为2¹⁰ = 1024字节。然而,一些存储制造商使用1000的十进制值。除非另有说明,CIE考试期望考生在计算中使用1024。


3. Number Systems and Text Representation | 数制与文本表示

Denary (base 10) uses digits 0–9. Binary (base 2) uses digits 0 and 1. Hexadecimal (base 16) uses digits 0–9 and letters A–F. Hexadecimal is a compact way to represent binary values and is widely used in memory addresses and colour codes.

十进制(基数10)使用0–9的数字;二进制(基数2)使用0和1;十六进制(基数16)使用0–9以及字母A–F。十六进制是表示二进制值的紧凑方式,广泛应用于内存地址和颜色代码。

Text characters are stored using character coding schemes. The most important schemes for CIE are ASCII and Unicode.

文本字符通过字符编码方案存储。CIE最重要的编码方案是ASCII和Unicode。

  • ASCII uses 7 or 8 bits per character, allowing 128 or 256 characters. It covers basic Latin letters, digits and punctuation. | ASCII每个字符使用7或8位,可表示128或256个字符,涵盖基本拉丁字母、数字和标点。

  • Unicode uses 16 or 32 bits per character, allowing over one million characters. It supports all world languages and special symbols. | Unicode每个字符使用16或32位,可表示超过一百万个字符,支持所有世界语言和特殊符号。

For example, in ASCII, the uppercase letter ‘A’ is represented as decimal 65, which in binary is 01000001. In Unicode, ‘A’ is still 65, but the same coding space also contains ‘中’ as decimal 20013, which requires multiple bytes.

例如,在ASCII中,大写字母’A’以十进制65表示,即二进制01000001。在Unicode中,’A’仍然是65,但同一个编码空间也包含’中’这个字符,其十进制为20013,这需要多个字节来表示。


4. Representing Images and Sound | 图像与声音的表示

Digital images are composed of a grid of pixels. Each pixel stores a numerical value that represents a colour. The colour depth determines how many bits are used per pixel. For example, a 1-bit image can show 2 colours (black and white), while an 8-bit image can show 256 colours, and a 24-bit image can show 16.7 million colours.

数字图像由像素网格组成。每个像素存储一个数值来表示颜色。颜色深度决定每个像素使用的比特数。例如,1位图像可以显示2种颜色(黑白),8位图像可以显示256种颜色,而24位图像可以显示1670万种颜色。

Sound is represented by sampling. A sound wave is continuous, so the computer measures its amplitude at regular intervals, a process called sampling. The sampling rate is the number of samples taken per second, measured in hertz (Hz). A CD-quality audio file uses a sampling rate of 44,100 Hz and a sample resolution of 16 bits.

声音通过采样来表示。声波是连续的,因此计算机按固定时间间隔测量其振幅,这个过程称为采样。采样率是每秒采样的次数,以赫兹(Hz)为单位。CD音质的音频文件使用44,100 Hz的采样率和16位的采样分辨率。

  • Higher sampling rate = better quality but larger file size. | 采样率越高 = 质量越好但文件越大。

  • Higher sample resolution = more precise amplitude values but larger file size. | 采样分辨率越高 = 振幅值更精确但文件越大。

  • More channels (stereo vs mono) also increase file size. | 更多声道(立体声对比单声道)也会增大文件大小。


5. Calculating File Size and Storage Requirements | 计算文件大小与存储需求

CIE examinations frequently ask candidates to calculate file sizes. The formulas below are essential.

CIE考试经常要求考生计算文件大小。以下公式是必须掌握的。

Image file size = width × height × colour depth (in bits)

图像文件大小 = 宽 × 高 × 颜色深度(比特)

Audio file size = sample rate × sample resolution × number of channels × duration (in seconds)

音频文件大小 = 采样率 × 采样分辨率 × 声道数 × 时长(秒)

Text file size = number of characters × bits per character

文本文件大小 = 字符数 × 每字符比特数

Worked example: A 1000 × 800 image with a colour depth of 16 bits has a size of 1000 × 800 × 16 = 12,800,000 bits = 1,600,000 bytes ≈ 1.53 MB. Whether you convert using 1024 or 1000, you must state your units clearly and show all steps.

示例:一张1000 × 800、颜色深度为16位的图像,其大小为1000 × 800 × 16 = 12,800,000比特 = 1,600,000字节 ≈ 1.53 MB。无论是用1024还是1000进行换算,都必须清楚注明单位,并展示所有计算步骤。


6. File Compression: Why We Need It | 文件压缩:为什么需要它

File compression is the process of reducing the number of bits required to represent information. Large files consume more storage space and take longer to transfer over networks, which increases bandwidth usage and cost.

文件压缩是减少表示信息所需比特数的过程。大文件占用更多存储空间,并需要更长的网络传输时间,这会增加带宽消耗和成本。

Compression can be divided into two main categories: lossless and lossy compression.

压缩可分为两大类:无损压缩和有损压缩。

Feature 特征 Lossless 无损 Lossy 有损
Data recovery 数据恢复 Perfect, identical to original 完美,与原始相同 Approximate, some data lost 近似,丢失部分数据
Compression ratio 压缩率 Lower 较低 Higher 较高
Typical files 典型文件 Text, programs, spreadsheets 文本、程序、电子表格 Images, audio, video 图像、音频、视频
Examples 示例 ZIP, RLE, Huffman, LZW JPEG, MP3, MP4

7. Lossless Compression: Run-Length Encoding (RLE) | 无损压缩:行程长度编码(RLE)

Run-length encoding is a simple lossless technique that replaces consecutive repeated characters with a count and a single character. It is most effective when data contains many runs, such as simple black-and-white images or repetitive text.

行程长度编码是一种简单的无损技术,用计数加单个字符来替换连续重复的字符。当数据包含大量连续重复内容时(如简单的黑白图像或重复性文本),它的效果最佳。

Example: The string “AAAAABBBBCCCC” contains 14 characters. Using RLE, we can represent it as 5A4B4C, which is only 6 characters long.

示例:字符串”AAAAABBBBCCCC”包含14个字符。使用RLE,我们可以将其表示为5A4B4C,仅6个字符长。

One crucial limitation is that RLE performs poorly on random or non-repetitive data, and in the worst case it may even expand the file. For this reason, more sophisticated methods are often preferred.

一个关键局限性是,RLE在随机或非重复数据上效果很差,在最坏情况下甚至可能使文件变大。因此,人们通常选择更复杂的方法。


8. Lossless Compression: Huffman Coding | 无损压缩:霍夫曼编码

Huffman coding is a statistical lossless compression method that assigns shorter codes to frequently occurring characters and longer codes to less frequent characters. It uses a frequency table to build a binary tree, from which variable-length codes are derived.

霍夫曼编码是一种统计型无损压缩方法,它为频繁出现的字符分配较短的编码,为不常出现的字符分配较长的编码。它使用频率表构建二叉树,并从中派生出变长编码。

Example: Consider a file containing only the letters A, B, C, D with frequencies A=50%, B=25%, C=12.5%, D=12.5%. A possible Huffman coding might be:

示例:假设一个文件只包含字母A、B、C、D,频率分别为A=50%,B=25%,C=12.5%,D=12.5%。一个可能的霍夫曼编码可能是:

  • A → 1 (1 bit) | A → 1(1位)

  • B → 01 (2 bits) | B → 01(2位)

  • C → 001 (3 bits) | C → 001(3位)

  • D → 000 (3 bits) | D → 000(3位)

Compared with fixed-length coding, where each character needs 2 bits, Huffman coding reduces the average code length to 0.5 × 1 + 0.25 × 2 + 0.125 × 3 + 0.125 × 3 = 1.75 bits per character, a clear saving.

与固定长度编码(每个字符需要2位)相比,霍夫曼编码将平均编码长度降至0.5 × 1 + 0.25 × 2 + 0.125 × 3 + 0.125 × 3 = 每字符1.75位,明显节省了空间。

A key feature of Huffman codes is that they are prefix-free, meaning no code is a prefix of another, which is necessary for unambiguous decoding.

霍夫曼编码的一个关键特性是无前缀,即没有任何代码是另一个代码的前缀,这对于无歧义解码是必需的。


9. Lossless Compression: Dictionary-Based Methods (LZW) | 无损压缩:字典编码方法(LZW)

LZW (Lempel-Ziv-Welch) is a dictionary-based lossless compression algorithm. It builds a dictionary of frequently appearing patterns and replaces those patterns with short index codes. Common formats that use LZW include GIF and TIFF images.

LZW(Lempel-Ziv-Welch)是一种基于字典的无损压缩算法。它构建常用模式的字典,并用短索引码替换这些模式。使用LZW的常见格式包括GIF和TIFF图像。

During compression, the algorithm reads input symbols and builds progressively longer patterns. The first occurrence of a pattern is output as the original symbols, and later occurrences are replaced by dictionary entries. Because the dictionary is constructed during both compression and decompression, it does not need to be stored separately.

在压缩过程中,算法读取输入符号并逐步构建更长的模式。模式的首次出现以原始符号输出,后续出现则替换为字典条目。由于解压缩过程中也会同步构建字典,因此无需单独存储字典。

LZW is particularly effective on text and graphical data with repeated patterns, such as computer-generated images with large areas of uniform colour.

LZW特别适用于具有重复模式的文本和图形数据,例如包含大面积均匀颜色的计算机生成图像。


10. Lossy Compression: JPEG and MP3 | 有损压缩:JPEG与MP3

Lossy compression permanently removes data that the human eye or ear cannot easily perceive. This technique achieves much smaller file sizes but cannot reconstruct the original file exactly. Two key applications are JPEG for images and MP3 for audio.

有损压缩会永久删除人眼或人耳不易察觉的数据。这项技术能实现更小的文件大小,但无法精确重建原始文件。两个关键应用是图像的JPEG和音频的MP3。

JPEG compression works in several stages: the image is divided into 8×8 blocks, a discrete cosine transform (DCT) is applied, high-frequency coefficients are reduced or discarded, and the results are quantised and entropy-coded. The degree of compression can be controlled by a quality factor; a higher quality setting preserves more detail but produces a larger file.

JPEG压缩通过几个阶段进行:图像被划分为8×8数据块,应用离散余弦变换(DCT),减少或丢弃高频系数,然后对结果进行量化和熵编码。压缩程度可以通过质量因子来控制;质量设置越高,保留的细节越多,但生成的文件越大。

MP3 compression uses a psychoacoustic model to remove sounds that are masked by louder sounds at similar frequencies. It reduces a CD-quality audio file to roughly one-tenth of its original size while maintaining acceptable listening quality. Other lossy audio formats include AAC and WMA.

MP3压缩利用心理声学模型,去除被相近频率的响亮声音掩蔽的音频内容。它可以将CD音质的音频文件减小到原始大小的十分之一左右,同时保持可接受的收听质量。其他有损音频格式包括AAC和WMA。


11. Video Compression and Compression Ratios | 视频压缩与压缩比

Video files combine both image and audio data, and they often have very large sizes. Video compression therefore relies on both spatial compression (within a single frame) and temporal compression (between frames). Temporal compression stores only the differences between consecutive frames rather than every full frame.

视频文件结合了图像和音频数据,通常体积非常大。因此,视频压缩既依赖空间压缩(帧内),也依赖时间压缩(帧间)。时间压缩只存储连续帧之间的差异,而不是每一帧的完整数据。

MPEG-4 and H.264 are common standards for video compression. They use reference frames (I-frames) and predicted frames (P-frames). This approach dramatically reduces data, making streaming and storage practical.

MPEG-4和H.264是常见的视频压缩标准。它们使用参考帧(I帧)和预测帧(P帧)。这种方法大幅减少数据量,使流媒体播放和存储变得切实可行。

The compression ratio is defined as the original file size divided by the compressed file size. For example, if a 20 MB file is compressed to 5 MB, the compression ratio is 4:1. A higher ratio generally means lower quality for lossy methods.

压缩比定义为原始文件大小除以压缩后文件大小。例如,如果一个20 MB的文件被压缩到5 MB,压缩比为4:1。对于有损方法,更高的压缩比通常意味着更低的画质。

Compression ratio = original size ÷ compressed size

压缩比 = 原始大小 ÷ 压缩后大小


12. Applications of Compression in the Real World | 压缩在现实世界中的应用

File compression is everywhere in modern computing. Without it, streaming services, cloud storage, and digital communication would be impractical.

文件压缩在现代计算中无处不在。没有它,流媒体服务、云存储和数字通信都将不切实际。

Practical applications include:

实际应用包括:

  • Email attachments can be zipped into a single archive using lossless compression, reducing upload and download time. | 电子邮件附件可以使用无损压缩打包成单个归档文件,从而减少上传和下载时间。

  • Web images are saved as JPEG or WebP to reduce page load times, while diagrams and logos are often saved as PNG or GIF for lossless quality. | 网页图像保存为JPEG或WebP以缩短页面加载时间,而图表和徽标常保存为PNG或GIF以保证无损质量。

  • Video streaming services such as YouTube use lossy compression to reduce bandwidth demand, while security and medical imaging systems prefer lossless formats to avoid losing critical details. | YouTube等视频流媒体服务使用有损压缩来降低带宽需求,而安防和医学影像系统倾向于无损格式,以避免丢失关键细节。

  • Backup systems often use lossless compression so that data can be restored perfectly after an error or failure. | 备份系统通常使用无损压缩,以便在发生错误或故障后能够完美恢复数据。

Choosing the right compression method depends on the context. If perfect accuracy is essential, lossless compression must be used. If the main goal is to minimise file size while retaining acceptable quality, lossy compression is appropriate.

选择正确的压缩方法取决于具体场景。如果必须保证完美准确性,就必须使用无损压缩。如果主要目标是在保留可接受画质的同时最小化文件大小,则适合使用有损压缩。


Published by TutorHao | Computer Science Revision Series | aleveler.com

更多咨询请联系16621398022(同微信)

Comments

屏轩国际教育cambridge primary/secondary checkpoint, cat4, ukiset,ukcat,igcse,alevel,PAT,STEP,MAT, ibdp,ap,ssat,sat,sat2课程辅导,国外大学本科硕士研究生博士课程论文辅导Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from aleveler.com

Subscribe now to keep reading and get access to the full archive.

Continue reading

Exit mobile version