Skip to content

feat: adding the hamming function - #3135

Merged
angeloskath merged 1 commit into
ml-explore:mainfrom
Vlor999:feat/add-hamming-window
Feb 17, 2026
Merged

angeloskath merged 1 commit into
ml-explore:mainfrom
Vlor999:feat/add-hamming-window

Conversation

@Vlor999

@Vlor999 Vlor999 commented Feb 16, 2026

Copy link
Copy Markdown
Contributor

Description

Following the addition of hanning, this PR implements the Hamming window function (mlx.core.hamming). This further improves parity with NumPy's signal processing tools (numpy.hamming).

Implementation Details

  • C++: Implemented hamming in mlx/ops.cpp.
    • Optimization: The angular factor 2π / (M-1) is pre-calculated as a scalar on the CPU before graph construction. This reduces the number of operations in the compute graph compared to a naive implementation.
    • Formula: Implements the standard Hamming window equation: $w(n) = 0.54 - 0.46 \cos\left(\frac{2\pi n}{M-1}\right)$.
    • Edge Cases: Handles M < 1 (empty) and M = 1 (returns [1.0]) explicitly to avoid division by zero.
  • Python Bindings: Exposed via nanobind with explicit type signature (nb::sig) and full documentation in python/src/ops.cpp.

Test Plan

Verified locally against NumPy for numerical accuracy.

Verification script:

import mlx.core as mx
import numpy as np

M = 50
mx_hamming = mx.hamming(M)
np_hamming = np.hamming(M)

# Check for numerical closeness (float32 precision)
assert np.allclose(mx_hamming, np_hamming, atol=1e-6), "Mismatch with NumPy implementation"
print("✅ Verification passed against numpy.hamming")

Unit Tests:

Added test_hamming in python/tests/test_ops.py covering standard cases, M=1, and 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)

@angeloskath angeloskath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good, thanks!

In the future feel free to merge all these into 1 PR since they are addressing the same topic and are fairly small additions.

@angeloskath
angeloskath merged commit 360639c into ml-explore:main Feb 17, 2026
16 checks passed
@Vlor999
Vlor999 deleted the feat/add-hamming-window branch February 17, 2026 17:52
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