> rice | adaptive | compress <
// Rice Coding - Adaptive integer compression with tunable parameter
>> features
Tunable Parameter
Adjust k to optimize for different data distributions.
Geometric Data
Optimal for data with geometric or exponential distribution.
Fast Coding
Simple division and remainder operations for speed.
>> technical info
How Rice Coding Works
Rice coding divides each integer n by 2^k to get quotient q and remainder r. The quotient is encoded in unary (q ones followed by a zero), and the remainder is encoded in k binary bits. This creates a variable-length code that adapts to the data distribution through the k parameter.
Rice Coding Example (k=2)
k=2, M=2^2=4 0 → q=0, r=0 → 0|00 → 000 1 → q=0, r=1 → 0|01 → 001 2 → q=0, r=2 → 0|10 → 010 3 → q=0, r=3 → 0|11 → 011 4 → q=1, r=0 → 10|00 → 1000 5 → q=1, r=1 → 10|01 → 1001 6 → q=1, r=2 → 10|10 → 1010 7 → q=1, r=3 → 10|11 → 1011 8 → q=2, r=0 → 110|00 → 11000 Larger k: fewer unary bits, more binary bits Smaller k: more unary bits, fewer binary bits
Why Use Rice Coding
- Adaptive to data distribution
- Simple to implement
- Fast encode/decode
- Good for sensor data
- Efficient for small integers
>> frequently asked questions
What is Rice coding?
Rice coding is a variable-length entropy encoding method that's particularly efficient for geometric distributions. It's a special case of Golomb coding where the divisor M is restricted to powers of 2 (M = 2^k), making it faster to compute using bit shifts.
How to choose the k parameter?
The optimal k depends on your data distribution. For data with mean μ, the optimal k ≈ log₂(μ × ln(2)). Small k (0-2) works well for very small numbers, while larger k (4-8) is better for data with larger values. Use the analyze function to find the optimal k for your data.
Rice vs Golomb coding?
Rice coding is a subset of Golomb coding where M = 2^k. This restriction makes Rice coding faster (using bit shifts instead of division) but potentially less optimal. Golomb can choose any M value for better compression, while Rice trades some compression efficiency for speed.
Where is Rice coding used?
Rice coding is widely used in lossless audio compression (FLAC, ALAC), image compression (JPEG-LS), and data from sensors with geometric distributions. It's particularly effective when data values are small non-negative integers with exponentially decreasing probability.