Skip to content

Commit

Permalink
deviation: Implement peak_signal_to_noise_ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
munckymagik committed May 6, 2019
1 parent 98c4928 commit a037fc4
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/deviation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ where
fn root_mean_sq_dev(&self, other: &ArrayBase<S, D>) -> A
where
A: AddAssign + Clone + FromPrimitive + Signed + Float;

fn peak_signal_to_noise_ratio(&self, other: &ArrayBase<S, D>, maxv: A) -> A
where
A: AddAssign + Clone + FromPrimitive + Signed + Float;
}

impl<A, S, D> DeviationExt<A, S, D> for ArrayBase<S, D>
Expand Down Expand Up @@ -176,6 +180,14 @@ where
{
self.mean_sq_dev(other).sqrt()
}

fn peak_signal_to_noise_ratio(&self, other: &ArrayBase<S, D>, maxv: A) -> A
where
A: AddAssign + Clone + FromPrimitive + Signed + Float,
{
let ten = A::from(10.).unwrap();
ten * Float::log10((maxv * maxv) / self.mean_sq_dev(&other))
}
}

#[cfg(test)]
Expand Down Expand Up @@ -327,4 +339,19 @@ mod tests {
assert_abs_diff_eq!(a.root_mean_sq_dev(&b), 10.0.sqrt(), epsilon = f64::EPSILON);
assert_abs_diff_eq!(b.root_mean_sq_dev(&a), 10.0.sqrt(), epsilon = f64::EPSILON);
}

#[test]
fn test_peak_signal_to_noise_ratio() {
let a = array![1., 1.];
assert!(a.peak_signal_to_noise_ratio(&a, 1.).is_infinite());

let a = array![1., 2., 3., 4., 5., 6., 7.];
let b = array![1., 3., 3., 4., 6., 7., 8.];
let maxv = 2.;

let expected = maxv * 10. * Float::log10(maxv) - 10. * Float::log10(a.mean_sq_dev(&b));
let actual = a.peak_signal_to_noise_ratio(&b, maxv);

assert_abs_diff_eq!(actual, expected, epsilon = f64::EPSILON * 8.);
}
}

0 comments on commit a037fc4

Please sign in to comment.