Skip to content

Commit 7fed2d2

Browse files
authored
Merge branch 'master' into add-wavelet-tree
2 parents a497bfe + e814d97 commit 7fed2d2

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

src/main/java/com/thealgorithms/audiofilters/EMAFilter.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,19 @@
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
*/
1315
public 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];

0 commit comments

Comments
 (0)