Add Exponential Moving Average Filter (#6075)

This commit is contained in:
Shreya
2024-11-01 14:08:27 +05:30
committed by GitHub
parent a676ebc472
commit 7b962a4a1d
2 changed files with 89 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
package com.thealgorithms.audiofilters;
/**
* Exponential Moving Average (EMA) Filter for smoothing audio signals.
*
* <p>This filter applies an exponential moving average to a sequence of audio
* signal values, making it useful for smoothing out rapid fluctuations.
* The smoothing factor (alpha) controls the degree of smoothing.
*
* <p>Based on the definition from
* <a href="https://en.wikipedia.org/wiki/Moving_average">Wikipedia link</a>.
*/
public class EMAFilter {
private final double alpha;
private double emaValue;
/**
* Constructs an EMA filter with a given smoothing factor.
*
* @param alpha Smoothing factor (0 < alpha <= 1)
* @throws IllegalArgumentException if alpha is not in (0, 1]
*/
public EMAFilter(double alpha) {
if (alpha <= 0 || alpha > 1) {
throw new IllegalArgumentException("Alpha must be between 0 and 1.");
}
this.alpha = alpha;
this.emaValue = 0.0;
}
/**
* Applies the EMA filter to an audio signal array.
*
* @param audioSignal Array of audio samples to process
* @return Array of processed (smoothed) samples
*/
public double[] apply(double[] audioSignal) {
if (audioSignal.length == 0) {
return new double[0];
}
double[] emaSignal = new double[audioSignal.length];
emaValue = audioSignal[0];
emaSignal[0] = emaValue;
for (int i = 1; i < audioSignal.length; i++) {
emaValue = alpha * audioSignal[i] + (1 - alpha) * emaValue;
emaSignal[i] = emaValue;
}
return emaSignal;
}
}

View File

@@ -0,0 +1,41 @@
package com.thealgorithms.audiofilters;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
public class EMAFilterTest {
@Test
public void testApplyBasicSignal() {
EMAFilter emaFilter = new EMAFilter(0.2);
double[] audioSignal = {0.1, 0.5, 0.8, 0.6, 0.3, 0.9, 0.4};
double[] expectedOutput = {0.1, 0.18, 0.304, 0.3632, 0.35056, 0.460448, 0.4483584};
double[] result = emaFilter.apply(audioSignal);
assertArrayEquals(expectedOutput, result, 1e-5);
}
@Test
public void testApplyEmptySignal() {
EMAFilter emaFilter = new EMAFilter(0.2);
double[] audioSignal = {};
double[] expectedOutput = {};
double[] result = emaFilter.apply(audioSignal);
assertArrayEquals(expectedOutput, result);
}
@Test
public void testAlphaBounds() {
EMAFilter emaFilterMin = new EMAFilter(0.01);
EMAFilter emaFilterMax = new EMAFilter(1.0);
double[] audioSignal = {1.0, 1.0, 1.0, 1.0};
// Minimal smoothing (alpha close to 0)
double[] resultMin = emaFilterMin.apply(audioSignal);
assertArrayEquals(audioSignal, resultMin, 1e-5);
// Maximum smoothing (alpha = 1, output should match input)
double[] resultMax = emaFilterMax.apply(audioSignal);
assertArrayEquals(audioSignal, resultMax, 1e-5);
}
}