Lesson 11 of 15
Coding Efficiency and Redundancy
Coding Efficiency and Redundancy
Having computed the entropy and the average code length , we can evaluate how well a code performs.
Coding Efficiency
Coding efficiency is the fraction of the average code length that is information (not waste):
- — perfect: the code achieves the entropy bound
- — the code is longer than necessary
Redundancy
Redundancy is the extra bits per symbol beyond the entropy:
Shannon's source coding theorem guarantees bit for Huffman codes.
Compression Ratio
Compression ratio compares original and compressed sizes:
- CR — compression achieved
- CR — no gain
- CR — expansion (the data was incompressible)
Example
With bits, bits:
With bits, bits:
def coding_efficiency(H, avg_length):
return H / avg_length
def redundancy(H, avg_length):
return avg_length - H
def compression_ratio(original_bits, compressed_bits):
return original_bits / compressed_bits
print(round(coding_efficiency(1.75, 2.0), 4)) # 0.875
print(redundancy(1.75, 2.0)) # 0.25
print(compression_ratio(1000, 800)) # 1.25
Your Task
Implement:
coding_efficiency(H, avg_length)—redundancy(H, avg_length)—compression_ratio(original_bits, compressed_bits)— ratio of sizes
Python runtime loading...
Loading...
Click "Run" to execute your code.