trainhorn:   Train Horn Calculators
exn:  fail:  trainhorn
finite-real/  c
length-unit/  c
awg/  c
1 Sound level versus distance
spl-at-distance
distance-for-spl
distance-ratio-loss
db-doubling-loss
combine-levels
air-absorption-db/  km
2 Air tank runtime and compressor recovery
atmospheric-psi
cubic-feet-per-gallon
free-air-volume-cuft
tank-runtime-seconds
blast-count
compressor-recovery-seconds
3 Wire gauge by voltage drop
awg-sizes
copper-resistivity-ohm-m
copper-temp-coeff-per-c
awg-diameter-mm
awg-area-mm2
awg-resistance-ohm/  1000ft
voltage-drop
wire-suggestion
suggest-awg
9.3

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.

Raised for out-of-range input. The message names the function and argument.

Accepts real numbers that are not NaN or infinite.
Either 'ft or 'm.
An exact integer (1 to 40, or 0 for 1/0) or a label such as "10", "10 AWG", "1/0" or "0000".

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
Level at distance from a level source-db known at ref-distance. Both distances must be positive and use unit.

Examples:
> (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
Inverse of spl-at-distance: the distance (in unit) at which the level falls to target-db. Closed form without absorption, bisection with it.

Examples:
> (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
Level change 20 log10(ratio) when the distance is multiplied by ratio (> 0).

procedure

(db-doubling-loss) → flonum?

Loss per doubling of distance, about 6.02 dB.

Example:
> (db-doubling-loss)

6.020599913279623

procedure

(combine-levels levels) → flonum?

  levels : (listof finite-real/c)
Combined level of incoherent sources: 10 log10(sum 10^(L_i/10)). Two equal horns add about 3 dB. The list must not be empty.

Example:
> (combine-levels '(100 100))

103.01029995663981

Rounded atmospheric absorption in dB/km at 10 °C and 70 % relative humidity (an ISO 9613-2 table condition), keyed by octave-band centre frequency in Hz (63 to 8000). A rough guide only.

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.

Sea-level atmosphere, 14.696 psi.
231/1728 cubic feet per US gallon.

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
Free air released between two gauge pressures. start-psi must be greater than end-psi (>= 0).

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
Seconds of continuous horn time from start-psi down to cutoff-psi.

Example:
> (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
Whole blasts of blast-seconds that fit in runtime-seconds.

Example:
> (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
Seconds to refill from cut-in-psi to cut-out-psi. By default the compressor delivers compressor-cfm over the whole band; use the CFM rated at your cut-out pressure. If both q0 and rated are given, output falls linearly from q0 at 0 psi to compressor-cfm at rated psi, and the time is the closed-form integral V/P_atm × ln(q_out/q_in) / slope.

Examples:
> (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.

Gauges tried by suggest-awg, thinnest first: 18 to 4/0.
1.7241e-8.
0.00393 per °C.

procedure

(awg-diameter-mm awg) → flonum?

  awg : awg/c
Bare conductor diameter in mm.

procedure

(awg-area-mm2 awg) → flonum?

  awg : awg/c
Conductor cross-section in mm².

procedure

(awg-resistance-ohm/1000ft awg    
  [temperature-c]) → flonum?
  awg : awg/c
  temperature-c : finite-real/c = 20
Copper resistance in ohms per 1000 ft; temperature-c must be between -60 and 200.

Example:
> (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
Volts lost over the out-and-back run: I × R × 2 × length.

Example:
> (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
Result of suggest-awg.

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
Smallest gauge in awg-sizes whose drop stays within max-drop-pct (0 < x <= 50) of system-voltage. Raises exn:fail:trainhorn if even 4/0 is not enough.

Examples:
> (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"