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,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);
}
}