#1457·gonum

dsp/window: fix spectral leakage parameter documentation

Author: kortschakCreated Sep 16, 2020Updated Sep 6, 2026

In the discussion around windowing end points, it was raised that the spectral leakage parameters we report were wrong for the functions that we provide. This got me interested to make sure that we are reporting the correct values.

So I wrote a small utility to leave in the dsp/window directory for use by authors of new window functions to check the spectral parameters before they're added. In doing this I checked all the parameters that we have from the original additions. They match-ish, making me feel a little uncomfortable. To address this I've extended the utility to be able to consume a spectrum profile from another source (e.g. NumPy) to confirm that the results match between implementations. NumPy only provides two windows (Blackman and Hamming), so those were checked.

The spectrum and spectral leakage parameter from Blackman match perfectly between the two implementations.

First the Gonum implementation,

blackman_20_1000_go

and then the NumPy.

blackman_20_1000_py

Note that the program does not calculate the beta value from the spectrum data from the python code, but the python code calculates the same value as the Go utility for the Gonum functions.

The match between Hamming implementations is close but not exact.

hamming_20_1000_go

hamming_20_1000_py.

The Hamming window is based on a constant defined (variably) as either α=25/46 or the decimal approximations α=0.54. NumPy uses the decimal approximation while we use a machine-level precision representation of the fraction.

If I replace the constant we use with the approximate value, again we match perfectly.

hamming_20_1000_go_approx

On the basis of this, I'm pretty confident that the utility works correctly and that at least the pair of windows that can be compared against other implementations are correct (depending on the correctness of using the higher precision constant* - perhaps we should use the 2dp approximations to match others).

Note added from "On the Use of Windows for Harmonic Analysis with the Discrete Fourier Transform" formula 30b

Perfect cancellation of the fit sidelobe (at = 2.5 [2n/N]) occurs when a = 25/46 ( ≐ 0.543 478 261). If is selected as 0.54 (an approximation to 25/46), the new zero occurs at ≐ 2.6 [2/N] and a marked improvement in sidelobe level is realized. For this value of , the window is called the Hamming window and is identified by... [the equation with the decimal approximations].

So, probably we should report the values that our functions (and those of other people) actually produce, not the magical numbers that have been passed down in books. Doing this, and providing the parameters that we use would be helpful.

It's nice to see that the utility allowed me to exactly replicate figure 5 from the WP article on DFT symmetry.

Symmetric: gaussian_8_symm

Periodic: gaussian_8_per

* There seems to be a lot of voodoo in all this.