Skip to content

Commit c3289c4

Browse files
committed
added dbfs
1 parent 03fce83 commit c3289c4

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/spectrum.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,24 @@ pub fn max_freq(data: &[Complex32], sample_rate: usize) -> f32 {
2323
}
2424
}
2525

26+
/// Calculate Decibels relative to the full scale
27+
pub fn dbfs(sample: &Complex32, max_power: f32) -> f32 {
28+
20.0 * (sample.norm() / max_power).log10()
29+
}
30+
2631

2732
/// ------------------------------------------------------------------------------------------------
2833
/// Module unit tests
2934
/// ------------------------------------------------------------------------------------------------
3035
#[cfg(test)]
3136
mod tests {
37+
use rustfft::num_traits::One;
38+
39+
use super::*;
40+
41+
#[test]
42+
fn test_dbfs() {
43+
assert_eq!(dbfs(&Complex32::one(), 1.), 0.);
44+
assert_eq!(dbfs(&Complex32::new(10., 0.), 100.), -20.);
45+
}
3246
}

0 commit comments

Comments
 (0)