12. Delta & Predictive Coding

Store differences instead of values

In many real-world signals, consecutive values are similar. Temperature readings drift by a degree or two. Audio samples at 44,100 Hz change gradually. Neighboring pixels in a photo are nearly identical. Each value is close to the one before it. Instead of storing absolute values, store the difference (delta) from a prediction. The deltas are small and compress much better.

Delta Coding: The Simplest Predictor

The simplest prediction you can make: the next value equals the current value. Store the difference. If the prediction is good, the difference is small. If the prediction is perfect, the difference is zero.

Example: hourly temperatures

Original values:

Values: [20, 21, 21, 22, 21, 20, 19, 20]

Prediction: next = current. Delta = actual − predicted:

Index:      0    1    2    3    4    5    6    7
Value:     20   21   21   22   21   20   19   20
Predicted:  —   20   21   21   22   21   20   19
Delta:     20    1    0    1   -1   -1   -1    1

The first value is stored as-is (no previous value to predict from). Every subsequent delta is tiny.

Original

Range: 19 to 22

8 values, each needs bits to represent 19–22

Delta-coded

First value: 20

Remaining range: −1 to 1

7 tiny values that cluster around zero

Why Small Values Compress Better

Smaller values need fewer bits

A Huffman or arithmetic coder assigns short codes to common values and long codes to rare ones. Since deltas cluster around zero, the distribution is highly peaked — most values are 0, ±1, or ±2. A peaked distribution means low entropy, and low entropy means excellent compression.

Entropy comparison

Original values: [20, 21, 21, 22, 21, 20, 19, 20]
  Distinct values: {19, 20, 21, 22} — 4 symbols
  Distribution:    19:1/8  20:3/8  21:3/8  22:1/8
  Entropy:         1.81 bits/symbol

Delta values: [20, 1, 0, 1, -1, -1, -1, 1] Ignoring the first value, the deltas are: Distinct values: {-1, 0, 1} — 3 symbols Distribution: -1:3/7 0:1/7 1:3/7 Entropy: 1.45 bits/symbol

The deltas have lower entropy. In real-world data with thousands of samples, the difference is far more dramatic — delta entropy is often below 1 bit/symbol while the original might need 8 or 16 bits.

Linear Prediction

Better predictions produce smaller residuals, and smaller residuals compress better. Instead of "next = current," use linear extrapolation: next = 2 × current − previous. This predicts that the trend continues. Even better: use a weighted average of several previous samples.

Simple delta vs linear prediction

Values:  [10, 12, 14, 16, 15, 13]

Simple delta (next = current): Index: 0 1 2 3 4 5 Value: 10 12 14 16 15 13 Predicted: — 10 12 14 16 15 Residual: 10 2 2 2 -1 -2

Linear prediction (next = 2×current − previous): Index: 0 1 2 3 4 5 Value: 10 12 14 16 15 13 Predicted: — — 14 16 18 14 Residual: 10 12 0 0 -3 -1

For the smooth upward trend (indices 2–3), linear prediction nails it exactly — zero residuals. It overshoots at the reversal point (index 4), but overall the residuals are smaller. For signals with smooth trends, linear prediction wins.

Higher-order predictors use more previous samples. A 4th-order predictor uses the last four values to predict the next one, fitting a cubic curve through the recent history. The tradeoff: better predictions for smooth signals, but more coefficients to store and worse performance on noisy or rapidly changing data.

PNG: Delta Coding for Images

PNG is the clearest real-world example of delta coding. Before compressing pixel data with DEFLATE, PNG applies a prediction filter to each row. There are five filter types:

  1. None

    No filtering. Store the raw pixel values.

  2. Sub

    Delta from the pixel to the left: Sub(x) = Raw(x) − Raw(x−1).

  3. Up

    Delta from the pixel above: Up(x) = Raw(x) − Raw(x, row−1).

  4. Average

    Delta from the average of left and above: Avg(x) = Raw(x) − floor((Left + Above) / 2).

  5. Paeth

    Uses left, above, and upper-left. Picks whichever of the three neighbors is closest to p = Left + Above − UpperLeft. Often the best filter for photographic content.

Each row independently chooses whichever filter produces the smallest values. The encoder tries all five and picks the winner.

Example: 4×3 pixel grid (grayscale)

Original pixels:
  Row 0:  100  102  104  106
  Row 1:  101  103  105  107
  Row 2:  102  104  106  108

Sub filter (delta from left): Row 0: 100 2 2 2 Row 1: 101 2 2 2 Row 2: 102 2 2 2

Up filter (delta from above): Row 0: 100 102 104 106 (no row above — stored raw) Row 1: 1 1 1 1 Row 2: 1 1 1 1

The Sub filter turns a smooth horizontal gradient into repeating 2s. The Up filter turns a smooth vertical gradient into 1s. For this image, Up is the better choice for rows 1 and 2 — the deltas are smaller.

PNG is lossless because the deltas are exact

The prediction and subtraction use integer arithmetic with no rounding or truncation. Given the delta and the prediction, you reconstruct the original value exactly: Raw(x) = Delta(x) + Predicted(x). Not a single bit is lost.

FLAC: Predictive Coding for Audio

CD audio is 44,100 samples per second, 16 bits per sample, two channels. That's 1,411 kbps — about 10 MB per minute. Adjacent samples are extremely correlated because sound waves are smooth, continuous signals.

FLAC (Free Lossless Audio Codec) exploits this correlation:

  1. Divide the audio into frames (typically 4096 samples)

    Each frame is compressed independently, enabling seeking.

  2. Fit a linear predictor to each frame (up to order 12)

    The encoder tries several predictor orders and picks the best one.

  3. Compute residuals (actual − predicted)

    The residuals are small integers clustered around zero.

  4. Encode residuals with Rice coding

    Rice codes are optimal for geometric distributions — exactly what residuals look like.

Example: audio samples (16-bit, range −32768 to 32767)

Samples:     [3012, 3025, 3040, 3058, 3070, 3079, 3085, 3088]

Simple delta: Residuals: [3012, 13, 15, 18, 12, 9, 6, 3]

2nd-order linear predictor (after first two samples): Predicted: [ —, —, 3038, 3055, 3076, 3082, 3088, 3091] Residuals: [3012, 3025, 2, 3, -6, -3, -3, -3]

The 2nd-order predictor captures the curve of the waveform, producing residuals that are even smaller. Typical result: FLAC compresses CD audio to 50–60% of original size, perfectly lossless.

DPCM and ADPCM

DPCM (Differential Pulse Code Modulation) is the formal name for delta coding applied to sampled signals. It quantizes the prediction error to a fixed number of bits, making it a lossy technique — you lose the least-significant part of each delta.

ADPCM (Adaptive DPCM) goes further: it adapts the quantization step size on the fly. When the signal changes rapidly, use larger steps. When it's steady, use smaller steps. This gives better quality for the same bit rate.

ADPCM in action

Signal:        [100, 102, 108, 120, 122, 123, 123]
Delta:         [100,   2,   6,  12,   2,   1,   0]
Step size:     [  4,   4,   4,   8,   8,   4,   4]
Quantized:     [ 25,   0,   2,   2,   0,   0,   0]
Reconstructed: [100, 100, 108, 124, 124, 124, 124]

Notice the step size doubles when a large delta is encountered (index 3) and shrinks when deltas are small. This is lossy — the reconstructed values don't exactly match the originals — but the bit rate is very low. ADPCM was the standard for telephone voice compression (ITU-T G.726) and is still used in WAV files and game audio.

Motion Compensation in Video

The ultimate predictive coding: predict the current video frame from previous frames. A video at 30 fps has frames 33 ms apart — most of the image barely changes. A camera pan shifts everything by a few pixels. An object moves across the scene.

The encoder divides each frame into blocks (typically 16×16 or smaller) and searches the reference frame for the best match. The result is a motion vector: "this block moved (dx, dy) from where it was in the reference frame." Only the residual (the difference between the predicted block and the actual block) is encoded.

Most of the residual is near zero — the motion prediction is usually very good. The residual is then transformed (DCT), quantized, and entropy coded. This is the heart of H.264, H.265, VP9, and AV1.

Why video achieves 100:1+ compression ratios

Temporal prediction removes most of the data. A 1920×1080 frame at 24 bits/pixel is 6.2 MB. At 30 fps, that's 186 MB/s uncompressed. A typical H.264 stream at 5 Mbps achieves a 300:1 ratio. Spatial prediction (within a frame) helps, but temporal prediction (between frames) is the main reason video compression works so spectacularly well.

Delta of Delta

For signals with linear trends, delta coding produces constant deltas. Apply delta coding a second time and you get zeros. This is remarkably effective for timestamps, counters, and other monotonically increasing sequences.

Example: timestamps at regular intervals

Timestamps:     [1000, 1060, 1120, 1180, 1240, 1300, 1360]
Delta:          [1000,   60,   60,   60,   60,   60,   60]
Delta of delta: [1000,   60,    0,    0,    0,    0,    0]

The delta-of-delta representation is almost entirely zeros. A run-length encoder or a simple variable-length integer scheme compresses this to nearly nothing.

This technique is used in Facebook's Gorilla time-series database and in Prometheus. When timestamps arrive at regular intervals (which they usually do), delta-of-delta encoding reduces them to a stream of zeros with occasional small corrections.

Where Delta & Predictive Coding Are Used

System Domain Technique
PNG Images Sub/Up/Average/Paeth filters before DEFLATE
FLAC Audio Linear prediction (up to order 12) + Rice coding
ALAC Audio Similar to FLAC — adaptive linear prediction
H.264 / H.265 / AV1 Video Motion compensation + spatial prediction
Git Version control Binary diffs (delta compression in packfiles)
HTTP Web Delta encoding (RFC 3229) for cache updates
Gorilla / Prometheus Time-series DB Delta-of-delta for timestamps, XOR for values

Key Takeaways

  • Delta coding replaces absolute values with differences from a prediction — the simplest prediction is "next = current"
  • Deltas cluster around zero, creating a peaked distribution with low entropy that compresses extremely well
  • Better predictions (linear, higher-order) produce smaller residuals and better compression
  • PNG applies per-row prediction filters (Sub, Up, Average, Paeth) before DEFLATE — entirely lossless
  • FLAC uses linear predictors up to order 12, achieving 50–60% compression on CD audio with no loss
  • Video codecs achieve 100:1+ ratios primarily through temporal prediction (motion compensation)
  • Delta-of-delta turns linear trends into zeros — the basis of modern time-series compression

Try It Yourself — Delta Coding Explorer

Enter a comma-separated list of numbers to see how delta coding transforms them. Watch how the deltas cluster around zero, reducing entropy.

Original Values

Delta Values

Visual Comparison

Statistics


Delta and predictive coding are not standalone compressors — they are transforms that reshape data into a form that entropy coders handle efficiently. The pattern is universal: predict, subtract, compress the residual. Whether it's a PNG filter, a FLAC predictor, or an H.265 motion estimator, the underlying idea is the same. The better the prediction, the smaller the residual, the fewer the bits.

The compression techniques on this site — from RLE to Huffman to delta coding — are building blocks. Real-world compressors combine them in layers. Understanding each layer lets you see why formats work the way they do and choose the right tool for your data.