mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-19 04:07:36 +00:00
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
This commit is contained in:
parent
1f1c1b073c
commit
2e1193507b
@ -7,11 +7,12 @@ https://en.wikipedia.org/wiki/Moving_average
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
|
|
||||||
class EMAFilter:
|
class EMAFilter:
|
||||||
"""
|
"""
|
||||||
A class for applying an Exponential Moving Average (EMA) filter
|
A class for applying an Exponential Moving Average (EMA) filter
|
||||||
to audio data.
|
to audio data.
|
||||||
|
|
||||||
Attributes:
|
Attributes:
|
||||||
alpha (float): Smoothing factor where 0 < alpha <= 1.
|
alpha (float): Smoothing factor where 0 < alpha <= 1.
|
||||||
ema_value (float): Stores the most recent EMA value
|
ema_value (float): Stores the most recent EMA value
|
||||||
@ -24,7 +25,7 @@ class EMAFilter:
|
|||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
alpha (float): Smoothing factor where 0 < alpha <= 1.
|
alpha (float): Smoothing factor where 0 < alpha <= 1.
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
ValueError: If alpha is not within the range (0, 1].
|
ValueError: If alpha is not within the range (0, 1].
|
||||||
"""
|
"""
|
||||||
@ -47,7 +48,7 @@ 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])
|
||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
@ -62,6 +63,8 @@ class EMAFilter:
|
|||||||
ema_signal.append(self.ema_value)
|
ema_signal.append(self.ema_value)
|
||||||
return np.array(ema_signal)
|
return np.array(ema_signal)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user