LedFx/ledfx/effects/math.py

50 lines
1.3 KiB
Python

from functools import lru_cache
import numpy as np
@lru_cache(maxsize=32)
def _normalized_linspace(size):
return np.linspace(0, 1, size)
def interpolate(y, new_length):
"""Resizes the array by linearly interpolating the values"""
if len(y) == new_length:
return y
x_old = _normalized_linspace(len(y))
x_new = _normalized_linspace(new_length)
z = np.interp(x_new, x_old, y)
return z
class ExpFilter:
"""Simple exponential smoothing filter"""
def __init__(self, val=None, alpha_decay=0.5, alpha_rise=0.5):
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):
# Handle deferred initilization
if self.value is None:
self.value = value
return 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