1
0
Fork 0

Get rid of unnecessary loop in ExponentialIIR

This commit is contained in:
Robbert van der Helm 2022-05-12 01:56:07 +02:00
parent 116d245149
commit 222036b335

View file

@ -274,16 +274,10 @@ impl Smoother<f32> {
SmoothingStyle::Linear(_) => current + (self.step_size * steps as f32), SmoothingStyle::Linear(_) => current + (self.step_size * steps as f32),
SmoothingStyle::Logarithmic(_) => current * (self.step_size.powi(steps as i32)), SmoothingStyle::Logarithmic(_) => current * (self.step_size.powi(steps as i32)),
SmoothingStyle::ExponentialIIR(_) => { SmoothingStyle::ExponentialIIR(_) => {
// TODO: Is there a way to avoid the loop here? // This is the same as calculating `current = (current * step_size) +
let mut current = current; // (target * (1 - step_size))` in a loop
// TODO: We could store this `1.0 - self.step_size` on the struct, but until let coefficient = self.step_size.powi(steps as i32);
// a profiler tells me that's needed this is probably fine (current * coefficient) + (self.target * (1.0 - coefficient))
let target_step_size = 1.0 - self.step_size;
for _ in 0..steps {
current = current * self.step_size + (self.target * target_step_size)
}
current
} }
} }
}; };
@ -404,14 +398,10 @@ impl Smoother<i32> {
SmoothingStyle::Linear(_) => current + (self.step_size * steps as f32), SmoothingStyle::Linear(_) => current + (self.step_size * steps as f32),
SmoothingStyle::Logarithmic(_) => current * self.step_size.powi(steps as i32), SmoothingStyle::Logarithmic(_) => current * self.step_size.powi(steps as i32),
SmoothingStyle::ExponentialIIR(_) => { SmoothingStyle::ExponentialIIR(_) => {
let target_step_size = 1.0 - self.step_size; // This is the same as calculating `current = (current * step_size) +
let target = self.target as f32; // (target * (1 - step_size))` in a loop
let mut current = current; let coefficient = self.step_size.powi(steps as i32);
for _ in 0..steps { (current * coefficient) + (self.target as f32 * (1.0 - coefficient))
current = current * self.step_size + (target * target_step_size)
}
current
} }
} }
}; };