From cd66f4f5320e79895622704c886c775c55c74e76 Mon Sep 17 00:00:00 2001 From: Shreya <95279016+Shreya123714@users.noreply.github.com> Date: Thu, 31 Oct 2024 09:48:05 +0530 Subject: [PATCH] Update ema_filter.py Corrected the build error , caused due to return of None by apply() --- audio_filters/ema_filter.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/audio_filters/ema_filter.py b/audio_filters/ema_filter.py index 8f63c5b12..d368f53ca 100644 --- a/audio_filters/ema_filter.py +++ b/audio_filters/ema_filter.py @@ -33,6 +33,8 @@ class EMAFilter: self.alpha = alpha self.ema_value = 0.0 + + def apply(self, audio_signal: list[float]) -> np.ndarray: """ Apply the EMA filter to a sequence of @@ -48,19 +50,21 @@ class EMAFilter: Example: >>> ema_filter = EMAFilter(0.2) >>> 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) True """ + if not audio_signal: + return np.array([]) + 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: - # 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 - ema_signal.append(self.ema_value) + ema_signal.append(self.ema_value) return np.array(ema_signal)