horn_acoustics 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/README.md +83 -0
- data/lib/horn_acoustics/atmospheric_absorption.rb +139 -0
- data/lib/horn_acoustics/chord_frequencies.rb +123 -0
- data/lib/horn_acoustics/doppler_shift.rb +109 -0
- data/lib/horn_acoustics/horn_window.rb +166 -0
- data/lib/horn_acoustics/speed_of_sound.rb +51 -0
- data/lib/horn_acoustics/spl_at_distance.rb +115 -0
- data/lib/horn_acoustics/util.rb +81 -0
- data/lib/horn_acoustics/version.rb +6 -0
- data/lib/horn_acoustics.rb +64 -0
- metadata +62 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: d6a4cc1ceec8ae784151b7118528d08b7cf1adf0c332cb440923d618499617ed
|
|
4
|
+
data.tar.gz: 60afe532fecdd5b21228414d9eee5c5910073cf20bdacbd41d97d0816c03f01b
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 7131af54a4dbb02888d88def7f0b43b3216d569998ecf81a207ef232686c434df332fb1ef8a5b51f8548f2f08f08f0f17819aeb481983b65cb3fe661245a1e2f
|
|
7
|
+
data.tar.gz: 17e73e1192e7d666b4eda830841104bc2e3ccddad2dd9e7ad94e15cfceae092b3c5aed232546994080bd2ee3c00d7fe7ec918685ae99b9b3998352193e0dd781
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Horn Guns
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# horn_acoustics
|
|
2
|
+
|
|
3
|
+
Small helpers for outdoor acoustics calculations that come up when
|
|
4
|
+
working with train horns — locomotive horns at a grade crossing, and
|
|
5
|
+
also portable and battery-powered train horns used for signalling away
|
|
6
|
+
from the rail right-of-way. The library covers how the sound-pressure
|
|
7
|
+
level of a point source changes with distance, how much air absorbs a
|
|
8
|
+
tone, the Doppler shift of a moving source or observer, the sounding
|
|
9
|
+
window required at a U.S. public highway-rail grade crossing, and the
|
|
10
|
+
equal-tempered frequencies of a multi-chime horn.
|
|
11
|
+
|
|
12
|
+
Every formula is taken from a published standard or regulation:
|
|
13
|
+
|
|
14
|
+
- inverse-square (spherical) spreading: ISO 9613-2:1996, clause 7.1
|
|
15
|
+
- atmospheric absorption: ISO 9613-1:1993, equations (3)--(5) and Annex B
|
|
16
|
+
- speed of sound versus temperature: ISO 2533:1975
|
|
17
|
+
- classical Doppler formula for a source and observer in a still medium
|
|
18
|
+
- locomotive-horn sounding window: 49 CFR 222.21 (b)(2)--(b)(3) only
|
|
19
|
+
- A4 = 440 Hz, twelve-tone equal temperament: ISO 16:1975
|
|
20
|
+
|
|
21
|
+
The gem does not invent spectra, empirical horn data, or fitted
|
|
22
|
+
constants. Background on this class of device is collected at
|
|
23
|
+
[Horn Guns](https://hornguns.com/).
|
|
24
|
+
|
|
25
|
+
## Installation
|
|
26
|
+
|
|
27
|
+
Once the gem is on RubyGems it can be installed with:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
gem install horn_acoustics
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
From a local `.gem` file:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
gem install horn_acoustics-0.1.0.gem
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
In a Gemfile:
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
gem "horn_acoustics", "0.1.0"
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
## Usage
|
|
46
|
+
|
|
47
|
+
```ruby
|
|
48
|
+
require "horn_acoustics"
|
|
49
|
+
|
|
50
|
+
# Level of a 1 kHz tone 100 m from a source that is 110 dB at 1 m,
|
|
51
|
+
# including ISO 9613-1 atmospheric absorption at 20 °C and 50 % RH
|
|
52
|
+
HornAcoustics.spl_at_distance(
|
|
53
|
+
110, 100, distance_ref: 1,
|
|
54
|
+
absorption: true, frequency: 1000
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
# Frequencies of a three-chime train-horn major triad, then the
|
|
58
|
+
# Doppler-shifted tones heard by a wayside observer as the train
|
|
59
|
+
# approaches at 30 m/s
|
|
60
|
+
notes = HornAcoustics.chord_frequencies(["D4", "F#4", "A4"])
|
|
61
|
+
HornAcoustics.doppler_shift(
|
|
62
|
+
notes.values,
|
|
63
|
+
source_speed: 30,
|
|
64
|
+
source_towards_observer: true
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
# 49 CFR 222.21 sounding window versus train speed
|
|
68
|
+
HornAcoustics.horn_window([45, 60, 80], distance_unit: "ft")
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
The same calculations apply whether the source is a locomotive horn or a
|
|
72
|
+
portable, battery-powered train horn: spherical spreading and air
|
|
73
|
+
absorption depend on level, distance, frequency and weather, not on how
|
|
74
|
+
the horn is powered.
|
|
75
|
+
|
|
76
|
+
## References
|
|
77
|
+
|
|
78
|
+
- ISO 9613-1:1993. *Acoustics — Attenuation of sound during propagation outdoors — Part 1: Calculation of the absorption of sound by the atmosphere.*
|
|
79
|
+
- ISO 9613-2:1996. *Acoustics — Attenuation of sound during propagation outdoors — Part 2: General method of calculation.*
|
|
80
|
+
- ISO 2533:1975. *Standard Atmosphere.*
|
|
81
|
+
- ISO 16:1975. *Acoustics — Standard tuning frequency (Standard musical pitch).*
|
|
82
|
+
- 49 CFR 222.21, *When must a locomotive horn be used?*
|
|
83
|
+
<https://www.ecfr.gov/current/title-49/section-222.21>
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HornAcoustics
|
|
4
|
+
# Atmospheric absorption coefficient (ISO 9613-1:1993).
|
|
5
|
+
module AtmosphericAbsorption
|
|
6
|
+
# ISO 9613-1:1993 clause 4.2 reference temperature, in kelvin.
|
|
7
|
+
T0 = 293.15
|
|
8
|
+
# Triple-point temperature of water, in kelvin (ISO 9613-1 Annex B).
|
|
9
|
+
T01 = 273.16
|
|
10
|
+
# ISO 9613-1:1993 reference pressure, in kilopascals (one standard atmosphere).
|
|
11
|
+
PR = 101.325
|
|
12
|
+
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
# Pure-tone atmospheric absorption coefficient α from ISO 9613-1:1993,
|
|
16
|
+
# equations (3)–(5) and the Annex B conversion from relative humidity
|
|
17
|
+
# to molar concentration of water vapour.
|
|
18
|
+
#
|
|
19
|
+
# The coefficient, in decibels per metre, is
|
|
20
|
+
#
|
|
21
|
+
# α = 8.686 f² { 1.84×10⁻¹¹ (p_a/p_r)⁻¹ (T/T₀)^(1/2)
|
|
22
|
+
# + (T/T₀)^(-5/2) [ 0.01275 e^(-2239.1/T) (f_rO + f²/f_rO)⁻¹
|
|
23
|
+
# + 0.1068 e^(-3352.0/T) (f_rN + f²/f_rN)⁻¹ ] }
|
|
24
|
+
#
|
|
25
|
+
# with oxygen and nitrogen relaxation frequencies
|
|
26
|
+
#
|
|
27
|
+
# f_rO = (p_a/p_r) [ 24 + 4.04×10⁴ h (0.02+h)/(0.391+h) ]
|
|
28
|
+
# f_rN = (p_a/p_r) (T/T₀)^(-1/2)
|
|
29
|
+
# { 9 + 280 h exp[ -4.170 ((T/T₀)^(-1/3) - 1) ] }
|
|
30
|
+
#
|
|
31
|
+
# The constants T₀ = 293.15 K and p_r = 101.325 kPa are the reference
|
|
32
|
+
# conditions of ISO 9613-1:1993, clause 4.2. The molar concentration
|
|
33
|
+
# of water vapour h, as a percentage, is h = h_r (p_sat / p_a), with
|
|
34
|
+
# p_sat/p_r = 10^(-6.8346 (T₀₁/T)^1.261 + 4.6151) and T₀₁ = 273.16 K
|
|
35
|
+
# (triple-point temperature of water), from Annex B of the same
|
|
36
|
+
# standard. All numerical coefficients are those printed in the
|
|
37
|
+
# standard; none are fitted or invented.
|
|
38
|
+
#
|
|
39
|
+
# ISO 9613-1:1993 states an accuracy of about ±10 % for temperatures
|
|
40
|
+
# from −20 to +50 °C and molar concentrations of water vapour between
|
|
41
|
+
# 0.05 % and 5 % (clause 7). Tabulated values in the standard cover
|
|
42
|
+
# 50 Hz to 10 kHz; the same equations are specified for use outside
|
|
43
|
+
# that frequency grid.
|
|
44
|
+
#
|
|
45
|
+
# @param frequency [Numeric, Array<Numeric>] sound frequency, in hertz.
|
|
46
|
+
# Must be positive.
|
|
47
|
+
# @param temperature [Numeric, Array<Numeric>] air temperature, in
|
|
48
|
+
# degrees Celsius. Defaults to 20 °C, the ISO 9613-1 reference
|
|
49
|
+
# temperature.
|
|
50
|
+
# @param humidity [Numeric, Array<Numeric>] relative humidity, as a
|
|
51
|
+
# percentage in [0, 100]. Defaults to 50.
|
|
52
|
+
# @param pressure [Numeric, Array<Numeric>] ambient atmospheric
|
|
53
|
+
# pressure, in kilopascals. Defaults to 101.325 kPa (one standard
|
|
54
|
+
# atmosphere).
|
|
55
|
+
# @param unit [String] unit of the returned coefficient: `"dB/m"`
|
|
56
|
+
# (the unit of ISO 9613-1 equation (5)) or `"dB/km"` (the unit of
|
|
57
|
+
# Table 1 of the standard).
|
|
58
|
+
#
|
|
59
|
+
# @return [Float, Array<Float>] attenuation coefficient(s) in the
|
|
60
|
+
# requested unit. An array is returned when any input is an array.
|
|
61
|
+
#
|
|
62
|
+
# @raise [ArgumentError] if a frequency or pressure is not positive,
|
|
63
|
+
# humidity is outside [0, 100], temperature is at or below absolute
|
|
64
|
+
# zero, or +unit+ is not `"dB/m"` or `"dB/km"`.
|
|
65
|
+
#
|
|
66
|
+
# @note A warning is issued if any temperature lies outside the
|
|
67
|
+
# −20 to +50 °C accuracy range stated in ISO 9613-1:1993.
|
|
68
|
+
#
|
|
69
|
+
# @example About 4.66 dB/km at 1 kHz, 20 °C, 50 % RH, 1 atm
|
|
70
|
+
# HornAcoustics.atmospheric_absorption(1000, unit: "dB/km")
|
|
71
|
+
#
|
|
72
|
+
# @example Absorption rises with frequency in the audio band
|
|
73
|
+
# HornAcoustics.atmospheric_absorption([125, 250, 500, 1000, 2000], unit: "dB/km")
|
|
74
|
+
#
|
|
75
|
+
# @see HornAcoustics.spl_at_distance
|
|
76
|
+
# @see https://www.iso.org/standard/17426.html ISO 9613-1:1993
|
|
77
|
+
#
|
|
78
|
+
# @reference International Organization for Standardization (1993).
|
|
79
|
+
# *Acoustics — Attenuation of sound during propagation outdoors —
|
|
80
|
+
# Part 1: Calculation of the absorption of sound by the atmosphere.*
|
|
81
|
+
# ISO 9613-1:1993.
|
|
82
|
+
def atmospheric_absorption(frequency, temperature: 20, humidity: 50,
|
|
83
|
+
pressure: 101.325, unit: "dB/m")
|
|
84
|
+
unless %w[dB/m dB/km].include?(unit)
|
|
85
|
+
raise ArgumentError, 'unit must be "dB/m" or "dB/km".'
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
Util.check_positive_each(frequency, "frequency")
|
|
89
|
+
Util.check_positive_each(pressure, "pressure")
|
|
90
|
+
Util.check_numeric_each(humidity, "humidity")
|
|
91
|
+
Util.check_numeric_each(temperature, "temperature")
|
|
92
|
+
|
|
93
|
+
freqs, temps, hums, press = Util.broadcast(frequency, temperature, humidity, pressure)
|
|
94
|
+
|
|
95
|
+
if hums.any? { |h| h < 0 || h > 100 }
|
|
96
|
+
raise ArgumentError,
|
|
97
|
+
"humidity must be a relative humidity in [0, 100] percent."
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
if temps.any? { |t| t < -20 || t > 50 }
|
|
101
|
+
warn "ISO 9613-1:1993 states its +/- 10% accuracy for temperatures " \
|
|
102
|
+
"from -20 to +50 degrees Celsius."
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
alphas = freqs.zip(temps, hums, press).map do |f, t_c, rh, p_a|
|
|
106
|
+
coefficient_db_per_m(f, t_c, rh, p_a)
|
|
107
|
+
end
|
|
108
|
+
alphas = alphas.map { |a| a * 1000.0 } if unit == "dB/km"
|
|
109
|
+
Util.maybe_scalar(alphas, frequency, temperature, humidity, pressure)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# ISO 9613-1:1993 equation (5), in dB/m, with Annex B humidity and
|
|
113
|
+
# equations (3)–(4) for the relaxation frequencies.
|
|
114
|
+
# @api private
|
|
115
|
+
def coefficient_db_per_m(frequency, temperature_c, humidity, pressure)
|
|
116
|
+
t_k = Util.to_kelvin(temperature_c)
|
|
117
|
+
# Annex B: saturation vapour pressure ratio and molar concentration h (%).
|
|
118
|
+
c = -6.8346 * ((T01 / t_k)**1.261) + 4.6151
|
|
119
|
+
psat_over_pr = 10**c
|
|
120
|
+
h = humidity * (psat_over_pr / (pressure / PR))
|
|
121
|
+
|
|
122
|
+
# Equations (3) and (4).
|
|
123
|
+
fr_o = (pressure / PR) * (24.0 + 4.04e4 * h * (0.02 + h) / (0.391 + h))
|
|
124
|
+
fr_n = (pressure / PR) * ((t_k / T0)**(-0.5)) *
|
|
125
|
+
(9.0 + 280.0 * h * Math.exp(-4.170 * (((t_k / T0)**(-1.0 / 3.0)) - 1.0)))
|
|
126
|
+
|
|
127
|
+
# Equation (5), in dB/m. The leading factor 8.686 is as printed in the
|
|
128
|
+
# standard (20 / ln(10) rounded to four significant figures).
|
|
129
|
+
8.686 * (frequency**2) * (
|
|
130
|
+
1.84e-11 * ((pressure / PR)**(-1)) * ((t_k / T0)**0.5) +
|
|
131
|
+
((t_k / T0)**(-2.5)) * (
|
|
132
|
+
0.01275 * Math.exp(-2239.1 / t_k) * ((fr_o + (frequency**2) / fr_o)**(-1)) +
|
|
133
|
+
0.1068 * Math.exp(-3352.0 / t_k) * ((fr_n + (frequency**2) / fr_n)**(-1))
|
|
134
|
+
)
|
|
135
|
+
)
|
|
136
|
+
end
|
|
137
|
+
private_class_method :coefficient_db_per_m
|
|
138
|
+
end
|
|
139
|
+
end
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HornAcoustics
|
|
4
|
+
# Equal-tempered frequencies of multi-chime horn notes (ISO 16:1975).
|
|
5
|
+
module ChordFrequencies
|
|
6
|
+
# Pitch class in semitones from C: C=0, D=2, E=4, F=5, G=7, A=9, B=11.
|
|
7
|
+
PITCH_CLASS = {
|
|
8
|
+
"C" => 0,
|
|
9
|
+
"D" => 2,
|
|
10
|
+
"E" => 4,
|
|
11
|
+
"F" => 5,
|
|
12
|
+
"G" => 7,
|
|
13
|
+
"A" => 9,
|
|
14
|
+
"B" => 11
|
|
15
|
+
}.freeze
|
|
16
|
+
|
|
17
|
+
NOTE_RE = /\A\s*([A-Ga-g])([#b]?)(-?\d+)\s*\z/.freeze
|
|
18
|
+
|
|
19
|
+
module_function
|
|
20
|
+
|
|
21
|
+
# Frequencies of named notes in twelve-tone equal temperament with
|
|
22
|
+
# the standard concert pitch A4 = 440 Hz given by ISO 16:1975. Each
|
|
23
|
+
# semitone is a frequency ratio of 2^(1/12), so a note n semitones
|
|
24
|
+
# above A4 has frequency
|
|
25
|
+
#
|
|
26
|
+
# f = 440 × 2^(n/12)
|
|
27
|
+
#
|
|
28
|
+
# Note names are scientific pitch notation: a letter A–G, an
|
|
29
|
+
# optional accidental `#` (sharp) or `b` (flat), and an octave
|
|
30
|
+
# number (for example `"A4"`, `"F#4"`, `"Db5"`). Middle C is `"C4"`.
|
|
31
|
+
# Octave numbers may be zero or negative (`"C0"`, `"B-1"`) in the
|
|
32
|
+
# usual way.
|
|
33
|
+
#
|
|
34
|
+
# No instrument-specific tuning tables or measured horn spectra are
|
|
35
|
+
# used; the result is exactly the equal-tempered frequency of the
|
|
36
|
+
# named pitch.
|
|
37
|
+
#
|
|
38
|
+
# @param notes [String, Array<String>] note names, or a single
|
|
39
|
+
# string of notes separated by spaces or commas (for example
|
|
40
|
+
# `"D4 F#4 A4"`).
|
|
41
|
+
# @param a4 [Numeric] frequency of A4, in hertz. Defaults to 440,
|
|
42
|
+
# the value specified in ISO 16:1975. Other values are provided
|
|
43
|
+
# only so that a known historical pitch standard can be
|
|
44
|
+
# substituted; they are not used unless you pass them.
|
|
45
|
+
#
|
|
46
|
+
# @return [Hash{String=>Float}] frequencies in hertz, keyed by the
|
|
47
|
+
# supplied note names, in input order.
|
|
48
|
+
#
|
|
49
|
+
# @raise [ArgumentError] if +a4+ is not a single positive number, a
|
|
50
|
+
# numeric value is passed in place of note names, a note name
|
|
51
|
+
# cannot be parsed, or +notes+ is empty.
|
|
52
|
+
#
|
|
53
|
+
# @example A common three-chime train-horn major triad
|
|
54
|
+
# HornAcoustics.chord_frequencies(["D4", "F#4", "A4"])
|
|
55
|
+
#
|
|
56
|
+
# @example A4 is 440 Hz; the A an octave above is 880 Hz
|
|
57
|
+
# HornAcoustics.chord_frequencies(["A4", "A5"])
|
|
58
|
+
#
|
|
59
|
+
# @see HornAcoustics.doppler_shift
|
|
60
|
+
# @see https://www.iso.org/standard/3601.html ISO 16:1975
|
|
61
|
+
#
|
|
62
|
+
# @reference International Organization for Standardization (1975).
|
|
63
|
+
# *Acoustics — Standard tuning frequency (Standard musical pitch).*
|
|
64
|
+
# ISO 16:1975. The twelve-tone equal-tempered scale with a common
|
|
65
|
+
# ratio of 2^(1/12) is the scale that ISO 16 anchors at A4 = 440 Hz.
|
|
66
|
+
def chord_frequencies(notes, a4: 440)
|
|
67
|
+
Util.check_positive(a4, "a4")
|
|
68
|
+
unless a4.is_a?(Numeric) && !a4.is_a?(Array)
|
|
69
|
+
raise ArgumentError, "a4 must be a single positive number."
|
|
70
|
+
end
|
|
71
|
+
if notes.is_a?(Numeric)
|
|
72
|
+
raise ArgumentError,
|
|
73
|
+
'notes must be note names such as "A4" or "F#4", not numbers.'
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
names = normalize_notes(notes)
|
|
77
|
+
if names.empty?
|
|
78
|
+
raise ArgumentError, "notes must contain at least one note name."
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
names.each_with_object({}) do |note, out|
|
|
82
|
+
out[note] = a4 * (2**(semitones_from_a4(note) / 12.0))
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# @api private
|
|
87
|
+
def normalize_notes(notes)
|
|
88
|
+
list =
|
|
89
|
+
if notes.is_a?(Array)
|
|
90
|
+
notes.map(&:to_s)
|
|
91
|
+
else
|
|
92
|
+
text = notes.to_s
|
|
93
|
+
if text.match?(/[[:space:],]/)
|
|
94
|
+
text.split(/[[:space:],]+/).reject(&:empty?)
|
|
95
|
+
else
|
|
96
|
+
[text]
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
list
|
|
100
|
+
end
|
|
101
|
+
private_class_method :normalize_notes
|
|
102
|
+
|
|
103
|
+
# Semitones from A4, using MIDI numbering with C4 = 60, A4 = 69.
|
|
104
|
+
# @api private
|
|
105
|
+
def semitones_from_a4(note)
|
|
106
|
+
match = NOTE_RE.match(note)
|
|
107
|
+
unless match
|
|
108
|
+
raise ArgumentError,
|
|
109
|
+
"Cannot parse note name \"#{note}\". Use scientific pitch " \
|
|
110
|
+
"notation such as \"A4\", \"F#4\", or \"Db5\"."
|
|
111
|
+
end
|
|
112
|
+
letter = match[1].upcase
|
|
113
|
+
accidental = match[2]
|
|
114
|
+
octave = Integer(match[3])
|
|
115
|
+
pc = PITCH_CLASS[letter]
|
|
116
|
+
pc += 1 if accidental == "#"
|
|
117
|
+
pc -= 1 if accidental == "b"
|
|
118
|
+
midi = (octave + 1) * 12 + pc
|
|
119
|
+
midi - 69
|
|
120
|
+
end
|
|
121
|
+
private_class_method :semitones_from_a4
|
|
122
|
+
end
|
|
123
|
+
end
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HornAcoustics
|
|
4
|
+
# Classical Doppler shift for a moving source or observer.
|
|
5
|
+
module DopplerShift
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
# Observed frequency of a tone when the source and/or the observer
|
|
9
|
+
# move along the line that joins them, in a still medium. The
|
|
10
|
+
# classical (non-relativistic) Doppler formula is
|
|
11
|
+
#
|
|
12
|
+
# f = f₀ (c + v_o) / (c − v_s)
|
|
13
|
+
#
|
|
14
|
+
# where v_o is the component of the observer's speed toward the
|
|
15
|
+
# source, v_s is the component of the source's speed toward the
|
|
16
|
+
# observer, and c is the speed of sound. The sign convention is the
|
|
17
|
+
# usual textbook one: motion of either party toward the other raises
|
|
18
|
+
# the observed frequency.
|
|
19
|
+
#
|
|
20
|
+
# The speed of sound is obtained from air temperature by
|
|
21
|
+
# {HornAcoustics.speed_of_sound}, which uses the sea-level standard
|
|
22
|
+
# atmosphere of ISO 2533:1975 (c = 340.294 √(T/T_n) m/s, T_n = 288.15 K).
|
|
23
|
+
# Motions not along the line of sight are not treated; pass the
|
|
24
|
+
# line-of-sight components as the speeds.
|
|
25
|
+
#
|
|
26
|
+
# @param frequency [Numeric, Array<Numeric>] source (emitted)
|
|
27
|
+
# frequency, in hertz. Must be positive.
|
|
28
|
+
# @param source_speed [Numeric, Array<Numeric>] speed of the source,
|
|
29
|
+
# in metres per second. Must be non-negative. Defaults to 0
|
|
30
|
+
# (stationary source).
|
|
31
|
+
# @param observer_speed [Numeric, Array<Numeric>] speed of the
|
|
32
|
+
# observer, in metres per second. Must be non-negative. Defaults
|
|
33
|
+
# to 0 (stationary observer).
|
|
34
|
+
# @param temperature [Numeric, Array<Numeric>] air temperature in
|
|
35
|
+
# degrees Celsius, used to obtain the speed of sound. Defaults to 20.
|
|
36
|
+
# @param source_towards_observer [Boolean, Array<Boolean>] if +true+
|
|
37
|
+
# (the default), the source moves toward the observer; if +false+,
|
|
38
|
+
# it moves away.
|
|
39
|
+
# @param observer_towards_source [Boolean, Array<Boolean>] if +true+
|
|
40
|
+
# (the default), the observer moves toward the source; if +false+,
|
|
41
|
+
# it moves away.
|
|
42
|
+
#
|
|
43
|
+
# @return [Float, Array<Float>] observed frequency, in hertz.
|
|
44
|
+
#
|
|
45
|
+
# @raise [ArgumentError] if a frequency is not positive, a speed is
|
|
46
|
+
# negative, a direction flag is not a boolean, or the line-of-sight
|
|
47
|
+
# source speed toward the observer is at or above the speed of sound
|
|
48
|
+
# (the classical formula does not apply at or above Mach 1).
|
|
49
|
+
#
|
|
50
|
+
# @example Stationary 440 Hz source and observer: no shift
|
|
51
|
+
# HornAcoustics.doppler_shift(440)
|
|
52
|
+
#
|
|
53
|
+
# @example Train-horn tone approaching a wayside observer at 30 m/s (~67 mph)
|
|
54
|
+
# HornAcoustics.doppler_shift(440, source_speed: 30, source_towards_observer: true)
|
|
55
|
+
#
|
|
56
|
+
# @example The same source receding
|
|
57
|
+
# HornAcoustics.doppler_shift(440, source_speed: 30, source_towards_observer: false)
|
|
58
|
+
#
|
|
59
|
+
# @see HornAcoustics.speed_of_sound
|
|
60
|
+
# @see HornAcoustics.chord_frequencies
|
|
61
|
+
#
|
|
62
|
+
# @reference The classical Doppler formula for a moving source and
|
|
63
|
+
# observer in a still medium, in the form f = f₀ (c + v_o) / (c − v_s)
|
|
64
|
+
# with the sign convention above, is the standard result of linear
|
|
65
|
+
# acoustics; see e.g. Morse, P. M. and Ingard, K. U. (1968),
|
|
66
|
+
# *Theoretical Acoustics*, McGraw-Hill, section 11.1. The speed of
|
|
67
|
+
# sound as a function of temperature follows ISO 2533:1975; see
|
|
68
|
+
# {HornAcoustics.speed_of_sound}.
|
|
69
|
+
def doppler_shift(frequency, source_speed: 0, observer_speed: 0,
|
|
70
|
+
temperature: 20, source_towards_observer: true,
|
|
71
|
+
observer_towards_source: true)
|
|
72
|
+
Util.check_positive_each(frequency, "frequency")
|
|
73
|
+
Util.check_positive_each(source_speed, "source_speed", strict: false)
|
|
74
|
+
Util.check_positive_each(observer_speed, "observer_speed", strict: false)
|
|
75
|
+
Util.check_numeric_each(temperature, "temperature")
|
|
76
|
+
|
|
77
|
+
freqs, v_src, v_obs, temps, src_towards, obs_towards = Util.broadcast(
|
|
78
|
+
frequency, source_speed, observer_speed, temperature,
|
|
79
|
+
source_towards_observer, observer_towards_source
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
unless src_towards.all? { |v| v == true || v == false }
|
|
83
|
+
raise ArgumentError, "source_towards_observer must be true or false."
|
|
84
|
+
end
|
|
85
|
+
unless obs_towards.all? { |v| v == true || v == false }
|
|
86
|
+
raise ArgumentError, "observer_towards_source must be true or false."
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
observed = freqs.zip(v_src, v_obs, temps, src_towards, obs_towards).map do |f0, vs, vo, t_c, st, ot|
|
|
90
|
+
c_sound = SpeedOfSound.speed_of_sound(t_c)
|
|
91
|
+
v_s = st ? vs : -vs
|
|
92
|
+
v_o = ot ? vo : -vo
|
|
93
|
+
denom = c_sound - v_s
|
|
94
|
+
if denom <= 0
|
|
95
|
+
raise ArgumentError,
|
|
96
|
+
"The line-of-sight source speed toward the observer must be " \
|
|
97
|
+
"smaller than the speed of sound (the classical formula does " \
|
|
98
|
+
"not apply at or above Mach 1)."
|
|
99
|
+
end
|
|
100
|
+
f0 * (c_sound + v_o) / denom
|
|
101
|
+
end
|
|
102
|
+
Util.maybe_scalar(
|
|
103
|
+
observed,
|
|
104
|
+
frequency, source_speed, observer_speed, temperature,
|
|
105
|
+
source_towards_observer, observer_towards_source
|
|
106
|
+
)
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HornAcoustics
|
|
4
|
+
# One row of a 49 CFR 222.21 (b)(2)–(b)(3) sounding window.
|
|
5
|
+
#
|
|
6
|
+
# @!attribute speed
|
|
7
|
+
# @return [Numeric] the input speed, in {#speed_unit}
|
|
8
|
+
# @!attribute speed_unit
|
|
9
|
+
# @return [String] `"mph"`, `"km/h"`, or `"m/s"`
|
|
10
|
+
# @!attribute start_distance_min
|
|
11
|
+
# @return [Float] distance from the crossing at which sounding must
|
|
12
|
+
# have begun (the 15-second point, or 1,320 feet if the cap in
|
|
13
|
+
# (b)(3) is tighter), in {#distance_unit}
|
|
14
|
+
# @!attribute start_distance_max
|
|
15
|
+
# @return [Float] farthest distance from the crossing at which
|
|
16
|
+
# sounding may begin (the 20-second point, capped at 1,320 feet
|
|
17
|
+
# when (b)(3) applies), in {#distance_unit}
|
|
18
|
+
# @!attribute distance_unit
|
|
19
|
+
# @return [String] `"ft"`, `"m"`, or `"mi"`
|
|
20
|
+
# @!attribute start_time_min
|
|
21
|
+
# @return [Float] seconds before entering the crossing that
|
|
22
|
+
# correspond to {#start_distance_min}
|
|
23
|
+
# @!attribute start_time_max
|
|
24
|
+
# @return [Float] seconds before entering the crossing that
|
|
25
|
+
# correspond to {#start_distance_max}
|
|
26
|
+
# @!attribute quarter_mile_cap_applied
|
|
27
|
+
# @return [Boolean] +true+ when speed is greater than 60 mph and the
|
|
28
|
+
# 1,320-foot cap of (b)(3) has limited the window
|
|
29
|
+
Window = Struct.new(
|
|
30
|
+
:speed,
|
|
31
|
+
:speed_unit,
|
|
32
|
+
:start_distance_min,
|
|
33
|
+
:start_distance_max,
|
|
34
|
+
:distance_unit,
|
|
35
|
+
:start_time_min,
|
|
36
|
+
:start_time_max,
|
|
37
|
+
:quarter_mile_cap_applied,
|
|
38
|
+
keyword_init: true
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
# Grade-crossing locomotive-horn sounding window (49 CFR 222.21).
|
|
42
|
+
module HornWindow
|
|
43
|
+
# International mile, in feet (the regulation writes 1,320 feet).
|
|
44
|
+
FT_PER_MI = 5280.0
|
|
45
|
+
# International foot, in metres (0.3048 m exactly).
|
|
46
|
+
M_PER_FT = 0.3048
|
|
47
|
+
# Quarter-mile cap of 49 CFR 222.21 (b)(3), in feet.
|
|
48
|
+
CAP_FT = 1320.0
|
|
49
|
+
# Speed above which the (b)(3) cap applies, in miles per hour.
|
|
50
|
+
CAP_SPEED_MPH = 60.0
|
|
51
|
+
# International mile in kilometres (exactly 1.609344).
|
|
52
|
+
KM_PER_MI = 1.609344
|
|
53
|
+
|
|
54
|
+
module_function
|
|
55
|
+
|
|
56
|
+
# Distances and times at which a locomotive horn must begin to be
|
|
57
|
+
# sounded when approaching a public highway-rail grade crossing,
|
|
58
|
+
# taken only from 49 CFR 222.21 (b)(2) and (b)(3) (current text of
|
|
59
|
+
# the U.S. *Electronic Code of Federal Regulations*).
|
|
60
|
+
#
|
|
61
|
+
# Paragraph (b)(2) requires that the horn begin to be sounded at
|
|
62
|
+
# least 15 seconds, but no more than 20 seconds, before the
|
|
63
|
+
# locomotive enters the crossing. Paragraph (b)(3) adds that trains
|
|
64
|
+
# traveling at speeds in excess of 60 mph shall not begin sounding
|
|
65
|
+
# the horn more than one-quarter mile (1,320 feet) in advance of the
|
|
66
|
+
# nearest public highway-rail grade crossing, even if that makes the
|
|
67
|
+
# advance warning shorter than 15 seconds. At 60 mph, 15 seconds of
|
|
68
|
+
# travel is exactly one-quarter mile, so the distance cap first
|
|
69
|
+
# binds only above 60 mph.
|
|
70
|
+
#
|
|
71
|
+
# The following provisions of the same section are documented here
|
|
72
|
+
# but are not implemented: the good-faith allowance in (b)(2) to
|
|
73
|
+
# begin sounding as early as 25 seconds when the engineer cannot
|
|
74
|
+
# precisely estimate arrival; the Chicago-Region exception in
|
|
75
|
+
# paragraph (c); and the close-proximity / stopped-train exceptions
|
|
76
|
+
# in paragraph (d). The sounding pattern of two long, one short and
|
|
77
|
+
# one long blasts in paragraph (a) is also outside the scope of this
|
|
78
|
+
# helper.
|
|
79
|
+
#
|
|
80
|
+
# @param speed [Numeric, Array<Numeric>] train speed. Must be
|
|
81
|
+
# positive. A list of speeds returns one {HornAcoustics::Window}
|
|
82
|
+
# per speed.
|
|
83
|
+
# @param speed_unit [String] unit of +speed+: `"mph"` (the unit used
|
|
84
|
+
# in the regulation), `"km/h"`, or `"m/s"`. Defaults to `"mph"`.
|
|
85
|
+
# @param distance_unit [String] unit of the returned start
|
|
86
|
+
# distances: `"ft"` (the regulation states the cap as 1,320 feet),
|
|
87
|
+
# `"m"`, or `"mi"`. Defaults to `"ft"`.
|
|
88
|
+
#
|
|
89
|
+
# @return [HornAcoustics::Window, Array<HornAcoustics::Window>] one
|
|
90
|
+
# window when +speed+ is a single number, otherwise one per speed.
|
|
91
|
+
#
|
|
92
|
+
# @raise [ArgumentError] if a speed is not positive, or a unit is
|
|
93
|
+
# not one of the values listed above.
|
|
94
|
+
#
|
|
95
|
+
# @example At 45 mph the 15–20 s window is shorter than a quarter mile
|
|
96
|
+
# HornAcoustics.horn_window(45)
|
|
97
|
+
#
|
|
98
|
+
# @example At 60 mph, 15 s is exactly a quarter mile; the cap is not yet applied
|
|
99
|
+
# HornAcoustics.horn_window(60)
|
|
100
|
+
#
|
|
101
|
+
# @example Above 60 mph the 1,320 ft cap binds and the warning can be under 15 s
|
|
102
|
+
# HornAcoustics.horn_window(80)
|
|
103
|
+
#
|
|
104
|
+
# @see https://www.ecfr.gov/current/title-49/section-222.21 49 CFR 222.21
|
|
105
|
+
#
|
|
106
|
+
# @reference *When must a locomotive horn be used?*, 49 CFR 222.21,
|
|
107
|
+
# https://www.ecfr.gov/current/title-49/section-222.21
|
|
108
|
+
# The quarter-mile cap is the 1,320 feet stated in paragraph
|
|
109
|
+
# (b)(3). Unit conversions use the international foot (0.3048 m
|
|
110
|
+
# exactly) and the international mile (5,280 feet).
|
|
111
|
+
def horn_window(speed, speed_unit: "mph", distance_unit: "ft")
|
|
112
|
+
unless %w[mph km/h m/s].include?(speed_unit)
|
|
113
|
+
raise ArgumentError, 'speed_unit must be "mph", "km/h", or "m/s".'
|
|
114
|
+
end
|
|
115
|
+
unless %w[ft m mi].include?(distance_unit)
|
|
116
|
+
raise ArgumentError, 'distance_unit must be "ft", "m", or "mi".'
|
|
117
|
+
end
|
|
118
|
+
Util.check_positive_each(speed, "speed")
|
|
119
|
+
|
|
120
|
+
rows = Util.list(speed).map do |v|
|
|
121
|
+
speed_mph = to_mph(v, speed_unit)
|
|
122
|
+
# Distances travelled in 15 s and 20 s, in feet. 1 mph = 5280/3600 ft/s.
|
|
123
|
+
ft_per_s = speed_mph * FT_PER_MI / 3600.0
|
|
124
|
+
d15_ft = ft_per_s * 15.0
|
|
125
|
+
d20_ft = ft_per_s * 20.0
|
|
126
|
+
|
|
127
|
+
cap_applies = speed_mph > CAP_SPEED_MPH
|
|
128
|
+
start_max_ft = cap_applies ? [d20_ft, CAP_FT].min : d20_ft
|
|
129
|
+
start_min_ft = cap_applies ? [d15_ft, CAP_FT].min : d15_ft
|
|
130
|
+
cap_applied = cap_applies && (d15_ft > CAP_FT || d20_ft > CAP_FT)
|
|
131
|
+
|
|
132
|
+
Window.new(
|
|
133
|
+
speed: v,
|
|
134
|
+
speed_unit: speed_unit,
|
|
135
|
+
start_distance_min: to_distance_unit(start_min_ft, distance_unit),
|
|
136
|
+
start_distance_max: to_distance_unit(start_max_ft, distance_unit),
|
|
137
|
+
distance_unit: distance_unit,
|
|
138
|
+
start_time_min: start_min_ft / ft_per_s,
|
|
139
|
+
start_time_max: start_max_ft / ft_per_s,
|
|
140
|
+
quarter_mile_cap_applied: cap_applied
|
|
141
|
+
)
|
|
142
|
+
end
|
|
143
|
+
speed.is_a?(Array) ? rows : rows.first
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
# @api private
|
|
147
|
+
def to_mph(speed, speed_unit)
|
|
148
|
+
case speed_unit
|
|
149
|
+
when "mph" then speed.to_f
|
|
150
|
+
when "km/h" then speed.to_f / KM_PER_MI
|
|
151
|
+
when "m/s" then speed.to_f / (KM_PER_MI * 1000.0 / 3600.0)
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
private_class_method :to_mph
|
|
155
|
+
|
|
156
|
+
# @api private
|
|
157
|
+
def to_distance_unit(feet, distance_unit)
|
|
158
|
+
case distance_unit
|
|
159
|
+
when "ft" then feet
|
|
160
|
+
when "m" then feet * M_PER_FT
|
|
161
|
+
when "mi" then feet / FT_PER_MI
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
private_class_method :to_distance_unit
|
|
165
|
+
end
|
|
166
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HornAcoustics
|
|
4
|
+
# Speed of sound in air from temperature (ISO 2533:1975).
|
|
5
|
+
module SpeedOfSound
|
|
6
|
+
# Sea-level standard speed of sound, in m/s (ISO 2533:1975).
|
|
7
|
+
AN = 340.294
|
|
8
|
+
# Sea-level standard temperature, in kelvin (ISO 2533:1975).
|
|
9
|
+
TN = 288.15
|
|
10
|
+
|
|
11
|
+
module_function
|
|
12
|
+
|
|
13
|
+
# Speed of sound in dry air at ambient temperature after the
|
|
14
|
+
# International Standard Atmosphere, ISO 2533:1975. At the standard
|
|
15
|
+
# sea-level temperature T_n = 288.15 K (15 °C) the standard gives
|
|
16
|
+
# a_n = 340.294 m/s. Under the ideal-gas square-root law used by
|
|
17
|
+
# that standard,
|
|
18
|
+
#
|
|
19
|
+
# c = 340.294 √(T / T_n)
|
|
20
|
+
#
|
|
21
|
+
# with T in kelvin. Humidity and static-pressure corrections are
|
|
22
|
+
# not applied; only the temperature dependence of the standard
|
|
23
|
+
# atmosphere is used.
|
|
24
|
+
#
|
|
25
|
+
# @param temperature [Numeric, Array<Numeric>] air temperature, in
|
|
26
|
+
# degrees Celsius. Defaults to 20.
|
|
27
|
+
#
|
|
28
|
+
# @return [Float, Array<Float>] speed of sound, in metres per second.
|
|
29
|
+
#
|
|
30
|
+
# @raise [ArgumentError] if temperature is at or below absolute zero.
|
|
31
|
+
#
|
|
32
|
+
# @example
|
|
33
|
+
# HornAcoustics.speed_of_sound(15) #=> 340.294
|
|
34
|
+
# HornAcoustics.speed_of_sound([0, 15, 20, 30])
|
|
35
|
+
#
|
|
36
|
+
# @see HornAcoustics.doppler_shift
|
|
37
|
+
# @see https://www.iso.org/standard/7472.html ISO 2533:1975
|
|
38
|
+
#
|
|
39
|
+
# @reference International Organization for Standardization (1975).
|
|
40
|
+
# *Standard Atmosphere.* ISO 2533:1975. Sea-level standard speed
|
|
41
|
+
# of sound a_n = 340.294 m/s at T_n = 288.15 K.
|
|
42
|
+
def speed_of_sound(temperature = 20)
|
|
43
|
+
Util.check_numeric_each(temperature, "temperature")
|
|
44
|
+
values = Util.list(temperature).map do |t_c|
|
|
45
|
+
t_k = Util.to_kelvin(t_c)
|
|
46
|
+
AN * Math.sqrt(t_k / TN)
|
|
47
|
+
end
|
|
48
|
+
Util.maybe_scalar(values, temperature)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HornAcoustics
|
|
4
|
+
# Sound pressure level versus distance (ISO 9613-2:1996, optional ISO 9613-1).
|
|
5
|
+
module SplAtDistance
|
|
6
|
+
module_function
|
|
7
|
+
|
|
8
|
+
# Change in sound pressure level with distance for a point source in
|
|
9
|
+
# a free field, from the inverse-square (spherical spreading) law,
|
|
10
|
+
# with optional atmospheric absorption.
|
|
11
|
+
#
|
|
12
|
+
# Geometrical divergence between two distances follows ISO 9613-2:1996,
|
|
13
|
+
# clause 7.1, equation (7): the attenuation due to spherical spreading
|
|
14
|
+
# is A_div = 20 log₁₀(d/d₀) + 11 dB with d₀ = 1 m. The constant 11 dB
|
|
15
|
+
# (= 10 log₁₀(4π)) converts a sound power level to a sound pressure
|
|
16
|
+
# level at 1 m and cancels when the change between two receiver
|
|
17
|
+
# distances is taken, leaving the inverse-square ratio
|
|
18
|
+
#
|
|
19
|
+
# L(d) = L(d_ref) − 20 log₁₀(d / d_ref)
|
|
20
|
+
#
|
|
21
|
+
# When +absorption+ is +true+, the pure-tone atmospheric absorption
|
|
22
|
+
# of ISO 9613-1:1993 (see {HornAcoustics.atmospheric_absorption}) is
|
|
23
|
+
# added over the path between +distance_ref+ and +distance+. This is
|
|
24
|
+
# the same term as ISO 9613-2:1996, equation (8), A_atm = α d with
|
|
25
|
+
# α in decibels per metre, applied to the path difference:
|
|
26
|
+
#
|
|
27
|
+
# L(d) = L(d_ref) − 20 log₁₀(d / d_ref) − α (d − d_ref)
|
|
28
|
+
#
|
|
29
|
+
# Distances are in metres. The method does not include ground effect,
|
|
30
|
+
# screening, or directivity (the remaining terms of ISO 9613-2); only
|
|
31
|
+
# spherical spreading and, optionally, atmospheric absorption are
|
|
32
|
+
# applied.
|
|
33
|
+
#
|
|
34
|
+
# @param spl_ref [Numeric, Array<Numeric>] sound pressure level at
|
|
35
|
+
# +distance_ref+, in decibels.
|
|
36
|
+
# @param distance [Numeric, Array<Numeric>] receiver distance from
|
|
37
|
+
# the source, in metres. Must be positive.
|
|
38
|
+
# @param distance_ref [Numeric, Array<Numeric>] reference distance at
|
|
39
|
+
# which +spl_ref+ is given, in metres. Defaults to 1.
|
|
40
|
+
# @param absorption [Boolean] if +true+, subtract atmospheric
|
|
41
|
+
# absorption from ISO 9613-1:1993 over the path between
|
|
42
|
+
# +distance_ref+ and +distance+. Defaults to +false+. Must be a
|
|
43
|
+
# single boolean.
|
|
44
|
+
# @param frequency [Numeric, Array<Numeric>, nil] sound frequency in
|
|
45
|
+
# hertz, required when +absorption+ is +true+.
|
|
46
|
+
# @param temperature [Numeric, Array<Numeric>] air temperature in
|
|
47
|
+
# degrees Celsius, passed to {HornAcoustics.atmospheric_absorption}
|
|
48
|
+
# when +absorption+ is +true+. Defaults to 20.
|
|
49
|
+
# @param humidity [Numeric, Array<Numeric>] relative humidity in
|
|
50
|
+
# percent, passed to {HornAcoustics.atmospheric_absorption} when
|
|
51
|
+
# +absorption+ is +true+. Defaults to 50.
|
|
52
|
+
# @param pressure [Numeric, Array<Numeric>] ambient pressure in
|
|
53
|
+
# kilopascals, passed to {HornAcoustics.atmospheric_absorption}
|
|
54
|
+
# when +absorption+ is +true+. Defaults to 101.325.
|
|
55
|
+
#
|
|
56
|
+
# @return [Float, Array<Float>] sound pressure level(s), in
|
|
57
|
+
# decibels, at +distance+.
|
|
58
|
+
#
|
|
59
|
+
# @raise [ArgumentError] if a distance is not positive, +absorption+
|
|
60
|
+
# is not a single true or false, or +frequency+ is omitted when
|
|
61
|
+
# +absorption+ is +true+.
|
|
62
|
+
#
|
|
63
|
+
# @example Inverse-square law only: doubling the distance lowers the level by 20 log₁₀(2) ≈ 6.02 dB
|
|
64
|
+
# HornAcoustics.spl_at_distance(100, 2, distance_ref: 1)
|
|
65
|
+
#
|
|
66
|
+
# @example A 1 kHz tone, including ISO 9613-1 atmospheric absorption
|
|
67
|
+
# HornAcoustics.spl_at_distance(110, 100, distance_ref: 1, absorption: true, frequency: 1000)
|
|
68
|
+
#
|
|
69
|
+
# @see HornAcoustics.atmospheric_absorption
|
|
70
|
+
# @see https://www.iso.org/standard/20649.html ISO 9613-2:1996
|
|
71
|
+
#
|
|
72
|
+
# @reference International Organization for Standardization (1996).
|
|
73
|
+
# *Acoustics — Attenuation of sound during propagation outdoors —
|
|
74
|
+
# Part 2: General method of calculation.* ISO 9613-2:1996, clause 7.1.
|
|
75
|
+
#
|
|
76
|
+
# International Organization for Standardization (1993).
|
|
77
|
+
# *Acoustics — Attenuation of sound during propagation outdoors —
|
|
78
|
+
# Part 1: Calculation of the absorption of sound by the atmosphere.*
|
|
79
|
+
# ISO 9613-1:1993.
|
|
80
|
+
def spl_at_distance(spl_ref, distance, distance_ref: 1, absorption: false,
|
|
81
|
+
frequency: nil, temperature: 20, humidity: 50,
|
|
82
|
+
pressure: 101.325)
|
|
83
|
+
Util.check_numeric_each(spl_ref, "spl_ref")
|
|
84
|
+
Util.check_positive_each(distance, "distance")
|
|
85
|
+
Util.check_positive_each(distance_ref, "distance_ref")
|
|
86
|
+
unless absorption == true || absorption == false
|
|
87
|
+
raise ArgumentError, "absorption must be a single true or false."
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
refs, dists, drefs = Util.broadcast(spl_ref, distance, distance_ref)
|
|
91
|
+
levels = refs.zip(dists, drefs).map do |l_ref, d, d0|
|
|
92
|
+
l_ref - 20.0 * Math.log10(d / d0)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
if absorption
|
|
96
|
+
if frequency.nil?
|
|
97
|
+
raise ArgumentError, "frequency must be supplied when absorption = true."
|
|
98
|
+
end
|
|
99
|
+
alphas = AtmosphericAbsorption.atmospheric_absorption(
|
|
100
|
+
frequency,
|
|
101
|
+
temperature: temperature,
|
|
102
|
+
humidity: humidity,
|
|
103
|
+
pressure: pressure,
|
|
104
|
+
unit: "dB/m"
|
|
105
|
+
)
|
|
106
|
+
alpha_list, dist_list, dref_list = Util.broadcast(alphas, dists, drefs)
|
|
107
|
+
levels = levels.zip(alpha_list, dist_list, dref_list).map do |level, alpha, d, d0|
|
|
108
|
+
level - alpha * (d - d0)
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
Util.maybe_scalar(levels, spl_ref, distance, distance_ref, frequency)
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module HornAcoustics
|
|
4
|
+
# Shared input checks and Kelvin conversion. Not part of the public API.
|
|
5
|
+
# @api private
|
|
6
|
+
module Util
|
|
7
|
+
module_function
|
|
8
|
+
|
|
9
|
+
# Recycle Ruby values the way R recycles vectors: a bare Numeric or
|
|
10
|
+
# one-element list is expanded to +length+, otherwise the list must
|
|
11
|
+
# already be that long or a multiple that tiles evenly.
|
|
12
|
+
def list(value)
|
|
13
|
+
value.is_a?(Array) ? value : [value]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def broadcast(*values)
|
|
17
|
+
lists = values.map { |v| list(v) }
|
|
18
|
+
length = lists.map(&:length).max
|
|
19
|
+
lists.map { |items| recycle(items, length) }
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def recycle(items, length)
|
|
23
|
+
return items if items.length == length
|
|
24
|
+
if length % items.length != 0
|
|
25
|
+
raise ArgumentError,
|
|
26
|
+
"input lengths (#{items.length} and #{length}) are not compatible"
|
|
27
|
+
end
|
|
28
|
+
items * (length / items.length)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def check_numeric(value, name, finite: true)
|
|
32
|
+
unless value.is_a?(Numeric)
|
|
33
|
+
raise ArgumentError, "#{name} must be numeric."
|
|
34
|
+
end
|
|
35
|
+
if finite && !value.finite?
|
|
36
|
+
raise ArgumentError, "#{name} must be finite."
|
|
37
|
+
end
|
|
38
|
+
value
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def check_positive(value, name, strict: true)
|
|
42
|
+
check_numeric(value, name)
|
|
43
|
+
if strict && value <= 0
|
|
44
|
+
raise ArgumentError, "#{name} must be greater than 0."
|
|
45
|
+
end
|
|
46
|
+
if !strict && value < 0
|
|
47
|
+
raise ArgumentError, "#{name} must be greater than or equal to 0."
|
|
48
|
+
end
|
|
49
|
+
value
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def check_numeric_each(values, name, finite: true)
|
|
53
|
+
list(values).each { |v| check_numeric(v, name, finite: finite) }
|
|
54
|
+
values
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def check_positive_each(values, name, strict: true)
|
|
58
|
+
list(values).each { |v| check_positive(v, name, strict: strict) }
|
|
59
|
+
values
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Convert degrees Celsius to kelvin. ISO 9613-1 and ISO 2533 use T in K.
|
|
63
|
+
def to_kelvin(temperature_c)
|
|
64
|
+
check_numeric(temperature_c, "temperature")
|
|
65
|
+
kelvin = temperature_c + 273.15
|
|
66
|
+
if kelvin <= 0
|
|
67
|
+
raise ArgumentError,
|
|
68
|
+
"temperature must be greater than -273.15 degrees Celsius."
|
|
69
|
+
end
|
|
70
|
+
kelvin
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def maybe_scalar(values, *originals)
|
|
74
|
+
if originals.any? { |item| item.is_a?(Array) } || values.length != 1
|
|
75
|
+
values
|
|
76
|
+
else
|
|
77
|
+
values.first
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "horn_acoustics/version"
|
|
4
|
+
require_relative "horn_acoustics/util"
|
|
5
|
+
require_relative "horn_acoustics/atmospheric_absorption"
|
|
6
|
+
require_relative "horn_acoustics/speed_of_sound"
|
|
7
|
+
require_relative "horn_acoustics/doppler_shift"
|
|
8
|
+
require_relative "horn_acoustics/horn_window"
|
|
9
|
+
require_relative "horn_acoustics/chord_frequencies"
|
|
10
|
+
require_relative "horn_acoustics/spl_at_distance"
|
|
11
|
+
|
|
12
|
+
# Outdoor acoustics helpers used with train horns: how the sound-pressure
|
|
13
|
+
# level of a point source changes with distance, how much air absorbs a
|
|
14
|
+
# tone, the Doppler shift of a moving source or observer, the sounding
|
|
15
|
+
# window required at a U.S. public highway-rail grade crossing, and the
|
|
16
|
+
# equal-tempered frequencies of a multi-chime horn.
|
|
17
|
+
#
|
|
18
|
+
# Every formula is taken from a published standard or regulation:
|
|
19
|
+
#
|
|
20
|
+
# * inverse-square (spherical) spreading: ISO 9613-2:1996, clause 7.1
|
|
21
|
+
# * atmospheric absorption: ISO 9613-1:1993, equations (3)–(5) and Annex B
|
|
22
|
+
# * speed of sound versus temperature: ISO 2533:1975
|
|
23
|
+
# * classical Doppler formula for a source and observer in a still medium
|
|
24
|
+
# * locomotive-horn sounding window: 49 CFR 222.21 (b)(2)–(b)(3) only
|
|
25
|
+
# * A4 = 440 Hz, twelve-tone equal temperament: ISO 16:1975
|
|
26
|
+
#
|
|
27
|
+
# The library does not invent spectra, empirical horn data, or fitted
|
|
28
|
+
# constants.
|
|
29
|
+
#
|
|
30
|
+
# @see https://hornguns.com/
|
|
31
|
+
# @author Horn Guns <info@hornguns.com>
|
|
32
|
+
module HornAcoustics
|
|
33
|
+
module_function
|
|
34
|
+
|
|
35
|
+
# @see AtmosphericAbsorption.atmospheric_absorption
|
|
36
|
+
def atmospheric_absorption(*args, **kwargs)
|
|
37
|
+
AtmosphericAbsorption.atmospheric_absorption(*args, **kwargs)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# @see SpeedOfSound.speed_of_sound
|
|
41
|
+
def speed_of_sound(*args, **kwargs)
|
|
42
|
+
SpeedOfSound.speed_of_sound(*args, **kwargs)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# @see DopplerShift.doppler_shift
|
|
46
|
+
def doppler_shift(*args, **kwargs)
|
|
47
|
+
DopplerShift.doppler_shift(*args, **kwargs)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# @see HornWindow.horn_window
|
|
51
|
+
def horn_window(*args, **kwargs)
|
|
52
|
+
HornWindow.horn_window(*args, **kwargs)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# @see ChordFrequencies.chord_frequencies
|
|
56
|
+
def chord_frequencies(*args, **kwargs)
|
|
57
|
+
ChordFrequencies.chord_frequencies(*args, **kwargs)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# @see SplAtDistance.spl_at_distance
|
|
61
|
+
def spl_at_distance(*args, **kwargs)
|
|
62
|
+
SplAtDistance.spl_at_distance(*args, **kwargs)
|
|
63
|
+
end
|
|
64
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: horn_acoustics
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Horn Guns
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: |
|
|
13
|
+
Calculate outdoor sound-pressure-level change with distance from the
|
|
14
|
+
free-field inverse-square law (geometrical divergence in ISO
|
|
15
|
+
9613-2:1996) and, optionally, atmospheric absorption after ISO
|
|
16
|
+
9613-1:1993. Compute the classical Doppler-shifted frequency for a
|
|
17
|
+
moving source or observer (speed of sound from air temperature after
|
|
18
|
+
ISO 2533:1975), the locomotive-horn sounding window at public
|
|
19
|
+
highway-rail grade crossings given in 49 CFR 222.21 (b)(2)-(b)(3),
|
|
20
|
+
and equal-tempered frequencies of multi-chime horn notes with A4 at
|
|
21
|
+
440 Hz (ISO 16:1975).
|
|
22
|
+
email:
|
|
23
|
+
- info@hornguns.com
|
|
24
|
+
executables: []
|
|
25
|
+
extensions: []
|
|
26
|
+
extra_rdoc_files: []
|
|
27
|
+
files:
|
|
28
|
+
- LICENSE
|
|
29
|
+
- README.md
|
|
30
|
+
- lib/horn_acoustics.rb
|
|
31
|
+
- lib/horn_acoustics/atmospheric_absorption.rb
|
|
32
|
+
- lib/horn_acoustics/chord_frequencies.rb
|
|
33
|
+
- lib/horn_acoustics/doppler_shift.rb
|
|
34
|
+
- lib/horn_acoustics/horn_window.rb
|
|
35
|
+
- lib/horn_acoustics/speed_of_sound.rb
|
|
36
|
+
- lib/horn_acoustics/spl_at_distance.rb
|
|
37
|
+
- lib/horn_acoustics/util.rb
|
|
38
|
+
- lib/horn_acoustics/version.rb
|
|
39
|
+
homepage: https://hornguns.com/
|
|
40
|
+
licenses:
|
|
41
|
+
- MIT
|
|
42
|
+
metadata:
|
|
43
|
+
homepage_uri: https://hornguns.com/
|
|
44
|
+
documentation_uri: https://www.rubydoc.info/gems/horn_acoustics
|
|
45
|
+
rdoc_options: []
|
|
46
|
+
require_paths:
|
|
47
|
+
- lib
|
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
|
+
requirements:
|
|
50
|
+
- - ">="
|
|
51
|
+
- !ruby/object:Gem::Version
|
|
52
|
+
version: '2.7'
|
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
|
+
requirements:
|
|
55
|
+
- - ">="
|
|
56
|
+
- !ruby/object:Gem::Version
|
|
57
|
+
version: '0'
|
|
58
|
+
requirements: []
|
|
59
|
+
rubygems_version: 3.6.9
|
|
60
|
+
specification_version: 4
|
|
61
|
+
summary: Acoustic calculations for train horns from published standards.
|
|
62
|
+
test_files: []
|