-
Notifications
You must be signed in to change notification settings - Fork 20.4k
Enhance EMAFilter for Audio Signals: Optimized, Memory-Efficient, and Stream-Friendly #6658
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+97
−27
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Manas-Dikshit
Manas-Dikshit
requested review from
DenizAltunkapan,
yanglbme and
alxkm
as code owners
October 5, 2025 18:03
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR enhances the EMAFilter, a tool used to smooth audio signals using an Exponential Moving Average (EMA). The main goal of these changes is to make the filter faster, more memory-efficient, and enterprise-ready, all while keeping its original functionality intact.
Smarter Memory Usage
We added a new method, applyInPlace(double[] samples), which allows the filter to process audio samples directly in the original array. This means we don’t need to create a new array for every batch of audio data, saving memory—especially helpful when dealing with large audio files.
Faster and More Efficient Processing
The filter loop has been optimized to reduce unnecessary calculations. Additionally, we introduced the next(double sample) method, which lets you process streaming data in real-time. This makes the filter suitable not just for offline processing, but also for live audio applications.
Enterprise-Ready Improvements
The class is now final and the smoothing factor (alpha) is immutable, ensuring safe usage across multiple threads or systems.
Robust input validation has been added to prevent errors when passing null or invalid values.
Clear and detailed Javadocs explain exactly how each method works, making the code easier to maintain.
A toString() method has been included for easy debugging and monitoring of the current EMA state.
Better Usability
apply(double[] samples) returns a new array with smoothed values, leaving the original untouched, giving developers flexibility depending on their needs.
next(double sample) is perfect for real-time, sample-by-sample processing.
getLastEma() lets you quickly access the most recent EMA value without recalculating everything.
Clear Initialization Handling
The filter uses Double.NaN to represent the initial state of lastEma, making it obvious when the filter hasn’t processed any data yet.
Why this matters:
Audio processing often involves large datasets or real-time streams, so efficiency, memory savings, and usability are critical. With these improvements, the EMAFilter is now better suited for modern Java applications, whether you’re handling offline batches or live audio streams.