From 7b2d4aff01358f9eeca6ac541dcb4b39f15bd706 Mon Sep 17 00:00:00 2001 From: LucasIsOnline Date: Mon, 11 Apr 2022 22:29:41 +0200 Subject: [PATCH] bugfix: scaling of windows with negative coefficients For proper scaling of the magnitude a division by the sum of all window elements is needed. To be able to handle complex window coefficients an abs() was introduced in a622508dd4c5b43e339c75716cc9f49a177b70a8 which falsifies the scaling for windows with negative coefficients. Most prominently the flattop window. A proper type check is introduced to handle both cases correctly. --- lib/matplotlib/mlab.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/matplotlib/mlab.py b/lib/matplotlib/mlab.py index 5e85a9c1194f..921eff1c0222 100644 --- a/lib/matplotlib/mlab.py +++ b/lib/matplotlib/mlab.py @@ -395,7 +395,11 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None, elif mode == 'psd': result = np.conj(result) * result elif mode == 'magnitude': - result = np.abs(result) / np.abs(window).sum() + result = np.abs(result) + if np.iscomplexobj(window): + result /= np.abs(window).sum() + else: + result /= np.asarray(window).sum() elif mode == 'angle' or mode == 'phase': # we unwrap the phase later to handle the onesided vs. twosided case result = np.angle(result)