Skip to content

Commit faff766

Browse files
authored
Update EMAFilterTest.java
1 parent d3def9e commit faff766

File tree

1 file changed

+39
-50
lines changed

1 file changed

+39
-50
lines changed
Lines changed: 39 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,45 @@
11
package com.thealgorithms.audiofilters;
22

3-
/**
4-
* Exponential Moving Average (EMA) Filter for smoothing audio signals.
5-
*
6-
* <p>
7-
* This filter applies an exponential moving average to a sequence of audio
8-
* signal values, making it useful for smoothing out rapid fluctuations.
9-
* The smoothing factor (alpha) controls the degree of smoothing.
10-
*
11-
* <p>
12-
* Based on the definition from
13-
* <a href="https://en.wikipedia.org/wiki/Moving_average">Wikipedia link</a>.
14-
*/
15-
public class EMAFilter {
16-
17-
private final double alpha;
18-
private double emaValue;
19-
20-
/**
21-
* Constructs an EMA filter with a given smoothing factor.
22-
*
23-
* @param alpha Smoothing factor (0 < alpha <= 1)
24-
* @throws IllegalArgumentException if alpha is not in (0, 1]
25-
*/
26-
public EMAFilter(double alpha) {
27-
if (alpha <= 0 || alpha > 1) {
28-
throw new IllegalArgumentException("Alpha must be between 0 and 1.");
29-
}
30-
this.alpha = alpha;
31-
this.emaValue = 0.0;
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import org.junit.jupiter.api.Test;
5+
6+
public class EMAFilterTest {
7+
8+
@Test
9+
public void testApplyBasicSignal() {
10+
EMAFilter emaFilter = new EMAFilter(0.2);
11+
double[] audioSignal = { 0.1, 0.5, 0.8, 0.6, 0.3, 0.9, 0.4 };
12+
double[] expectedOutput = { 0.1, 0.18, 0.304, 0.3632, 0.35056, 0.460448, 0.4483584 };
13+
14+
double[] result = emaFilter.apply(audioSignal);
15+
16+
assertArrayEquals(expectedOutput, result, 1e-5);
17+
}
18+
19+
@Test
20+
public void testApplyEmptySignal() {
21+
EMAFilter emaFilter = new EMAFilter(0.2);
22+
double[] audioSignal = {};
23+
double[] expectedOutput = {};
24+
25+
double[] result = emaFilter.apply(audioSignal);
26+
27+
assertArrayEquals(expectedOutput, result);
3228
}
3329

34-
/**
35-
* Applies the EMA filter to an audio signal array.
36-
*
37-
* @param audioSignal Array of audio samples to process
38-
* @return Array of processed (smoothed) samples
39-
*/
40-
public double[] apply(double[] audioSignal) {
41-
if (audioSignal.length == 0) {
42-
return new double[0];
43-
}
44-
45-
double[] emaSignal = new double[audioSignal.length];
46-
emaValue = audioSignal[0];
47-
emaSignal[0] = emaValue;
48-
49-
for (int i = 1; i < audioSignal.length; i++) {
50-
emaValue = alpha * audioSignal[i] + (1 - alpha) * emaValue;
51-
emaSignal[i] = emaValue;
52-
}
53-
54-
return emaSignal;
30+
@Test
31+
public void testAlphaBounds() {
32+
EMAFilter emaFilterMin = new EMAFilter(0.01);
33+
EMAFilter emaFilterMax = new EMAFilter(1.0);
34+
35+
double[] audioSignal = { 1.0, 1.0, 1.0, 1.0 };
36+
37+
// Minimal smoothing (alpha close to 0)
38+
double[] resultMin = emaFilterMin.apply(audioSignal);
39+
40+
// Maximum smoothing (alpha = 1, output should match input)
41+
double[] resultMax = emaFilterMax.apply(audioSignal);
42+
43+
assertArrayEquals(audioSignal, resultMax, 1e-5);
5544
}
5645
}

0 commit comments

Comments
 (0)