Skip to content

feat: implement mlx.core.blackman#3136

Open
Vlor999 wants to merge 1 commit intoml-explore:mainfrom
Vlor999:feat/add-blackman-window
Open

feat: implement mlx.core.blackman#3136
Vlor999 wants to merge 1 commit intoml-explore:mainfrom
Vlor999:feat/add-blackman-window

Conversation

@Vlor999
Copy link
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)

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.

1 participant