1pub const REFERENCE_SOUND_PRESSURE_AIR_PA: f64 = 20e-6;
3
4pub const REFERENCE_SOUND_INTENSITY_W_PER_M2: f64 = 1e-12;
6
7#[must_use]
9pub fn amplitude_ratio_to_db(ratio: f64) -> Option<f64> {
10 if !is_valid_positive_scalar(ratio) {
11 return None;
12 }
13
14 Some(20.0 * ratio.log10())
15}
16
17#[must_use]
19pub fn power_ratio_to_db(ratio: f64) -> Option<f64> {
20 if !is_valid_positive_scalar(ratio) {
21 return None;
22 }
23
24 Some(10.0 * ratio.log10())
25}
26
27#[must_use]
29pub fn db_to_amplitude_ratio(db: f64) -> Option<f64> {
30 if !db.is_finite() {
31 return None;
32 }
33
34 Some(10.0_f64.powf(db / 20.0))
35}
36
37#[must_use]
39pub fn db_to_power_ratio(db: f64) -> Option<f64> {
40 if !db.is_finite() {
41 return None;
42 }
43
44 Some(10.0_f64.powf(db / 10.0))
45}
46
47#[must_use]
49pub fn sound_pressure_level_db(pressure_pa: f64) -> Option<f64> {
50 if !is_valid_positive_scalar(pressure_pa) {
51 return None;
52 }
53
54 Some(20.0 * (pressure_pa.log10() - REFERENCE_SOUND_PRESSURE_AIR_PA.log10()))
55}
56
57#[must_use]
59pub fn sound_intensity_level_db(intensity_w_per_m2: f64) -> Option<f64> {
60 if !is_valid_positive_scalar(intensity_w_per_m2) {
61 return None;
62 }
63
64 Some(10.0 * (intensity_w_per_m2.log10() - REFERENCE_SOUND_INTENSITY_W_PER_M2.log10()))
65}
66
67fn is_valid_positive_scalar(value: f64) -> bool {
68 value.is_finite() && value > 0.0
69}