Estimate sound pressure level (SPL) at a distance in a Yii 2 application. Give it a level known at a reference distance (for example "150 dB at 10 ft") and it returns the free-field estimate at any other distance, using the inverse-square law with an optional air-absorption term.
It is a PHP port of the calculator behind the Train Horn decibel-at-distance tool, with the same formulas and test vectors. Maintained by Train Horn, an independent reference on train horns.
Source code: github.com/train-horn/yii2-sound-level
What is included ¶
SoundLevel: an application component. Level at distance, the inverse (distance for a target level), level addition for several sources, distance-ratio loss.SoundLevelTable: a widget that renders an accessible HTML table (caption,scope="col"andscope="row"headers) for a list of distances.sound-level: a console command, registered automatically, e.g.yii sound-level/table 150 10.
Requirements ¶
PHP 7.4 or later and Yii 2.0.45 or later. Tested on PHP 8.5 with Yii 2.0.55 (40 PHPUnit tests).
Installation ¶
composer require train-horn/yii2-sound-level
Usage ¶
Configure the component:
'components' => [
'soundLevel' => [
'class' => \trainhorn\soundlevel\SoundLevel::class,
'refDistance' => 10, // reference distance
'unit' => 'ft', // 'ft' or 'm'
'absorptionDbPerKm' => 0, // 0 = pure inverse-square law
],
],
$spl = Yii::$app->soundLevel;
$spl->splAtDistance(150, 100); // 130.0 (150 dB at 10 ft, heard at 100 ft)
$spl->splAtDistance(120, 10, 1, 'm'); // 100.0
$spl->splAtDistance(150, 1010, 10, 'm', 2); // 107.91 with 2 dB/km air absorption
$spl->distanceForSpl(150, 90); // 10000.0 ft
$spl->combineLevels([100, 100]); // 103.01 (two equal sources add about 3 dB)
Render a table in a view:
use trainhorn\soundlevel\widgets\SoundLevelTable;
echo SoundLevelTable::widget([
'sourceDb' => 150,
'refDistance' => 10,
'unit' => 'ft',
'distances' => [10, 50, 100, 500, 1000],
]);
From the console:
./yii sound-level/table 150 10
./yii sound-level/table 120 1 --unit=m --distances=1,10,100,1000 --absorption=1.9
./yii sound-level/at 150 100 # 130.0 dB
./yii sound-level/distance 150 90 # 10000 ft
./yii sound-level/combine 100,100 # 103.0 dB
Invalid input (non-numeric values, NaN, distance of 0 or less, negative absorption, unknown unit) throws trainhorn\soundlevel\InvalidInputException, a subclass of yii\base\InvalidArgumentException. The console command prints the error and exits with code 65.
The model ¶
L(d) = L_ref - 20 * log10(d / d_ref) - alpha * (d - d_ref) / 1000
Each doubling of distance costs about 6.02 dB. SoundLevel::AIR_ABSORPTION_DB_PER_KM holds rounded typical absorption values by octave band (10 degC, 70 % RH, after ISO 9613-1/9613-2).
Limits: free field only. Ground reflection, barriers, wind and temperature gradients are ignored. Results are estimates, not measurements, and not a legal noise assessment.
License ¶
MIT
User Contributed Notes
Leave a comment
If you have any questions, please ask in the forum instead.
Join the conversation to share a note.