Skip to content

Commit cebd3d7

Browse files
authored
FIX: scaling factor for window with negative value
Simply drop the `np.abs()` on window to fix the wrong scaling factor for window with negative value. For more detail refer to matplotlib#24821 **Caution**: With this fix, the behavior would change for window with complex value. With `np.abs()` on window, it seems can handle complex value, but I don't think it's the right way to do it. As it can't fall back to real value case for complex value with zero imaginary part and negative real part (for example -1 + 0j). Also, I didn't understand the need for complex window, so here I simply ignore the complex case. And this is consistent with the implementation of [scipy](https://github.com/scipy/scipy/blob/d9f75db82fdffef06187c9d8d2f0f5b36c7a791b/scipy/signal/_spectral_py.py#L1854-L1859).
1 parent 5ec2bd2 commit cebd3d7

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

lib/matplotlib/mlab.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,12 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
395395
elif mode == 'psd':
396396
result = np.conj(result) * result
397397
elif mode == 'magnitude':
398-
result = np.abs(result) / np.abs(window).sum()
398+
result = np.abs(result) / window.sum()
399399
elif mode == 'angle' or mode == 'phase':
400400
# we unwrap the phase later to handle the onesided vs. twosided case
401401
result = np.angle(result)
402402
elif mode == 'complex':
403-
result /= np.abs(window).sum()
403+
result /= window.sum()
404404

405405
if mode == 'psd':
406406

@@ -424,10 +424,10 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
424424
result /= Fs
425425
# Scale the spectrum by the norm of the window to compensate for
426426
# windowing loss; see Bendat & Piersol Sec 11.5.2.
427-
result /= (np.abs(window)**2).sum()
427+
result /= (window**2).sum()
428428
else:
429429
# In this case, preserve power in the segment, not amplitude
430-
result /= np.abs(window).sum()**2
430+
result /= window.sum()**2
431431

432432
t = np.arange(NFFT/2, len(x) - NFFT/2 + 1, NFFT - noverlap)/Fs
433433

0 commit comments

Comments
 (0)