Skip to content

Commit ce69871

Browse files
committed
deviation: Implement peak_signal_to_noise_ratio
1 parent 98c4928 commit ce69871

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/deviation.rs

+26
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ where
5252
fn root_mean_sq_dev(&self, other: &ArrayBase<S, D>) -> A
5353
where
5454
A: AddAssign + Clone + FromPrimitive + Signed + Float;
55+
56+
fn peak_signal_to_noise_ratio(&self, other: &ArrayBase<S, D>, maxv: A) -> A
57+
where
58+
A: AddAssign + Clone + FromPrimitive + Signed + Float;
5559
}
5660

5761
impl<A, S, D> DeviationExt<A, S, D> for ArrayBase<S, D>
@@ -176,6 +180,14 @@ where
176180
{
177181
self.mean_sq_dev(other).sqrt()
178182
}
183+
184+
fn peak_signal_to_noise_ratio(&self, other: &ArrayBase<S, D>, maxv: A) -> A
185+
where
186+
A: AddAssign + Clone + FromPrimitive + Signed + Float,
187+
{
188+
let ten = A::from(10.).unwrap();
189+
ten * Float::log10(maxv * maxv / self.mean_sq_dev(&other))
190+
}
179191
}
180192

181193
#[cfg(test)]
@@ -327,4 +339,18 @@ mod tests {
327339
assert_abs_diff_eq!(a.root_mean_sq_dev(&b), 10.0.sqrt(), epsilon = f64::EPSILON);
328340
assert_abs_diff_eq!(b.root_mean_sq_dev(&a), 10.0.sqrt(), epsilon = f64::EPSILON);
329341
}
342+
343+
#[test]
344+
fn test_peak_signal_to_noise_ratio() {
345+
let a = array![1., 1.];
346+
assert!(a.peak_signal_to_noise_ratio(&a, 1.).is_infinite());
347+
348+
let a = array![1., 2., 3., 4., 5., 6., 7.];
349+
let b = array![1., 3., 3., 4., 6., 7., 8.];
350+
let maxv = 8.;
351+
let expected = 20. * Float::log10(maxv) - 10. * Float::log10(a.mean_sq_dev(&b));
352+
let actual = a.peak_signal_to_noise_ratio(&b, maxv);
353+
354+
assert_abs_diff_eq!(actual, expected, epsilon = f64::EPSILON);
355+
}
330356
}

0 commit comments

Comments
 (0)