Skip to content

Fix periodic hann_window and hamming_window for odd lengths - #129744

Merged
copybara-service[bot] merged 1 commit into
tensorflow:masterfrom
bodapatisaikrishna:fix/periodic-window-odd-length
Nov 2, 2026
Merged

copybara-service[bot] merged 1 commit into
tensorflow:masterfrom
bodapatisaikrishna:fix/periodic-window-odd-length

Conversation

@bodapatisaikrishna

Copy link
Copy Markdown
Contributor

Fixes #129743

Problem

tf.signal.hann_window and tf.signal.hamming_window return the symmetric window for odd window_length even when periodic=True, which is the default. For example, hann_window(5) gives [0, .5, 1, .5, 0] instead of [0, .345, .905, .905, .345]. Because tf.signal.stft uses hann_window by default, an odd frame_length gives spectra that differ from SciPy, librosa and PyTorch, and from TensorFlow's own tf.raw_ops.AudioSpectrogram: a max abs difference of 0.89 on the repro in the issue, compared with 3e-7 for an even length. The window also loses its constant overlap-add property, e.g. N = 9 with a hop of 3.

Change

In _raised_cosine_window, divide by window_length + periodic - 1 for every length, instead of window_length + periodic * even - 1. A periodic window of length N is then the first N points of the symmetric window of length N + 1, for both odd and even N. This is how scipy.signal.get_window, torch.hann_window and GetPeriodicHann in core/kernels/spectrogram.cc define it. Even lengths and symmetric windows are unchanged. Length 1 keeps its existing special case.

Tests:

  • window_ops_test._scipy_raised_cosine copied SciPy 0.14's windows, which had the same odd-length behavior. It now extends and truncates for every length, as current SciPy does.
  • spectral_ops_test._np_hann_periodic_window returned a symmetric window for odd lengths. It is now the standard periodic Hann.
  • New test_periodic_window_odd_length checks Hann and Hamming at lengths 3, 5, 9 and 31, with both a static length and one only known at run time.

Testing

I don't have a Bazel build. I ran the changed test files with Python against the TF 2.21.0 macOS arm64 wheel, with this PR's window_ops.py copied in:

  • window_ops_test.py: 453 tests OK (1 skipped).
  • spectral_ops_test.py: 70 tests OK (1 skipped). For this run I also copied master's reconstruction_ops.py into the wheel and left out test_inverse_stft_rejects_zero_frame_length. That test needs kernel validation that is on master but not in the 2.21 binary, and it aborts there with or without this change.
  • The updated tests against the old window_ops.py: 68 failures in window_ops_test (odd-length periodic cases) and 8 in spectral_ops_test (test_stft_and_inverse_stft with frame lengths 25 and 23). So the tests catch the bug.
  • pylint --rcfile=tensorflow/tools/ci_build/pylintrc on the three Python files: no new messages.

I did not test on GPU or TFLite. The TFLite window_ops_test only uses length 10, so it is unaffected.

Notes

This changes numerical output for hann_window/hamming_window with an odd length and periodic=True, and for stft/inverse_stft with the default window and an odd frame_length. I added a RELEASE.md entry. inverse_stft_window_fn derives its window from the forward window, so stft → inverse_stft round trips still reconstruct the signal. Anyone who needs the old values can pass periodic=False.

@google-ml-butler google-ml-butler Bot added the size:M CL Change Size: Medium label Oct 30, 2026
@google-ml-butler
google-ml-butler Bot requested a review from cantonios October 30, 2026 06:39
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Oct 30, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates the periodic window calculation for tf.signal.hann_window and tf.signal.hamming_window to ensure consistent behavior for odd window lengths, aligning them with scipy.signal.get_window and tf.raw_ops.AudioSpectrogram. The changes include updates to the core logic in window_ops.py, adjustments to test utilities in spectral_ops_test.py and window_ops_test.py, and a corresponding update to RELEASE.md. I have no feedback to provide as the changes are well-implemented and include appropriate test coverage.

@keerthanakadiri
keerthanakadiri requested a review from a team October 30, 2026 07:48
@keerthanakadiri keerthanakadiri self-assigned this Oct 30, 2026
@keerthanakadiri keerthanakadiri added the prtype:bugfix PR to fix a bug label Oct 30, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Oct 30, 2026
@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Oct 30, 2026
@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Approved by Reviewer in PR Queue Oct 30, 2026
`_raised_cosine_window` divided by `window_length + periodic * even - 1`,
so `periodic=True` only took effect for even lengths. For odd lengths the
symmetric window was returned instead, e.g. `hann_window(5)` gave
`[0, .5, 1, .5, 0]` rather than `[0, .345, .905, .905, .345]`.

This followed SciPy 0.14's window code, which the test helper linked to.
SciPy has since defined a periodic window of length N as the first N
points of a symmetric window of length N + 1 for every N, and that is
also what `scipy.signal.get_window`, `torch.hann_window` and
TensorFlow's own `AudioSpectrogram` kernel compute.

Because `tf.signal.stft` uses `hann_window` by default, an odd
`frame_length` gave spectra that differ from those references, and the
"periodic" window lost its constant overlap-add property (for example
N = 9 with a hop of 3).

Divide by `window_length + periodic - 1` for every length, and update
the NumPy reference windows in window_ops_test and spectral_ops_test,
which encoded the old behavior. Add a test that a periodic window of odd
length, static or only known at run time, is the truncated symmetric
window of length N + 1.

This changes the output of hann_window/hamming_window with odd lengths
and periodic=True, and of stft/inverse_stft that use them; RELEASE.md
notes it.
@bodapatisaikrishna
bodapatisaikrishna force-pushed the fix/periodic-window-odd-length branch from f27f750 to 826090a Compare October 30, 2026 09:54
@google-ml-butler google-ml-butler Bot removed the ready to pull PR ready for merge process label Oct 30, 2026
@bodapatisaikrishna

Copy link
Copy Markdown
Contributor Author

Rebased onto the latest master (826090a); no code changes. The earlier build failures were all in compiling org_xprof (dcn_analysis.cc: kMaxCollectivesToDisplay and LineIdType missing from tsl::profiler), before any code from this PR was built.

@google-ml-butler google-ml-butler Bot added the ready to pull PR ready for merge process label Oct 30, 2026
@nithyak0204 nithyak0204 removed the awaiting review Pull request awaiting review label Oct 30, 2026
@copybara-service
copybara-service Bot merged commit e7a0851 into tensorflow:master Nov 2, 2026
34 of 39 checks passed
@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Merged in PR Queue Nov 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kokoro:force-run Tests on submitted change prtype:bugfix PR to fix a bug ready to pull PR ready for merge process size:M CL Change Size: Medium

Projects

Status: Merged

Development

Successfully merging this pull request may close these issues.

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

4 participants