Quantizing the Wind into Eight Buckets
The arrow I’m shaping from basswood can point at 137.4 degrees. It can point at 137.41 degrees. It can point at any of the infinitely many directions between north and south, east and west. But when I tell you where the wind’s coming from, I say “southeast.” The continuous becomes discrete. This translation — from real-valued measurement to categorical bucket — is quantization, and it underpins every analog-to-digital conversion ever built.
The algorithm is simple. Divide 360 degrees into eight equal sectors of 45 degrees each, centered on their compass points. North isn’t 0° to 45°; it’s 337.5° to 22.5°, wrapping around. The cleanest way to handle that wraparound: add half a sector width (22.5°) before dividing.
(define (angle->compass degrees)
(let* ((dirs '#("N" "NE" "E" "SE" "S" "SW" "W" "NW"))
(shifted (modulo (+ degrees 22.5) 360))
(index (quotient (exact (floor shifted)) 45)))
(vector-ref dirs index)))
; (angle->compass 47) => "NE"
; (angle->compass 359) => "N"
; (angle->compass 180) => "S"
public class Compass {
static final String[] DIRS = {"N","NE","E","SE","S","SW","W","NW"};
public static String fromAngle(double deg) {
int i = (int) Math.floor(((deg + 22.5) % 360) / 45);
return DIRS[i];
}
}
// Compass.fromAngle(89.9) => "E"
// Compass.fromAngle(90.1) => "E"
// Compass.fromAngle(112.5) => "SE"
The Scheme version leans on integer quotient for the division — Scheme’s numerical tower distinguishes exact from inexact, so floor alone gives a flonum that needs coercion. Java just truncates with the cast. Same math, different ceremony.
Quantization error is the gap between what the vane actually points at and what the bucket claims. With eight directions, worst-case error is ±22.5°. Want better resolution? Use sixteen buckets (NNE, ENE, …) and cut the error in half. But there’s a tradeoff: more categories means more bits to store, more states to handle, more complexity downstream. Weather services settled on sixteen-point roses; sailors historically used thirty-two. For a whittled vane on a shed roof, eight feels right.
The 1960s saw this operation become industrially critical as analog sensors fed digital computers. Telephone systems, seismographs, early digital audio — all faced the same question: how many buckets are enough? Claude Shannon had already answered mathematically (sample at twice the highest frequency, quantize with enough bits for your noise floor), but implementation was engineering, not theory.
My vane will spin freely to any angle. The act of reading it — glancing up, saying “wind’s from the west” — is the quantization. The computation happens in my head, eight mental buckets waiting for continuous input.