Repository navigation
Fix periodic hann_window and hamming_window for odd lengths - #129744
copybara-service[bot] merged 1 commit into
Conversation
There was a problem hiding this comment.
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.
`_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.
f27f750 to
826090a
Compare
|
Rebased onto the latest master (826090a); no code changes. The earlier build failures were all in compiling |
e7a0851
into
tensorflow:master
Fixes #129743
Problem
tf.signal.hann_windowandtf.signal.hamming_windowreturn the symmetric window for oddwindow_lengtheven whenperiodic=True, which is the default. For example,hann_window(5)gives[0, .5, 1, .5, 0]instead of[0, .345, .905, .905, .345]. Becausetf.signal.stftuseshann_windowby default, an oddframe_lengthgives spectra that differ from SciPy, librosa and PyTorch, and from TensorFlow's owntf.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 bywindow_length + periodic - 1for every length, instead ofwindow_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 howscipy.signal.get_window,torch.hann_windowandGetPeriodicHannincore/kernels/spectrogram.ccdefine it. Even lengths and symmetric windows are unchanged. Length 1 keeps its existing special case.Tests:
window_ops_test._scipy_raised_cosinecopied 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_windowreturned a symmetric window for odd lengths. It is now the standard periodic Hann.test_periodic_window_odd_lengthchecks 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.pycopied 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'sreconstruction_ops.pyinto the wheel and left outtest_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.window_ops.py: 68 failures inwindow_ops_test(odd-length periodic cases) and 8 inspectral_ops_test(test_stft_and_inverse_stftwith frame lengths 25 and 23). So the tests catch the bug.pylint --rcfile=tensorflow/tools/ci_build/pylintrcon the three Python files: no new messages.I did not test on GPU or TFLite. The TFLite
window_ops_testonly uses length 10, so it is unaffected.Notes
This changes numerical output for
hann_window/hamming_windowwith an odd length andperiodic=True, and forstft/inverse_stftwith the default window and an oddframe_length. I added aRELEASE.mdentry.inverse_stft_window_fnderives its window from the forward window, sostft→inverse_stftround trips still reconstruct the signal. Anyone who needs the old values can passperiodic=False.