@@ -210,39 +210,6 @@ def detrend_linear(y):
210210 return y - (b * x + a )
211211
212212
213- def _stride_windows (x , n , noverlap = 0 ):
214- """
215- Get all windows of *x* with length *n* as a single array,
216- using strides to avoid data duplication.
217-
218- .. warning::
219-
220- It is not safe to write to the output array. Multiple
221- elements may point to the same piece of memory,
222- so modifying one value may change others.
223-
224- Parameters
225- ----------
226- x : 1D array or sequence
227- Array or sequence containing the data.
228- n : int
229- The number of data points in each window.
230- noverlap : int, default: 0 (no overlap)
231- The overlap between adjacent windows.
232-
233- References
234- ----------
235- `stackoverflow: Rolling window for 1D arrays in Numpy?
236- <https://stackoverflow.com/a/6811241>`_
237- `stackoverflow: Using strides for an efficient moving average filter
238- <https://stackoverflow.com/a/4947453>`_
239- """
240- if noverlap >= n :
241- raise ValueError ('noverlap must be less than n' )
242- return np .lib .stride_tricks .sliding_window_view (
243- x , n , axis = 0 )[::n - noverlap ].T
244-
245-
246213def _spectral_helper (x , y = None , NFFT = None , Fs = None , detrend_func = None ,
247214 window = None , noverlap = None , pad_to = None ,
248215 sides = None , scale_by_freq = None , mode = None ):
@@ -272,6 +239,9 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
272239 if NFFT is None :
273240 NFFT = 256
274241
242+ if noverlap >= NFFT :
243+ raise ValueError ('noverlap must be less than NFFT' )
244+
275245 if mode is None or mode == 'default' :
276246 mode = 'psd'
277247 _api .check_in_list (
@@ -334,15 +304,17 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
334304 raise ValueError (
335305 "The window length must match the data's first dimension" )
336306
337- result = _stride_windows (x , NFFT , noverlap )
307+ result = np .lib .stride_tricks .sliding_window_view (
308+ x , NFFT , axis = 0 )[::NFFT - noverlap ].T
338309 result = detrend (result , detrend_func , axis = 0 )
339310 result = result * window .reshape ((- 1 , 1 ))
340311 result = np .fft .fft (result , n = pad_to , axis = 0 )[:numFreqs , :]
341312 freqs = np .fft .fftfreq (pad_to , 1 / Fs )[:numFreqs ]
342313
343314 if not same_data :
344315 # if same_data is False, mode must be 'psd'
345- resultY = _stride_windows (y , NFFT , noverlap )
316+ resultY = np .lib .stride_tricks .sliding_window_view (
317+ y , NFFT , axis = 0 )[::NFFT - noverlap ].T
346318 resultY = detrend (resultY , detrend_func , axis = 0 )
347319 resultY = resultY * window .reshape ((- 1 , 1 ))
348320 resultY = np .fft .fft (resultY , n = pad_to , axis = 0 )[:numFreqs , :]
0 commit comments