A1.1.8 · Computer fundamentals
A1.1.8 · 计算机基础
The Concept of Compression
压缩的概念
A file usually contains more bits than it strictly needs: the same value repeated, or detail a person would never notice missing. Compression finds that slack and removes it, either in a way that can be undone exactly, or in a way that cannot.
一个文件通常包含比它严格需要的更多的位:重复的同一个值,或人根本不会察觉少了的细节。压缩找出这些"余量"并去掉它,要么以一种能被完全还原的方式,要么以一种不能还原的方式。
A1's guiding question asks what principles underpin how a computer works. Compression rests on one: real data has redundancy, and redundancy can be encoded more briefly.
A1 的引导性问题问的是支撑计算机运行的原理。压缩建立在一个原理之上:真实的数据存在冗余,而冗余可以用更简短的方式来编码。
Explainer
讲解
What compression is, and why
压缩是什么,以及为什么
Compression is encoding a piece of data using fewer bits than its original representation. The two reasons to do it: the file takes up less storage space, and it is faster to send across a network.
压缩是用比原始表示更少的位来编码一段数据。这样做的两个原因:文件占用更少的存储空间,而且通过网络传输更快。
Explainer
讲解
Lossless vs lossy
无损与有损
| Compared on | Lossless compression | Lossy compression |
|---|---|---|
| Can the original be restored? | Yes, exactly, bit for bit | No, some information is thrown away for good |
| What it removes | Only repeated or predictable information (redundancy), which is put back on decompression | Detail judged unimportant, such as fine detail a person is unlikely to notice missing |
| How much smaller | Modest reduction | Much larger reduction (often more than half) |
| Used for | Text, source code, databases, executables, anything where a single wrong bit matters | Images, audio and video, where a small quality drop is an acceptable trade for a much smaller file |
| 比较项 | 无损压缩 | 有损压缩 |
|---|---|---|
| 能否还原出原始数据? | 能,一位不差地完全还原 | 不能,一部分信息被永久丢弃 |
| 它去掉什么 | 只去掉重复的或可预测的信息(冗余),解压时会把它放回去 | 去掉被判断为不重要的细节,例如人不太可能察觉少了的精细细节 |
| 能小多少 | 缩小幅度不大 | 缩小幅度大得多(常常超过一半) |
| 用于 | 文本、源代码、数据库、可执行文件,以及任何一位出错都要命的数据 | 图像、音频和视频,那里为了文件小得多,可以接受一点点质量下降 |
Try it yourself
动手试试
Run-length encoding
行程长度编码
Run-length encoding (RLE) is a lossless method for data with long runs of the same value. It replaces each run with a pair: how many times, then the value. Click the cells to paint a row of 40 pixels (black or white) and watch the encoding.
行程长度编码(RLE)是一种无损方法,适用于含有很长的同值行程的数据。它把每一段行程替换成一对:重复多少次,然后是那个值。点击格子来涂画一行 40 个像素(黑或白),看看编码结果。
RLE only helps when runs are long. Try the noisy pattern: with the colour changing almost every cell, the encoded version is bigger than the original, because every "1 white, 1 black" pair costs more than the single bit it replaces.
只有行程很长时 RLE 才有用。试试杂乱图案:颜色几乎每一格都在变,编码后的版本反而比原来更大,因为每一对"1 白、1 黑"都比它所替代的那 1 位花费更多。
Explainer
讲解
Transform coding (JPEG, MP3)
变换编码(JPEG、MP3)
Transform coding is a lossy method used in JPEG images and MP3 audio. It works in stages, using JPEG as the example:
变换编码是 JPEG 图像和 MP3 音频所用的一种有损方法。它分阶段进行,以 JPEG 为例:
| Stage | What it does |
|---|---|
| Split into blocks | The image is cut into small square sub-images |
| Transform | Each block is converted from pixel values into frequency values: low frequencies describe broad areas of colour, high frequencies describe fine detail and edges |
| Quantise | The precision of the high-frequency values is reduced, because the eye barely notices small changes to fine detail. This is where information is actually lost, and where the compression level is set |
| Encode | The reduced values are packed down further using lossless steps (including run-length encoding) |
| 阶段 | 它做什么 |
|---|---|
| 切成块 | 图像被切成一个个小的正方形子图 |
| 变换 | 每个块从像素值转换成频率值:低频描述大片的颜色区域,高频描述精细的细节和边缘 |
| 量化 | 降低高频值的精度,因为人眼几乎察觉不到精细细节的微小变化。这一步才是真正丢失信息的地方,也是设定压缩程度的地方 |
| 编码 | 把降低后的这些值用无损的步骤(包括行程长度编码)进一步压缩 |
The quantise stage deliberately drops precision from the high-frequency detail and does not record it anywhere. Turn the quantisation up and the file gets smaller but the image gets blockier. That discarded detail cannot be recovered.
量化阶段故意舍弃高频细节的精度,并且不在任何地方记录它。把量化调大,文件更小,但图像更"块状"。那些被舍弃的细节无法恢复。
Check your understanding
检验你的理解
You need to compress a holiday photo and a spreadsheet of financial figures. Which should use lossless compression?
你需要压缩一张度假照片和一张财务数字的电子表格。哪一个应该用无损压缩?
Common mistake
常见错误
RecurringWeak answers say lossy compression "deletes random data" or "corrupts the file". It removes chosen detail, the parts judged least noticeable, so the result still looks or sounds almost the same. It is only a mistake to use it where exact data matters. A second slip: claiming RLE always makes a file smaller. It only helps when there are long runs of the same value; on data that changes constantly, the count-value pairs add up to more than the original.
反复出现薄弱的答案说有损压缩"随机删除数据"或"破坏文件"。它去掉的是被挑选出来的细节,即被判断为最不易察觉的部分,所以结果看起来或听起来仍然几乎一样。只有在需要精确数据的地方用它,才是错误。第二个错误:声称 RLE 总能让文件变小。只有当存在很长的同值行程时它才有用;对不断变化的数据,这些"计数-值"对加起来比原始数据还多。
Misstates lossy: "Lossy compression randomly deletes bits, so the file is damaged and lower quality."
误解有损: "有损压缩随机删除位,所以文件被损坏、质量更低。"
Right: "Lossy compression permanently removes detail that a person is unlikely to notice, so the file is much smaller and still looks or sounds close to the original. It cannot be restored exactly, so it suits images and audio but not data like text or spreadsheets."
正确: "有损压缩永久去掉人不太可能察觉的细节,所以文件小得多,而且看起来或听起来仍然接近原始。它无法被完全还原,所以适合图像和音频,而不适合文本或电子表格这类数据。"
Revision questions
复习题
Specimen-paper style. Write an answer, then reveal the marking points and mark yourself.
采用样卷风格。写下答案,然后展开评分要点给自己打分。
[2]State the two main benefits of compressing a file.
[2]说出压缩一个文件的两个主要好处。
[3]Set out how lossless and lossy compression differ, and name one kind of file that suits each.
[3]解释无损压缩与有损压缩的区别,并各举一种适合的文件类型。
Marking points
评分要点
- Lossless: the original can be restored exactly, bit for bit; it removes only redundancy. Suits text, source code or a database.
- Lossy: some information is permanently thrown away, so the original cannot be restored exactly, but the file is much smaller. Suits a photograph or an audio track.
- 无损:可以一位不差地完全还原原始数据;它只去掉冗余。适合文本、源代码或数据库。
- 有损:一部分信息被永久丢弃,所以无法精确还原,但文件小得多。适合一张照片或一段音频。
[4]Describe how run-length encoding works, and explain one type of data for which it is not effective.
[4]描述行程长度编码如何工作,并解释一种它效果不好的数据类型。
Marking points
评分要点
- Run-length encoding replaces each run of identical adjacent values with a pair: the number of repeats, then the value.
- So a long run of the same value is stored as two numbers instead of many.
- It is not effective for data with no long runs (for example ordinary text or a detailed photograph), where the encoded version can be as large as the original or larger.
- 行程长度编码把每一串相同的相邻值替换成一对:重复的次数,然后是值。
- 所以同一值的一段长重复被存成两个数,而不是很多个。
- 对于没有长重复的数据(例如普通文本或一张细节丰富的照片)它不奏效,那时编码后的版本可能和原始一样大甚至更大。
[5]Describe the stages of transform coding as used in JPEG, then state which stage makes the method lossy and why.
[5]描述 JPEG 压缩中变换编码的各个阶段,并解释它为什么是一种有损方法。
Marking points
评分要点
- Split: the image is cut into small square blocks.
- Transform: each block is converted from pixel values into frequency values (low frequencies for broad colour areas, high frequencies for fine detail).
- Quantise: the precision of the high-frequency values is reduced.
- Encode: the reduced values are packed further with lossless steps.
- The quantise stage makes it lossy, because it permanently discards precision from the high-frequency detail, which cannot be recovered.
- 分块:图像被切成一个个小的正方形块。
- 变换:每个块从像素值转换成频率值(低频对应大片颜色区域,高频对应精细细节)。
- 量化:降低高频值的精度。
- 编码:降低后的值用无损步骤进一步压缩。
- 量化阶段使它有损,因为它永久丢弃了高频细节的精度,而这无法被恢复。
[3]A 12-character string "WWWWWWBBBWWW" is compressed with run-length encoding. State the encoded output and the size saving, assuming one byte per character.
[3]用行程长度编码压缩一个 12 字符的字符串 "WWWWWWBBBWWW"。假设每个字符占一个字节,写出编码后的输出以及节省的大小。
Marking points
评分要点
- The runs are 6 W, 3 B, 3 W, so the encoded output is (6, W)(3, B)(3, W), often written 6W3B3W.
- That is 6 values instead of 12 characters, so with one byte each it is 6 bytes instead of 12: a saving of 6 bytes (50 percent).
- 各段是 6 个 W、3 个 B、3 个 W,所以编码输出是 (6, W)(3, B)(3, W),常写成 6W3B3W。
- 那是 6 个值而不是 12 个字符,所以每个 1 字节时是 6 字节而不是 12 字节:节省 6 字节(50%)。
Flashcards
记忆卡片
Active recall for this statement’s key terms.
针对本条考纲陈述关键术语的主动回忆练习。
Keep this statement’s practice with you
把这条考纲陈述的练习保存下来
Free account: unlock every revision question and flashcard for A1.1.8, and every other IB CS 2027 statement.
免费账户:解锁 A1.1.8 以及所有其他 IB CS 2027 考纲陈述的全部复习题和记忆卡片。