|
1 | 1 | package com.thealgorithms.audiofilters;
|
2 | 2 |
|
3 |
| -import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
4 |
| -import org.junit.jupiter.api.Test; |
| 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 { |
5 | 16 |
|
6 |
| -public class EMAFilterTest { |
| 17 | + private final double alpha; |
| 18 | + private double emaValue; |
7 | 19 |
|
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 |
| - double[] result = emaFilter.apply(audioSignal); |
14 |
| - assertArrayEquals(expectedOutput, result, 1e-5); |
| 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; |
15 | 32 | }
|
16 |
| - @Test |
17 |
| - public void testApplyEmptySignal() { |
18 |
| - EMAFilter emaFilter = new EMAFilter(0.2); |
19 |
| - double[] audioSignal = {}; |
20 |
| - double[] expectedOutput = {}; |
21 |
| - double[] result = emaFilter.apply(audioSignal); |
22 |
| - assertArrayEquals(expectedOutput, result); |
23 |
| - } |
24 |
| - @Test |
25 |
| - public void testAlphaBounds() { |
26 |
| - EMAFilter emaFilterMin = new EMAFilter(0.01); |
27 |
| - EMAFilter emaFilterMax = new EMAFilter(1.0); |
28 |
| - double[] audioSignal = {1.0, 1.0, 1.0, 1.0}; |
29 |
| - // Minimal smoothing (alpha close to 0) |
30 |
| - double[] resultMin = emaFilterMin.apply(audioSignal); |
31 |
| - assertArrayEquals(audioSignal, resultMin, 1e-5); |
32 |
| - // Maximum smoothing (alpha = 1, output should match input) |
33 |
| - double[] resultMax = emaFilterMax.apply(audioSignal); |
34 |
| - assertArrayEquals(audioSignal, resultMax, 1e-5); |
| 33 | + |
| 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; |
35 | 55 | }
|
36 | 56 | }
|
0 commit comments