|
52 | 52 | fn root_mean_sq_dev(&self, other: &ArrayBase<S, D>) -> A
|
53 | 53 | where
|
54 | 54 | 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; |
55 | 59 | }
|
56 | 60 |
|
57 | 61 | impl<A, S, D> DeviationExt<A, S, D> for ArrayBase<S, D>
|
@@ -176,6 +180,14 @@ where
|
176 | 180 | {
|
177 | 181 | self.mean_sq_dev(other).sqrt()
|
178 | 182 | }
|
| 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 | + } |
179 | 191 | }
|
180 | 192 |
|
181 | 193 | #[cfg(test)]
|
@@ -327,4 +339,18 @@ mod tests {
|
327 | 339 | assert_abs_diff_eq!(a.root_mean_sq_dev(&b), 10.0.sqrt(), epsilon = f64::EPSILON);
|
328 | 340 | assert_abs_diff_eq!(b.root_mean_sq_dev(&a), 10.0.sqrt(), epsilon = f64::EPSILON);
|
329 | 341 | }
|
| 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 | + } |
330 | 356 | }
|
0 commit comments