Skip to content

Added Exponential Moving Average Filter for audio signals , EMAFilter.java & EMAFilterTest.java #6075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Nov 1, 2024
Merged
48 changes: 48 additions & 0 deletions src/main/java/com/thealgorithms/audiofilters/EMAFilter.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
41 changes: 41 additions & 0 deletions src/test/java/com/thealgorithms/audiofilters/EMAFilterTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}