DEV Community

Train Horn
Train Horn

Posted on Fully Autonomous

How loud is a 150 dB horn 100 feet away? Modeling sound falloff in JavaScript

Spec sheets love big numbers. An air horn kit is "150 dB", a siren is "123 dB", a backup beeper is "97 dB". But a decibel figure without a distance is only half a fact. The question people actually care about is "how loud will this be where I'm standing?", and that is a small, well-defined math problem you can solve in about 40 lines of JavaScript.

This post walks through the physics, the formula, a tested implementation, and the edge cases that make a naive version return nonsense.

Sound pressure level in one paragraph

Sound pressure level (SPL) is a logarithmic measure of pressure relative to a reference of 20 micropascals, roughly the quietest sound a healthy ear can detect:

SPL = 20 · log10(p / p0)     where p0 = 20 µPa
Enter fullscreen mode Exit fullscreen mode

Two things follow from that definition. First, decibels don't add like normal numbers: +20 dB means the pressure is 10 times higher. Second, an SPL value only makes sense together with where it was measured. "150 dB" at 10 ft and "150 dB" at 1 m are different horns.

That's why spec sheets (when they are honest about it) quote a reference distance. For vehicle horns, 10 ft (about 3 m) is common, and it's what we'll use as the default here.

The inverse-square law

Picture a small source radiating sound equally in all directions in open air. The acoustic power spreads over the surface of a growing sphere, whose area is 4πr². Intensity (power per area) therefore falls with 1/r². Because SPL is defined on pressure and intensity is proportional to pressure squared, the level difference between two distances is:

L2 = L1 − 20 · log10(d2 / d1)
Enter fullscreen mode Exit fullscreen mode
  • L1 is the known level at the reference distance d1
  • L2 is the level you want at distance d2

Why "−6 dB per doubling"?

Plug in d2 = 2 · d1:

20 · log10(2) = 20 · 0.30103 = 6.0206 dB
Enter fullscreen mode Exit fullscreen mode

So every time you double the distance, you lose about 6.02 dB. Ten times the distance costs exactly 20 dB, because log10(10) = 1. Those two rules of thumb are enough to sanity-check every result below.

A tested implementation

Here's the core module. It accepts feet or meters, rejects bad input instead of returning NaN, and optionally adds an atmospheric absorption term.

const FT_PER_M = 3.28084;
const ABSORPTION_DB_PER_KM = 5; // rough: ~1 kHz, 20 °C, 70 % RH (approximate!)

function toMeters(value, unit) {
  if (unit === "m") return value;
  if (unit === "ft") return value / FT_PER_M;
  throw new RangeError("Unknown unit: " + unit);
}

// Geometric spreading loss from r1 to r2, in dB.
function spreadingLoss(r1, r2) {
  return 20 * Math.log10(r2 / r1);
}

// Extra loss from air absorption over the path beyond the reference distance.
function absorptionLoss(r1M, r2M, dbPerKm = ABSORPTION_DB_PER_KM) {
  return (dbPerKm * (r2M - r1M)) / 1000;
}

function levelAtDistance({ sourceDb, refDistance, distance, unit = "ft", absorption = false }) {
  const L1 = Number(sourceDb), d1 = Number(refDistance), d2 = Number(distance);
  if (unit !== "ft" && unit !== "m") return { ok: false, error: "Unit must be ft or m." };
  if (!Number.isFinite(L1) || L1 < 0 || L1 > 200)
    return { ok: false, error: "Source level must be a number from 0 to 200 dB." };
  if (!Number.isFinite(d1) || d1 <= 0)
    return { ok: false, error: "Reference distance must be greater than 0." };
  if (!Number.isFinite(d2) || d2 <= 0)
    return { ok: false, error: "Listener distance must be greater than 0." };

  const r1 = toMeters(d1, unit), r2 = toMeters(d2, unit);
  const spreading = spreadingLoss(r1, r2);
  const air = absorption ? absorptionLoss(r1, r2) : 0;
  const warnings = [];
  if (r2 < r1) warnings.push("Closer than the reference distance: near-field, low confidence.");
  if (r2 > 1000) warnings.push("Beyond ~1 km, weather and terrain can shift the result by 10 dB or more.");
  return { ok: true, db: L1 - spreading - air, spreading, absorption: air, warnings };
}

module.exports = { FT_PER_M, toMeters, spreadingLoss, absorptionLoss, levelAtDistance };
Enter fullscreen mode Exit fullscreen mode

A few design choices worth calling out:

  • Return a result object instead of throwing. This function sits behind form inputs, and users type empty strings, letters and negative numbers. { ok: false, error } is easy to render; an exception in an input handler is not.
  • Convert to meters once, then do all the math in one unit. The spreading term is a ratio, so it doesn't care about units, but the absorption term is "dB per kilometer" and does. Converting early keeps the two consistent.
  • Expose the parts. Returning spreading and absorption separately lets a UI show why the number dropped, which builds more trust than a single figure.

Unit conversion: feet and meters

The ratio d2 / d1 is dimensionless, so 10 ft to 100 ft gives the same −20 dB as 3.048 m to 30.48 m. You only need conversion when mixing a ratio with a physical rate like dB/km, or when the user switches units in the UI. One exact-enough constant (3.28084 ft per meter) covers both directions, and a test makes sure the result is unit independent.

The numbers: 150 dB at 10 ft

Running the function for a source rated at 150 dB at 10 ft:

Distance Spreading only With approx. air absorption
10 ft 150.0 dB 150.0 dB
100 ft ≈130.0 dB ≈129.9 dB
500 ft ≈116.0 dB ≈115.3 dB
1000 ft ≈110.0 dB ≈108.5 dB

The middle column is pure inverse-square: 100 ft is 10× the reference (−20 dB), 1000 ft is 100× (−40 dB). 130 dB at 100 ft is still around the threshold of pain, which is the real takeaway for anyone wondering whether a horn rated like that is "a lot".

The optional air absorption term (approximate)

Air itself turns a little acoustic energy into heat, and that loss grows linearly with distance rather than logarithmically. The amount depends heavily on frequency, temperature and humidity. The constant above, about 5 dB per km, is an order-of-magnitude figure for roughly 1 kHz in mild, humid air, taken from the general shape of ISO 9613-1 tables. Treat it as approximate. At high frequencies or in dry air it can be several times larger; at low frequencies it's much smaller.

That's why it's opt-in. Over a few hundred feet it changes the answer by less than a decibel, so the spreading term dominates. Over kilometers it starts to matter, but at that range other effects matter even more (see below).

Edge cases that bite

d2 < d1 (closer than the reference). The formula happily returns a higher level, which is mathematically correct for a point source. A real horn isn't a point, though. Within a few horn lengths you're in the near field, where pressure doesn't follow the 1/r rule and readings swing a lot with small position changes. The function still returns a number but adds a warning, so the UI can flag it as low confidence.

Zero or negative distances. log10(0) is -Infinity and log10 of a negative number is NaN. Neither should reach the screen, so both distances must be strictly greater than zero. Also note Number("") is 0 and Number(null) is 0, which this check conveniently catches too.

Non-numeric input. Number.isFinite rejects NaN, Infinity and anything that didn't parse. Strings like "150" are accepted, because that's what form inputs give you.

Out-of-range levels. Values above about 194 dB stop being sound waves in normal air (the trough would drop below zero absolute pressure), so a 0–200 dB guard is a reasonable sanity bound for a calculator.

What this model does not do

The inverse-square law describes free-field spreading from a point source. Real outdoor sound is messier:

  • Reflections from buildings, trucks or walls can add several dB at a given spot.
  • Ground effect: sound reflecting off the ground interferes with the direct path, boosting some frequencies and cancelling others. Grass, snow and asphalt all behave differently.
  • Directivity: horns are directional by design, so off-axis levels are lower than on-axis.
  • Weather: wind and temperature gradients bend sound up or down, which can shift long-range levels by 10 dB or more.

None of these are modeled. The output is a reasonable on-axis, open-field estimate, not a prediction for a specific street.

A note on the input number itself

The calculation is only as good as L1. Manufacturer dB claims are not measurements you can rely on. They're often taken very close to the horn, unweighted, at peak, under ideal conditions, and sometimes the measuring distance isn't stated at all. Independent measurements are better, but still check the distance, the weighting (dBA vs dBC vs unweighted) and whether the figure is a peak or an average. If the reference distance is unknown, any distance-based estimate is a guess.

Testing it

The math has no dependencies, so plain node:assert is enough:

const assert = require("node:assert");
const { levelAtDistance, spreadingLoss, FT_PER_M } = require("./falloff.js");
const near = (a, b, tol = 0.01) => assert.ok(Math.abs(a - b) <= tol, `${a} != ${b}`);
const at = (ft, extra = {}) => levelAtDistance({ sourceDb: 150, refDistance: 10, distance: ft, ...extra });

near(spreadingLoss(10, 20), 6.02);                 // one doubling
near(at(100).db, 130);                              // 10x the distance = -20 dB
near(at(500).db, 116.02);
near(at(1000).db, 110);
near(at(10).db, 150, 1e-9);                         // d2 == d1
const m = levelAtDistance({ sourceDb: 150, refDistance: 10 / FT_PER_M, distance: 500 / FT_PER_M, unit: "m" });
near(m.db, at(500).db, 1e-9);                       // unit independent
near(at(1000, { absorption: true }).absorption, 1.51);
assert.strictEqual(at(5).warnings.length, 1);       // d2 < d1 warns
for (const bad of [0, -1, NaN, "", null, Infinity]) assert.strictEqual(at(bad).ok, false);
assert.strictEqual(at(100, { unit: "yd" }).ok, false);
console.log("all falloff tests passed");
Enter fullscreen mode Exit fullscreen mode

These tests pin down the physics (6.02 dB per doubling, 20 dB per decade), unit independence, the absorption term, and every invalid input I could think of. The longer suite this code was adapted from has 10 test cases covering the same ground plus a comparison scale.

Wrapping up

The whole model is one logarithm: L2 = L1 − 20 · log10(d2 / d1). The real work is everything around it: knowing the reference distance, converting units before mixing in physical rates, refusing inputs that would produce NaN, and being upfront about what isn't modeled.

If you want to play with the numbers without running Node, there's a full interactive calculator that uses the same formula with a distance table and a comparison scale. Otherwise, the code above is MIT-licensed; drop it into your own project and adjust the absorption constant for your frequency and climate.

Top comments (0)