Skip to content

tf.signal.hann_window/hamming_window ignore periodic=True for odd window lengths #129743

Closed

Description

@bodapatisaikrishna

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

No. There is no tf-nightly wheel for macOS arm64. The code involved is unchanged on master (97b8364).

Source

binary

TensorFlow version

2.21.0

Custom code

Yes

OS platform and distribution

macOS 15.7.2 (arm64)

Python version

3.11.7

Current behavior?

tf.signal.hann_window and tf.signal.hamming_window ignore periodic=True (the default) when window_length is odd. They return the symmetric window instead.

A periodic window of length N is the first N points of a symmetric window of length N + 1. That is what scipy.signal.get_window, scipy.signal.windows.hann(N, sym=False) and torch.hann_window(N, periodic=True) return for every N. TensorFlow's own AudioSpectrogram kernel does the same (GetPeriodicHann in tensorflow/core/kernels/spectrogram.cc).

The cause is in _raised_cosine_window in tensorflow/python/ops/signal/window_ops.py:

even = 1 - math_ops.mod(window_length, 2)
n = math_ops.cast(window_length + periodic * even - 1, dtype=dtype)

The even factor means periodic only matters for even lengths. This copies SciPy 0.14's window code, which window_ops_test.py links to and which SciPy has since changed. spectral_ops_test.py has a NumPy helper named _np_hann_periodic_window that makes the same choice, so the tests don't catch it.

Effects:

  • tf.signal.stft uses hann_window by default. With an odd frame_length, its output differs from SciPy/librosa/PyTorch and from tf.raw_ops.AudioSpectrogram on the same input (below).
  • The window no longer has the constant overlap-add property a periodic Hann window should have. For N = 9 with a hop of 3, the sum of shifted windows varies by about 0.06 instead of being constant.

Expected: hann_window(5) returns [0, 0.3455, 0.9045, 0.9045, 0.3455] (periodic), not the same values as hann_window(5, periodic=False).

Standalone code to reproduce the issue

import numpy as np
import scipy.signal
import tensorflow as tf

np.set_printoptions(precision=4)
# periodic=True is the default.
print("periodic :", tf.signal.hann_window(5, dtype=tf.float64).numpy())
print("symmetric:", tf.signal.hann_window(5, periodic=False, dtype=tf.float64).numpy())
print("scipy    :", scipy.signal.get_window("hann", 5))  # periodic Hann

# tf.signal.stft (default window) vs TensorFlow's own AudioSpectrogram op,
# which uses a periodic Hann window for every length.
x = tf.random.stateless_normal([64], seed=[1, 2])
for n, fft_length in ((8, 8), (9, 16)):
  spec = tf.raw_ops.AudioSpectrogram(
      input=x[:, None], window_size=n, stride=4, magnitude_squared=False)[0]
  stft = tf.abs(tf.signal.stft(x, frame_length=n, frame_step=4,
                               fft_length=fft_length))
  print(f"frame_length={n}: max diff = {float(tf.reduce_max(tf.abs(spec - stft))):.3g}")

Relevant log output

periodic : [0.  0.5 1.  0.5 0. ]
symmetric: [0.  0.5 1.  0.5 0. ]
scipy    : [0.     0.3455 0.9045 0.9045 0.3455]
frame_length=8: max diff = 2.98e-07
frame_length=9: max diff = 0.891

I have a fix ready and will open a PR that links here.

Activity

  1. added
    awaiting PR merge
    2.21.0
    comp:signal
    on Oct 30, 2026
  2. Kayyuri commented on Oct 30, 2026

    @Kayyuri

    Hi @bodapatisaikrishna,
    Thanks for reporting the issue and submitting a fix for TensorFlow. Once the PR is merged, the issue will be resolved. We appreciate your contribution.

  3. google-ml-butler commented on Nov 2, 2026

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

2.21.0awaiting PR mergecomp:signaltype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions