Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/main/java/com/thealgorithms/audiofilters/EMAFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
/**
* Exponential Moving Average (EMA) Filter for smoothing audio signals.
*
* <p>This filter applies an exponential moving average to a sequence of audio
* <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
* <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.
*
Expand All @@ -26,14 +29,17 @@ public EMAFilter(double alpha) {
this.alpha = alpha;
this.emaValue = 0.0;
}

/**
* Applies the EMA filter to an audio signal array.
* EMA formula:
* EMA = alpha * currentSample + (1 - alpha) * previousEMA
*
* @param audioSignal Array of audio samples to process
* @return Array of processed (smoothed) samples
*/
public double[] apply(double[] audioSignal) {
if (audioSignal.length == 0) {
if (audioSignal == null || audioSignal.length == 0) {
return new double[0];
}
double[] emaSignal = new double[audioSignal.length];
Expand Down
Loading