@@ -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