@@ -215,6 +215,7 @@ def detrend_linear(y):
215
215
return y - (b * x + a )
216
216
217
217
218
+ @_api .deprecated ("3.6" )
218
219
def stride_windows (x , n , noverlap = None , axis = 0 ):
219
220
"""
220
221
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):
246
247
"""
247
248
if noverlap is None :
248
249
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
249
262
250
263
if noverlap >= n :
251
264
raise ValueError ('noverlap must be less than n' )
@@ -254,8 +267,6 @@ def stride_windows(x, n, noverlap=None, axis=0):
254
267
255
268
x = np .asarray (x )
256
269
257
- if x .ndim != 1 :
258
- raise ValueError ('only 1-dimensional arrays can be used' )
259
270
if n == 1 and noverlap == 0 :
260
271
if axis == 0 :
261
272
return x [np .newaxis ]
@@ -370,15 +381,15 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
370
381
raise ValueError (
371
382
"The window length must match the data's first dimension" )
372
383
373
- result = stride_windows (x , NFFT , noverlap , axis = 0 )
384
+ result = _stride_windows (x , NFFT , noverlap )
374
385
result = detrend (result , detrend_func , axis = 0 )
375
386
result = result * window .reshape ((- 1 , 1 ))
376
387
result = np .fft .fft (result , n = pad_to , axis = 0 )[:numFreqs , :]
377
388
freqs = np .fft .fftfreq (pad_to , 1 / Fs )[:numFreqs ]
378
389
379
390
if not same_data :
380
391
# if same_data is False, mode must be 'psd'
381
- resultY = stride_windows (y , NFFT , noverlap )
392
+ resultY = _stride_windows (y , NFFT , noverlap )
382
393
resultY = detrend (resultY , detrend_func , axis = 0 )
383
394
resultY = resultY * window .reshape ((- 1 , 1 ))
384
395
resultY = np .fft .fft (resultY , n = pad_to , axis = 0 )[:numFreqs , :]
0 commit comments