From 94a928200e4e1125c5c5a3e7bb40242d978804ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20J=C3=B6rdens?= Date: Tue, 16 Apr 2019 11:27:11 +0000 Subject: [PATCH] iir: tweak --- src/iir.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/iir.rs b/src/iir.rs index d3a60e8..2a59272 100644 --- a/src/iir.rs +++ b/src/iir.rs @@ -1,3 +1,6 @@ +use core::ops::{Add, Mul}; +use core::iter::Sum; + use core::f32; pub type IIRState = [f32; 5]; @@ -22,8 +25,10 @@ fn copysign(x: f32, y: f32) -> f32 { } } -fn macc(y0: f32, x: &[f32], a: &[f32]) -> f32 { - y0 + x.iter().zip(a.iter()).map(|(&i, &j)| i * j).sum::() +fn macc(y0: T, x: &[T], a: &[T]) -> T + where T: Sum + Add + Mul + Copy +{ + y0 + x.iter().zip(a.iter()).map(|(&i, &j)| i * j).sum() } impl IIR { @@ -55,9 +60,10 @@ impl IIR { pub fn get_x_offset(&self) -> Result { let b: f32 = self.ba[..3].iter().sum(); if abs(b) < f32::EPSILON { - return Err("b is zero") + Err("b is zero") + } else { + Ok(self.y_offset/b) } - Ok(self.y_offset/b) } pub fn set_x_offset(&mut self, xo: f32) {