Skip to content

Commit baebe44

Browse files
authored
Merge pull request #25122 from gapplef/patch-1
FIX: scaling factor for window with negative value
2 parents d1f321f + 0a9e632 commit baebe44

File tree

2 files changed

+33
-6
lines changed

2 files changed

+33
-6
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

lib/matplotlib/tests/test_mlab.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ def test_psd_window_hanning(self):
615615
noverlap=0,
616616
sides=self.sides,
617617
window=mlab.window_none)
618-
spec_c *= len(ycontrol1)/(np.abs(windowVals)**2).sum()
618+
spec_c *= len(ycontrol1)/(windowVals**2).sum()
619619
assert_array_equal(fsp_g, fsp_c)
620620
assert_array_equal(fsp_b, fsp_c)
621621
assert_allclose(spec_g, spec_c, atol=1e-08)
@@ -662,14 +662,41 @@ def test_psd_window_hanning_detrend_linear(self):
662662
noverlap=0,
663663
sides=self.sides,
664664
window=mlab.window_none)
665-
spec_c *= len(ycontrol1)/(np.abs(windowVals)**2).sum()
665+
spec_c *= len(ycontrol1)/(windowVals**2).sum()
666666
assert_array_equal(fsp_g, fsp_c)
667667
assert_array_equal(fsp_b, fsp_c)
668668
assert_allclose(spec_g, spec_c, atol=1e-08)
669669
# these should not be almost equal
670670
with pytest.raises(AssertionError):
671671
assert_allclose(spec_b, spec_c, atol=1e-08)
672672

673+
def test_psd_window_flattop(self):
674+
# flattop window
675+
# adaption from https://github.com/scipy/scipy/blob\
676+
# /v1.10.0/scipy/signal/windows/_windows.py#L562-L622
677+
a = [0.21557895, 0.41663158, 0.277263158, 0.083578947, 0.006947368]
678+
fac = np.linspace(-np.pi, np.pi, self.NFFT_density_real)
679+
win = np.zeros(self.NFFT_density_real)
680+
for k in range(len(a)):
681+
win += a[k] * np.cos(k * fac)
682+
683+
spec, fsp = mlab.psd(x=self.y,
684+
NFFT=self.NFFT_density,
685+
Fs=self.Fs,
686+
noverlap=0,
687+
sides=self.sides,
688+
window=win,
689+
scale_by_freq=False)
690+
spec_a, fsp_a = mlab.psd(x=self.y,
691+
NFFT=self.NFFT_density,
692+
Fs=self.Fs,
693+
noverlap=0,
694+
sides=self.sides,
695+
window=win)
696+
assert_allclose(spec*win.sum()**2,
697+
spec_a*self.Fs*(win**2).sum(),
698+
atol=1e-08)
699+
673700
def test_psd_windowarray(self):
674701
freqs = self.freqs_density
675702
spec, fsp = mlab.psd(x=self.y,

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy