@@ -215,6 +215,7 @@ def detrend_linear(y):
215215 return y - (b * x + a )
216216
217217
218+ @_api .deprecated ("3.6" )
218219def stride_windows (x , n , noverlap = None , axis = 0 ):
219220 """
220221 Get all windows of x with length n as a single array,
@@ -246,6 +247,18 @@ def stride_windows(x, n, noverlap=None, axis=0):
246247 """
247248 if noverlap is None :
248249 noverlap = 0
250+ if np .ndim (x ) != 1 :
251+ raise ValueError ('only 1-dimensional arrays can be used' )
252+ return _stride_windows (x , n , noverlap , axis )
253+
254+
255+ def _stride_windows (x , n , noverlap = 0 , axis = 0 ):
256+ # np>=1.20 provides sliding_window_view, and we only ever use axis=0.
257+ if hasattr (np .lib .stride_tricks , "sliding_window_view" ) and axis == 0 :
258+ if noverlap >= n :
259+ raise ValueError ('noverlap must be less than n' )
260+ return np .lib .stride_tricks .sliding_window_view (
261+ x , n , axis = 0 )[::n - noverlap ].T
249262
250263 if noverlap >= n :
251264 raise ValueError ('noverlap must be less than n' )
@@ -254,8 +267,6 @@ def stride_windows(x, n, noverlap=None, axis=0):
254267
255268 x = np .asarray (x )
256269
257- if x .ndim != 1 :
258- raise ValueError ('only 1-dimensional arrays can be used' )
259270 if n == 1 and noverlap == 0 :
260271 if axis == 0 :
261272 return x [np .newaxis ]
@@ -370,15 +381,15 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
370381 raise ValueError (
371382 "The window length must match the data's first dimension" )
372383
373- result = stride_windows (x , NFFT , noverlap , axis = 0 )
384+ result = _stride_windows (x , NFFT , noverlap )
374385 result = detrend (result , detrend_func , axis = 0 )
375386 result = result * window .reshape ((- 1 , 1 ))
376387 result = np .fft .fft (result , n = pad_to , axis = 0 )[:numFreqs , :]
377388 freqs = np .fft .fftfreq (pad_to , 1 / Fs )[:numFreqs ]
378389
379390 if not same_data :
380391 # if same_data is False, mode must be 'psd'
381- resultY = stride_windows (y , NFFT , noverlap )
392+ resultY = _stride_windows (y , NFFT , noverlap )
382393 resultY = detrend (resultY , detrend_func , axis = 0 )
383394 resultY = resultY * window .reshape ((- 1 , 1 ))
384395 resultY = np .fft .fft (resultY , n = pad_to , axis = 0 )[:numFreqs , :]
0 commit comments