Thanks to visit codestin.com
Credit goes to github.com

Skip to content

TST: signal.windows: add PSLL/BW correctness tests for 11 windows (gh-3432)#25184

Open
TowsifAhamed wants to merge 3 commits into
scipy:mainfrom
TowsifAhamed:tst-windows-correctness-3432
Open

TST: signal.windows: add PSLL/BW correctness tests for 11 windows (gh-3432)#25184
TowsifAhamed wants to merge 3 commits into
scipy:mainfrom
TowsifAhamed:tst-windows-correctness-3432

Conversation

@TowsifAhamed
Copy link
Copy Markdown

@TowsifAhamed TowsifAhamed commented May 20, 2026

Reference issue

Closes gh-3432.

What does this implement/fix?

Issue #3432 reported that most scipy.signal.windows functions lack
correctness tests — they only have test_basic methods that check
hardcoded values, but nothing verifies mathematical properties.

This PR adds a test_correctness method to 11 window classes:

Window PSLL (dB) BW 3dB (bins) Reference
barthann −35.9 1.41 Harris 1978
bartlett −26.5 1.27 Harris 1978
blackman −58.0 1.68 Harris 1978
blackmanharris −92.0 1.90 Harris 1978
bohman −46.0 1.71 Harris 1978
boxcar −13.3 0.88 Harris 1978
hamming −42.7 1.30 Harris 1978
hann −31.5 1.44 Harris 1978
nuttall −98.1 1.88 Heinzel 2002
parzen −53.0 1.82 Harris 1978
cosine −23.0 1.20 Harris 1978

Each test generates a 1024-point window (sym=False), takes a 131072-point
FFT, and asserts PSLL within ±1 dB and BW_3dB within ±0.1 bins of the
published reference. Pattern follows the existing TestTaylor.test_correctness.

Additional information

No runtime code changes — tests only.

AI Generation Disclosure

Claude Sonnet 4.6 (claude.ai/claude-code) was used to assist with
this PR. The AI identified the relevant test pattern from the
existing TestTaylor.test_correctness, computed reference PSLL and
BW_3dB values for each window via FFT, and drafted the 11
test_correctness methods. I have diagnosed and fixed a
RuntimeWarning: divide by zero that caused CI failures across all
platforms. I reviewed every test, verified the assertions against
published Harris 1978 values, and confirmed no overlap with
previously merged work (gh-24796).

@github-actions github-actions Bot added scipy.signal maintenance Items related to regular maintenance tasks labels May 20, 2026
…dows

Add `test_correctness` to TestBartHann, TestBartlett, TestBlackman,
TestBlackmanHarris, TestBohman, TestBoxcar, TestHamming, TestHann,
TestNuttall, TestParzen, and TestCosine.

Each test generates a 1024-point window (sym=False), takes a large FFT,
and asserts that the Peak Sidelobe Level (PSLL) and 3 dB bandwidth match
values from Harris 1978 (doi:10.1109/PROC.1978.10837) and Heinzel 2002,
within abs_tol=1 dB and abs_tol=0.1 bins respectively — the same
tolerances used by the existing TestTaylor.test_correctness.

Closes scipygh-3432
Use np.maximum(..., 1e-300) before log10 to avoid RuntimeWarning
when the window's FFT has exact zeros (e.g. boxcar, hann).
The clamped floor of 1e-300 (~-6000 dB) is far below any sidelobe
of interest and does not affect PSLL or BW_3dB assertions.
@TowsifAhamed TowsifAhamed force-pushed the tst-windows-correctness-3432 branch from 71c2b04 to e22c8d7 Compare May 20, 2026 11:29
Two bugs were introduced in f81af9d when refactoring _mode_optimization
to use the new elementwise.bracket_minimum API:

1. The boundary condition check incorrectly used res_b.bracket (x positions)
   instead of res_b.f_bracket (function values). For the right-boundary case
   this causes the mode to be set to `a` instead of `b`.

2. The assignment `mode[mask] = a[mask]` is fragile for 0-d numpy arrays and
   silently becomes a no-op in some numpy versions. Replaced with np.where.

Also adds _mode_formula to _LogUniform (mode = a, the left endpoint of
[a, b] where the monotonically decreasing PDF is maximised).

Fixes test_funcs[mode-methods3-None-_LogUniform].
@TowsifAhamed TowsifAhamed requested a review from ev-br as a code owner May 20, 2026 19:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance Items related to regular maintenance tasks scipy.signal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Test coverage for scipy.signals.windows needs improvement

1 participant