Add the bartlett function (#3155)

This commit is contained in:
willem adnet
2026-03-03 11:40:54 -08:00
committed by GitHub
parent f145ece976
commit 8cd377b7db
4 changed files with 52 additions and 0 deletions
+22
View File
@@ -1428,6 +1428,28 @@ void init_ops(nb::module_& m) {
"stream"_a = nb::none(),
nb::sig(
"def arange(stop : Union[int, float], step : Union[None, int, float] = None, dtype: Optional[Dtype] = None, *, stream: Union[None, Stream, Device] = None) -> array"));
m.def(
"bartlett",
&mlx::core::bartlett,
"M"_a,
nb::kw_only(),
"stream"_a = nb::none(),
R"pbdoc(
Return the Bartlett window.
The Bartlett window is a taper formed by using a weighted cosine.
.. math::
w(n) = 1 - \frac{2|n - (M-1)/2|}{M-1}
\qquad 0 \le n \le M-1
Args:
M (int): Number of points in the output window.
Returns:
array: The window, with the maximum value normalized to one (the value one
appears only if the number of samples is odd).
)pbdoc");
m.def(
"hanning",
&mlx::core::hanning,
+12
View File
@@ -1474,6 +1474,18 @@ class TestOps(mlx_tests.MLXTestCase):
self.assertEqual(a.size, 0)
self.assertEqual(a.dtype, mx.float32)
def test_bartlett_general(self):
a = mx.bartlett(10)
expected = np.bartlett(10)
self.assertTrue(np.allclose(a, expected, atol=1e-5))
a = mx.bartlett(1)
self.assertEqual(a.item(), 1.0)
a = mx.bartlett(0)
self.assertEqual(a.size, 0)
self.assertEqual(a.dtype, mx.float32)
def test_blackman_general(self):
a = mx.blackman(10)
expected = np.blackman(10)