mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-07 14:25:54 +00:00
Update ema_filter.py
Corrected the build error , caused due to return of None by apply()
This commit is contained in:
parent
235d527b9f
commit
cd66f4f532
@ -33,6 +33,8 @@ class EMAFilter:
|
|||||||
self.alpha = alpha
|
self.alpha = alpha
|
||||||
self.ema_value = 0.0
|
self.ema_value = 0.0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def apply(self, audio_signal: list[float]) -> np.ndarray:
|
def apply(self, audio_signal: list[float]) -> np.ndarray:
|
||||||
"""
|
"""
|
||||||
Apply the EMA filter to a sequence of
|
Apply the EMA filter to a sequence of
|
||||||
@ -48,19 +50,21 @@ class EMAFilter:
|
|||||||
Example:
|
Example:
|
||||||
>>> ema_filter = EMAFilter(0.2)
|
>>> ema_filter = EMAFilter(0.2)
|
||||||
>>> np.allclose(ema_filter.apply([0.1, 0.5, 0.8, 0.6, 0.3, 0.9, 0.4]),
|
>>> np.allclose(ema_filter.apply([0.1, 0.5, 0.8, 0.6, 0.3, 0.9, 0.4]),
|
||||||
... [0.1, 0.18, 0.304, 0.3632, 0.35056, 0.460448, 0.4483584]
|
... [0.1, 0.18, 0.304, 0.3632, 0.35056, 0.460448, 0.4483584],
|
||||||
... rtol=1e-5, atol=1e-8)
|
... rtol=1e-5, atol=1e-8)
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
|
if not audio_signal:
|
||||||
|
return np.array([])
|
||||||
|
|
||||||
ema_signal: list[float] = []
|
ema_signal: list[float] = []
|
||||||
for sample in audio_signal:
|
self.ema_value = audio_signal[0]
|
||||||
|
ema_signal.append(self.ema_value)
|
||||||
|
|
||||||
|
for sample in audio_signal[1:]:
|
||||||
if self.ema_value is None:
|
if self.ema_value is None:
|
||||||
# Initialize the EMA with the first sample
|
|
||||||
self.ema_value = sample
|
|
||||||
else:
|
|
||||||
# Calculate the EMA for the current sample
|
|
||||||
self.ema_value = self.alpha * sample + (1 - self.alpha) * self.ema_value
|
self.ema_value = self.alpha * sample + (1 - self.alpha) * self.ema_value
|
||||||
ema_signal.append(self.ema_value)
|
ema_signal.append(self.ema_value)
|
||||||
return np.array(ema_signal)
|
return np.array(ema_signal)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user