trainhorn: Train Horn Calculators
| (require trainhorn) | package: trainhorn |
Small, dependency-free calculators for vehicle and train horn setups: how loud a horn is at a given distance, how long an onboard air tank lasts, how long a compressor needs to refill it, and which copper wire gauge keeps the voltage drop in budget. It is a faithful port of the tested trainhorn-calc core, with the same formulas and test vectors. Train Horn also hosts interactive versions of these calculators.
All results are estimates from textbook formulas. They are not measurements, and nothing here is a legal noise assessment.
Every export has a contract. Arguments of the wrong type raise a contract error; values of the right type but out of range (a zero distance, a start pressure below the cut-off, an unknown wire gauge) raise exn:fail:trainhorn, a subtype of exn:fail:contract, so exn:fail:contract? catches both.
struct
value
value
value
1 Sound level versus distance
A horn is treated as a point source in a free field, so the level falls by 20 log10(d2/d1) dB, plus optional air absorption over the extra path:
L(d) = L_ref - 20 log10(d / d_ref) - alpha (d - d_ref) / 1000
with distances in metres for the absorption term. Ground reflection, barriers, wind and temperature gradients are ignored, and the model does not hold within a horn length or two of the source.
procedure
(spl-at-distance source-db distance [ #:ref-distance ref-distance #:unit unit #:absorption-db/km absorption]) → flonum? source-db : finite-real/c distance : finite-real/c ref-distance : finite-real/c = 10 unit : length-unit/c = 'ft absorption : finite-real/c = 0
> (spl-at-distance 150 100) 130.0
> (spl-at-distance 120 10 #:ref-distance 1 #:unit 'm) 100.0
> (spl-at-distance 150 1000 #:unit 'm #:absorption-db/km 1.9) 108.119
procedure
(distance-for-spl source-db target-db [ #:ref-distance ref-distance #:unit unit #:absorption-db/km absorption]) → flonum? source-db : finite-real/c target-db : finite-real/c ref-distance : finite-real/c = 10 unit : length-unit/c = 'ft absorption : finite-real/c = 0
> (distance-for-spl 150 130) 100.0
> (distance-for-spl 150 90 #:absorption-db/km 1.9) 6491.256148481227
procedure
(distance-ratio-loss ratio) → flonum?
ratio : finite-real/c
procedure
> (db-doubling-loss) 6.020599913279623
procedure
(combine-levels levels) → flonum?
levels : (listof finite-real/c)
> (combine-levels '(100 100)) 103.01029995663981
2 Air tank runtime and compressor recovery
By Boyle’s law, a tank of V gallons dropping from gauge pressure P_start to P_end releases V_free = V × 0.13368 × (P_start - P_end) / P_atm cubic feet of free air. Horn consumption and compressor output are rated in CFM of free air. The expansion is taken as isothermal, so treat the runtime as an upper estimate.
value
value
procedure
(free-air-volume-cuft tank-gallons start-psi end-psi [ #:atmospheric-psi patm]) → flonum? tank-gallons : finite-real/c start-psi : finite-real/c end-psi : finite-real/c patm : finite-real/c = atmospheric-psi
procedure
(tank-runtime-seconds tank-gallons start-psi cutoff-psi horn-cfm [ #:atmospheric-psi patm]) → flonum? tank-gallons : finite-real/c start-psi : finite-real/c cutoff-psi : finite-real/c horn-cfm : finite-real/c patm : finite-real/c = atmospheric-psi
> (tank-runtime-seconds 5 150 110 10) 10.91566866267465
procedure
(blast-count runtime-seconds blast-seconds)
→ exact-nonnegative-integer? runtime-seconds : finite-real/c blast-seconds : finite-real/c
> (blast-count (tank-runtime-seconds 5 150 110 10) 2) 5
procedure
(compressor-recovery-seconds tank-gallons cut-in-psi cut-out-psi compressor-cfm [ #:cfm-at-zero-psi q0 #:rated-psi rated #:atmospheric-psi patm]) → flonum? tank-gallons : finite-real/c cut-in-psi : finite-real/c cut-out-psi : finite-real/c compressor-cfm : finite-real/c q0 : (or/c #f finite-real/c) = #f rated : (or/c #f finite-real/c) = #f patm : finite-real/c = atmospheric-psi
> (compressor-recovery-seconds 2 110 150 1.0) 43.662674650698605
> (compressor-recovery-seconds 2 110 150 1.0 #:cfm-at-zero-psi 2.0 #:rated-psi 150) 38.70512365385508
3 Wire gauge by voltage drop
Resistances are computed from the AWG definition (ASTM B258 diameter, annealed copper at 100 % IACS, rho = 1.7241e-8 ohm·m) and agree with published solid-copper tables to about 1 %. The round trip is twice the one-way run. This sizes for voltage drop only: it does not check ampacity or fusing, so always fuse to protect the wire you choose.
value
value
procedure
(awg-diameter-mm awg) → flonum?
awg : awg/c
procedure
(awg-area-mm2 awg) → flonum?
awg : awg/c
procedure
(awg-resistance-ohm/1000ft awg [ temperature-c]) → flonum? awg : awg/c temperature-c : finite-real/c = 20
> (awg-resistance-ohm/1000ft "10") 0.9988409095411369
procedure
(voltage-drop amps one-way-length awg [ #:unit unit #:temperature-c temperature-c]) → flonum? amps : finite-real/c one-way-length : finite-real/c awg : awg/c unit : length-unit/c = 'ft temperature-c : finite-real/c = 20
> (voltage-drop 20 10 10) 0.39953636381645474
struct
(struct wire-suggestion ( awg voltage-drop-v voltage-drop-pct allowed-drop-pct system-voltage amps one-way-length unit) #:transparent) awg : string? voltage-drop-v : flonum? voltage-drop-pct : flonum? allowed-drop-pct : flonum? system-voltage : flonum? amps : flonum? one-way-length : flonum? unit : length-unit/c
procedure
(suggest-awg amps one-way-length [ #:system-voltage system-voltage #:max-drop-pct max-drop-pct #:unit unit #:temperature-c temperature-c]) → wire-suggestion? amps : finite-real/c one-way-length : finite-real/c system-voltage : finite-real/c = 12 max-drop-pct : finite-real/c = 3 unit : length-unit/c = 'ft temperature-c : finite-real/c = 20
> (suggest-awg 30 10)
(wire-suggestion
"6"
0.23703872341214613
1.9753226951012177
3.0
12.0
30.0
10.0
'ft)
> (wire-suggestion-awg (suggest-awg 30 10 #:system-voltage 24)) "10"