File tree Expand file tree Collapse file tree
src/main/java/com/thealgorithms/audiofilters Expand file tree Collapse file tree Original file line number Diff line number Diff line change 33/**
44 * Exponential Moving Average (EMA) Filter for smoothing audio signals.
55 *
6- * <p>This filter applies an exponential moving average to a sequence of audio
6+ * <p>
7+ * This filter applies an exponential moving average to a sequence of audio
78 * signal values, making it useful for smoothing out rapid fluctuations.
89 * The smoothing factor (alpha) controls the degree of smoothing.
910 *
10- * <p>Based on the definition from
11+ * <p>
12+ * Based on the definition from
1113 * <a href="https://en.wikipedia.org/wiki/Moving_average">Wikipedia link</a>.
1214 */
1315public class EMAFilter {
1416 private final double alpha ;
1517 private double emaValue ;
18+
1619 /**
1720 * Constructs an EMA filter with a given smoothing factor.
1821 *
@@ -26,14 +29,17 @@ public EMAFilter(double alpha) {
2629 this .alpha = alpha ;
2730 this .emaValue = 0.0 ;
2831 }
32+
2933 /**
3034 * Applies the EMA filter to an audio signal array.
35+ * EMA formula:
36+ * EMA = alpha * currentSample + (1 - alpha) * previousEMA
3137 *
3238 * @param audioSignal Array of audio samples to process
3339 * @return Array of processed (smoothed) samples
3440 */
3541 public double [] apply (double [] audioSignal ) {
36- if (audioSignal .length == 0 ) {
42+ if (audioSignal == null || audioSignal .length == 0 ) {
3743 return new double [0 ];
3844 }
3945 double [] emaSignal = new double [audioSignal .length ];
You can’t perform that action at this time.
0 commit comments