2017-12-18 21:50:27 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
import numpy as np
|
2018-02-27 21:12:08 +01:00
|
|
|
import lib.config as config
|
|
|
|
import lib.melbank as melbank
|
2017-12-18 21:50:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ExpFilter:
|
|
|
|
"""Simple exponential smoothing filter"""
|
|
|
|
def __init__(self, val=0.0, alpha_decay=0.5, alpha_rise=0.5):
|
|
|
|
"""Small rise / decay factors = more smoothing"""
|
|
|
|
assert 0.0 < alpha_decay < 1.0, 'Invalid decay smoothing factor'
|
|
|
|
assert 0.0 < alpha_rise < 1.0, 'Invalid rise smoothing factor'
|
|
|
|
self.alpha_decay = alpha_decay
|
|
|
|
self.alpha_rise = alpha_rise
|
|
|
|
self.value = val
|
|
|
|
|
|
|
|
def update(self, value):
|
|
|
|
if isinstance(self.value, (list, np.ndarray, tuple)):
|
|
|
|
alpha = value - self.value
|
|
|
|
alpha[alpha > 0.0] = self.alpha_rise
|
|
|
|
alpha[alpha <= 0.0] = self.alpha_decay
|
|
|
|
else:
|
|
|
|
alpha = self.alpha_rise if value > self.value else self.alpha_decay
|
|
|
|
self.value = alpha * value + (1.0 - alpha) * self.value
|
|
|
|
return self.value
|
|
|
|
|