Skip to content

feat: implement mlx.core.blackman#3136

Merged
angeloskath merged 1 commit intoml-explore:mainfrom
Vlor999:feat/add-blackman-window
Feb 25, 2026
Merged

feat: implement mlx.core.blackman#3136
angeloskath merged 1 commit intoml-explore:mainfrom
Vlor999:feat/add-blackman-window

Conversation

@Vlor999
Copy link
Copy Markdown
Contributor

@Vlor999 Vlor999 commented Feb 16, 2026

Description

This PR implements the Blackman window function (mlx.core.blackman), completing the standard trio of window functions (Hanning, Hamming, Blackman) and improving feature parity with NumPy.

Implementation Details

  • C++: Implemented in mlx/ops.cpp.
    • Optimization: Instead of computing two separate cosine terms ($\cos(x)$ and $\cos(2x)$ ), I utilized the double-angle identity: $\cos(2x) = 2\cos^2(x) - 1$.
    • Performance: This reduces the computational cost to a single transcendental cos operation followed by cheap polynomial arithmetic ($0.34 - 0.5\cos(x) + 0.16\cos^2(x)$ ).
    • Edge Cases: Explicitly handles M < 1 (empty) and M = 1 (returns [1.0]), matching NumPy's behavior exactly.
  • Python Bindings: Exposed via nanobind with nb::sig for proper type hinting and full LaTeX documentation.

Test Plan

Verified locally against NumPy for numerical accuracy and edge cases.

Verification script:

import mlx.core as mx
import numpy as np

# 1. Standard Case
M = 100
mx_blackman = mx.blackman(M)
np_blackman = np.blackman(M)

assert np.allclose(mx_blackman, np_blackman, atol=1e-6), "Mismatch in values"

# 2. Edge Case M=1
# Verified that both return [1.0]
assert mx.blackman(1).item() == 1.0
assert np.blackman(1).item() == 1.0

# 3. Edge Case M=0
assert mx.blackman(0).size == 0

print("✅ All verifications passed against numpy.blackman")

Unit Tests:

Added test_blackman in python/tests/test_ops.py.

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
Copy Markdown
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 as well.

Feel free to fix the conflicts and then I can run the tests and merge.

@Vlor999 Vlor999 force-pushed the feat/add-blackman-window branch from 4279cea to dd96ab7 Compare February 17, 2026 18:23
@Vlor999 Vlor999 requested a review from angeloskath February 17, 2026 18:25
@Vlor999
Copy link
Copy Markdown
Contributor Author

Vlor999 commented Feb 17, 2026

Thanks
I've corrected all the issues.

@Vlor999 Vlor999 force-pushed the feat/add-blackman-window branch from dd96ab7 to f24b4c4 Compare February 21, 2026 11:10
@Vlor999
Copy link
Copy Markdown
Contributor Author

Vlor999 commented Feb 21, 2026

Hi team! 👋
Just a gentle ping on this PR. Let me know if there's anything else you need from my side or if you'd like any changes to the implementation! 🚀

@Vlor999 Vlor999 force-pushed the feat/add-blackman-window branch from f24b4c4 to 0f57dbd Compare February 24, 2026 20:26
@angeloskath
Copy link
Copy Markdown
Member

Hi @Vlor999, sorry for the delay.

Out of curiosity is there a specific project that needs these window functions? They are becoming a bit complicated and I am wondering if they are worth it. Looking for instance at the i0 function and the Kaiser window 🤔

@Vlor999
Copy link
Copy Markdown
Contributor Author

Vlor999 commented Feb 25, 2026

Hi @Vlor999, sorry for the delay.

Out of curiosity is there a specific project that needs these window functions? They are becoming a bit complicated and I am wondering if they are worth it. Looking for instance at the i0 function and the Kaiser window 🤔

I'm currently building a fully GPU-accelerated STFT (Short-Time Fourier Transform) pipeline MLX.

The goal is to provide a fast, autograd-compatible alternative to CPU-bound libraries like librosa or scipy. By keeping the framing, windowing, and mx.fft operations entirely within the MLX graph, this will enable highly parallelized audio preprocessing directly integrated into neural network training loops.

Copy link
Copy Markdown
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.

👍

@angeloskath angeloskath merged commit a8ba5ac into ml-explore:main Feb 25, 2026
16 checks passed
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