Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window functions precision (they are not exactly symmetric) #12811

Open
kimstik opened this issue Sep 3, 2020 · 4 comments
Open

Window functions precision (they are not exactly symmetric) #12811

kimstik opened this issue Sep 3, 2020 · 4 comments

Comments

@kimstik
Copy link

kimstik commented Sep 3, 2020

I noted that window functions is perfectly bit-to-bit symmetric in case of float32, but in float64 they are not.
Error is about 2 mantissa bit in case of Blackman window.
Numpy issue was solved in #17195 .

@kimstik
Copy link
Author

kimstik commented Dec 4, 2020

Copy-paste of original report:

I noted that window functions is perfectly bit-to-bit symmetric in case of float32, but in float64 they are not.
Error is about 2 mantissa bit in case of Blackman window.

Reproducing code example:

import numpy as np
import scipy.signal.windows


wnd = (	np.blackman,
		np.bartlett,
		np.hamming,
		np.hanning,
		scipy.signal.windows.flattop            ,
		scipy.signal.windows.barthann           ,
		scipy.signal.windows.bartlett           ,
		scipy.signal.windows.blackman           ,
		scipy.signal.windows.blackmanharris     ,
		scipy.signal.windows.bohman             ,
		scipy.signal.windows.boxcar             ,
		scipy.signal.windows.cosine             ,
		scipy.signal.windows.flattop            ,
		scipy.signal.windows.hamming            ,
		scipy.signal.windows.hann               ,
		scipy.signal.windows.nuttall            ,
		scipy.signal.windows.parzen             ,
		scipy.signal.windows.triang				,
		)

for t in (np.float32, np.float64):	#, np.longdouble):
	print (t, np.finfo(t))
	for w in wnd:
		x = w(4096).astype(t)
		err = np.max(np.abs( x[:2048] - x[4095:2047:-1] ))
		print (w, err, np.log2(err))

Results:

<class 'numpy.float32'> Machine parameters for float32
---------------------------------------------------------------
precision =   6   resolution = 1.0000000e-06
machep =    -23   eps =        1.1920929e-07
negep =     -24   epsneg =     5.9604645e-08
minexp =   -126   tiny =       1.1754944e-38
maxexp =    128   max =        3.4028235e+38
nexp =        8   min =        -max
---------------------------------------------------------------

<function blackman at 0x000001B4D49289D0> 0.0 -inf
<function bartlett at 0x000001B4D4928A60> 0.0 -inf
<function hamming at 0x000001B4D4928B80> 0.0 -inf
<function hanning at 0x000001B4D4928AF0> 0.0 -inf
<function flattop at 0x000001B4E3591E50> 0.0 -inf
<function barthann at 0x000001B4E35971F0> 0.0 -inf
<function bartlett at 0x000001B4E3591EE0> 0.0 -inf
<function blackman at 0x000001B4E3591CA0> 0.0 -inf
<function blackmanharris at 0x000001B4E3591DC0> 0.0 -inf
<function bohman at 0x000001B4E3591C10> 0.0 -inf
<function boxcar at 0x000001B4E3591A60> 0.0 -inf
<function cosine at 0x000001B4E3597670> 0.0 -inf
<function flattop at 0x000001B4E3591E50> 0.0 -inf
<function hamming at 0x000001B4E3597310> 0.0 -inf
<function hann at 0x000001B4E3591F70> 0.0 -inf
<function nuttall at 0x000001B4E3591D30> 0.0 -inf
<function parzen at 0x000001B4E3591B80> 0.0 -inf
<function triang at 0x000001B4E3591AF0> 0.0 -inf


<class 'numpy.float64'> Machine parameters for float64
---------------------------------------------------------------
precision =  15   resolution = 1.0000000000000001e-15
machep =    -52   eps =        2.2204460492503131e-16
negep =     -53   epsneg =     1.1102230246251565e-16
minexp =  -1022   tiny =       2.2250738585072014e-308
maxexp =   1024   max =        1.7976931348623157e+308
nexp =       11   min =        -max
---------------------------------------------------------------

<function blackman at 0x000001B4D49289D0> 7.771561172376096e-16 -50.192645077942394
<function bartlett at 0x000001B4D4928A60> 1.1102230246251565e-16 -53.0
<function hamming at 0x000001B4D4928B80> 5.551115123125783e-16 -50.67807190511264
<function hanning at 0x000001B4D4928AF0> 5.551115123125783e-16 -50.67807190511264
<function flattop at 0x000001B4E3591E50> 5.551115123125783e-16 -50.67807190511264
<function barthann at 0x000001B4E35971F0> 3.3306690738754696e-16 -51.415037499278846
<function bartlett at 0x000001B4E3591EE0> 1.1102230246251565e-16 -53.0
<function blackman at 0x000001B4E3591CA0> 3.3306690738754696e-16 -51.415037499278846
<function blackmanharris at 0x000001B4E3591DC0> 4.440892098500626e-16 -51.0
<function bohman at 0x000001B4E3591C10> 4.440892098500626e-16 -51.0
<function boxcar at 0x000001B4E3591A60> 0.0 -inf
<function cosine at 0x000001B4E3597670> 3.469446951953614e-16 -51.35614381022528
<function flattop at 0x000001B4E3591E50> 5.551115123125783e-16 -50.67807190511264
<function hamming at 0x000001B4E3597310> 2.220446049250313e-16 -52.0
<function hann at 0x000001B4E3591F70> 2.220446049250313e-16 -52.0
<function nuttall at 0x000001B4E3591D30> 4.440892098500626e-16 -51.0
<function parzen at 0x000001B4E3591B80> 0.0 -inf
<function triang at 0x000001B4E3591AF0> 0.0 -inf

Numpy/Python version information:

1.19.0 3.8.4 (tags/v3.8.4:dfa645a, Jul 13 2020, 16:46:45) [MSC v.1924 64 bit (AMD64)]

@kimstik kimstik changed the title Window functions precision Window functions precision (they are not exactly symmetric) Dec 4, 2020
@kimstik
Copy link
Author

kimstik commented Jan 5, 2021

May this issue be classified as issue?

@kimstik
Copy link
Author

kimstik commented May 28, 2021

What is more that "scipy.signal.windows.dpss" is worst window function in terms of symmetricity.

import numpy as np
import scipy.signal.windows

for w in (	scipy.signal.windows.dpss	(4096,5)      ,
		scipy.signal.windows.kaiser	(4096,5*np.pi),	):
	err = np.max(np.abs( w-np.flip(w) ))
	exp = np.abs(np.log2(err))
	print( err, exp )

shows that Kaiser is perfect, but DPSS have error of 9.44e-12

@kimstik
Copy link
Author

kimstik commented Jul 5, 2022

scipy 1.9.0rc2 python 3.10.3
a lot of useful windows are still have limited precision (non-symmetric):

func                 	error   	exp bits
dpss                 	9.45e-12	36.6
general_cosine       	3.11e-15	48.2
flattop              	5.55e-16	50.7
nuttall              	4.44e-16	51.0
...

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

No branches or pull requests

3 participants