From 3a2c56cbd9ef75e5d1eee1d49bc0569bb372705a Mon Sep 17 00:00:00 2001 From: Muhammad Umar Nadeem Date: Sun, 29 Nov 2026 06:49:46 +0500 Subject: [PATCH] Add a lazy Hanning window generator --- docs/source/api/xbuilder.rst | 2 + docs/source/builder.rst | 7 ++++ include/xtensor/generators/xbuilder.hpp | 53 +++++++++++++++++++++++++ test/test_xbuilder.cpp | 38 ++++++++++++++++++ 4 files changed, 100 insertions(+) diff --git a/docs/source/api/xbuilder.rst b/docs/source/api/xbuilder.rst index b315c3730..714eb3deb 100644 --- a/docs/source/api/xbuilder.rst +++ b/docs/source/api/xbuilder.rst @@ -39,6 +39,8 @@ Defined in ``xtensor/generators/xbuilder.hpp`` .. doxygenfunction:: xt::logspace +.. doxygenfunction:: xt::hanning + .. doxygenfunction:: xt::concatenate(std::tuple&&, std::size_t) .. doxygenfunction:: xt::stack diff --git a/docs/source/builder.rst b/docs/source/builder.rst index 04b193def..05fffb9d4 100644 --- a/docs/source/builder.rst +++ b/docs/source/builder.rst @@ -26,6 +26,13 @@ Numerical ranges - :cpp:func:`xt::linspace(start, stop, num_samples) `: generates num_samples evenly spaced numbers over given interval. - :cpp:func:`xt::logspace(start, stop, num_samples) `: generates num_samples evenly spaced on a log scale over given interval +Window functions +---------------- + +- :cpp:func:`xt::hanning(size) `: generates a symmetric Hann window. + Non-positive sizes produce an empty expression; a size of one produces ``{1.}``. + The default value type is ``double``; use ``xt::hanning(size)`` for ``float`` values. + Joining expressions ------------------- diff --git a/include/xtensor/generators/xbuilder.hpp b/include/xtensor/generators/xbuilder.hpp index 1026b21c7..e0bdf2124 100644 --- a/include/xtensor/generators/xbuilder.hpp +++ b/include/xtensor/generators/xbuilder.hpp @@ -19,6 +19,8 @@ #include #include #include +#include +#include #include #include @@ -467,6 +469,57 @@ namespace xt ); } + namespace detail + { + template + class hanning_fn + { + public: + + using value_type = T; + + hanning_fn(std::size_t size) + : m_size(size) + { + } + + template + inline T operator()(const It& /*begin*/, const It& end) const + { + if (m_size == 1) + { + return T(1); + } + T phase = T(2) * std::numbers::pi_v + * static_cast(*(end - 1)) / static_cast(m_size - 1); + return T(0.5) - T(0.5) * std::cos(phase); + } + + private: + + std::size_t m_size; + }; + } + + /** + * Generates a symmetric Hann window of length @a size. + * The values are 0.5 - 0.5 * cos(2 * pi * n / (size - 1)). + * Returns an empty expression for non-positive sizes and a single one for size 1. + * @param size number of samples + * @tparam T floating-point value_type of the expression (defaults to double) + * @return xgenerator that generates the values on access + */ + template + inline auto hanning(std::ptrdiff_t size) + { + static_assert(std::is_floating_point::value, "hanning requires a floating-point value type"); + std::size_t count = size > 0 ? static_cast(size) : 0; + return detail::make_xgenerator( + detail::fn_impl>(detail::hanning_fn(count)), + {count} + ); + } + /** * Generates @a num_samples numbers evenly spaced on a log scale over given interval * @param start start of interval (pow(base, start) is the first value). diff --git a/test/test_xbuilder.cpp b/test/test_xbuilder.cpp index d35847a8e..b1800ee67 100644 --- a/test/test_xbuilder.cpp +++ b/test/test_xbuilder.cpp @@ -298,6 +298,44 @@ namespace xt EXPECT_TENSOR_EQ(a, b); } + TEST(xbuilder, hanning) + { + auto odd = hanning(5); + EXPECT_EQ(odd.dimension(), size_t(1)); + EXPECT_EQ(odd.shape()[0], size_t(5)); + xarray expected_odd = {0., 0.5, 1., 0.5, 0.}; + EXPECT_TRUE(allclose(odd, expected_odd)); + xarray assigned = odd; + EXPECT_TRUE(allclose(assigned, expected_odd)); + + xarray expected_even = {0., 0.75, 0.75, 0.}; + EXPECT_TRUE(allclose(hanning(4), expected_even)); + EXPECT_EQ(hanning(5)(2), 1.); + + xarray broadcasted = ones({2, 5}) * hanning(5); + EXPECT_TRUE(allclose(broadcasted, broadcast(expected_odd, {2, 5}))); + } + + TEST(xbuilder, hanning_small_sizes) + { + EXPECT_EQ(hanning(-3).size(), size_t(0)); + EXPECT_EQ(hanning(0).size(), size_t(0)); + xarray empty = hanning(0); + EXPECT_EQ(empty.dimension(), size_t(1)); + EXPECT_EQ(empty.size(), size_t(0)); + EXPECT_EQ(hanning(1)(0), 1.); + xarray expected = {0., 0.}; + EXPECT_TRUE(allclose(hanning(2), expected)); + } + + TEST(xbuilder, hanning_float) + { + auto window = hanning(5); + static_assert(std::is_same_v); + xarray expected = {0.f, 0.5f, 1.f, 0.5f, 0.f}; + EXPECT_TRUE(allclose(window, expected)); + } + TEST(xbuilder, logspace) { auto ls = logspace(2., 3., 4);