Skip to content

[featuring] - add hanning window function#3124

Merged
angeloskath merged 1 commit intoml-explore:mainfrom
Vlor999:feat/hanning_function
Feb 16, 2026
Merged

[featuring] - add hanning window function#3124
angeloskath merged 1 commit intoml-explore:mainfrom
Vlor999:feat/hanning_function

Conversation

@Vlor999
Copy link
Contributor

@Vlor999 Vlor999 commented Feb 13, 2026

Description

This PR implements the Hanning window function (mlx.core.hanning), bringing MLX closer to feature parity with NumPy's signal processing capabilities (numpy.hanning).

Window functions are essential for audio processing and various signal analysis tasks.

Implementation Details

  • C++: Implemented hanning in mlx/ops.cpp using existing primitives (arange, cos, multiply, subtract). Used M_PI from <cmath> for precision.
  • Python Bindings: Exposed the function via pybind11 in python/src/ops.cpp under mlx.core.
  • Documentation: Added docstrings describing the parameters and return values, matching the NumPy behavior.

Test Plan

I have verified the implementation locally against NumPy to ensure numerical accuracy.

Verification script:

import mlx.core as mx
import numpy as np

M = 50
mx_hanning = mx.hanning(M)
np_hanning = np.hanning(M)

# Check for numerical closeness
assert np.allclose(mx_hanning, np_hanning, atol=1e-6), "Mismatch with NumPy implementation"
print("✅ Verification passed against numpy.hanning")

Unit Tests:

Added a test case in python/tests/test_ops.py covering standard usage, edge cases (M=1), and empty input (M=0).

Checklist

Put an x in the boxes that apply.

  • I have read the CONTRIBUTING document
  • I have run pre-commit run --all-files to format my code / installed pre-commit prior to committing changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have updated the necessary documentation (if needed)

Copy link
Member

@angeloskath angeloskath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I left some comments after these we can merge it.

@Vlor999 Vlor999 force-pushed the feat/hanning_function branch 2 times, most recently from b42e5ec to 37e7a3c Compare February 15, 2026 15:20
@Vlor999 Vlor999 requested a review from angeloskath February 15, 2026 15:21
@Vlor999 Vlor999 force-pushed the feat/hanning_function branch from 37e7a3c to 00f7403 Compare February 15, 2026 15:23
@Vlor999
Copy link
Contributor Author

Vlor999 commented Feb 15, 2026

Thanks for the feedback!

I've updated the implementation as discussed:

  • Optimization: Switched to the $\sin^2$ identity (square(sin(...))). This reduces the compute graph size and memory usage by avoiding the intermediate arrays needed for the cosine formula.
  • Verification: Verified that tests still pass and numerical results remain consistent with NumPy.

Ready for another look! 🚀

Copy link
Member

@angeloskath angeloskath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good thanks!

@angeloskath angeloskath merged commit 3bbe87e into ml-explore:main Feb 16, 2026
16 checks passed
@Vlor999 Vlor999 deleted the feat/hanning_function branch February 16, 2026 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments