Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitbc3a3ba

Browse files
committed
Use old stride_windows implementation on 32-bit builds
This was originally for i686 on Fedora, but is now applicable to WASM,which is 32-bit. The older implementation doesn't OOM.
1 parent1c84927 commitbc3a3ba

File tree

1 file changed

+37
-4
lines changed

1 file changed

+37
-4
lines changed

‎lib/matplotlib/mlab.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050
importfunctools
5151
fromnumbersimportNumber
52+
importsys
5253

5354
importnumpyasnp
5455

@@ -210,6 +211,30 @@ def detrend_linear(y):
210211
returny- (b*x+a)
211212

212213

214+
def_stride_windows(x,n,noverlap=0):
215+
ifnoverlap>=n:
216+
raiseValueError('noverlap must be less than n')
217+
ifn<1:
218+
raiseValueError('n cannot be less than 1')
219+
220+
x=np.asarray(x)
221+
222+
ifn==1andnoverlap==0:
223+
returnx[np.newaxis]
224+
ifn>x.size:
225+
raiseValueError('n cannot be greater than the length of x')
226+
227+
# np.lib.stride_tricks.as_strided easily leads to memory corruption for
228+
# non integer shape and strides, i.e. noverlap or n. See #3845.
229+
noverlap=int(noverlap)
230+
n=int(n)
231+
232+
step=n-noverlap
233+
shape= (n, (x.shape[-1]-noverlap)//step)
234+
strides= (x.strides[0],step*x.strides[0])
235+
returnnp.lib.stride_tricks.as_strided(x,shape=shape,strides=strides)
236+
237+
213238
def_spectral_helper(x,y=None,NFFT=None,Fs=None,detrend_func=None,
214239
window=None,noverlap=None,pad_to=None,
215240
sides=None,scale_by_freq=None,mode=None):
@@ -304,17 +329,25 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
304329
raiseValueError(
305330
"The window length must match the data's first dimension")
306331

307-
result=np.lib.stride_tricks.sliding_window_view(
308-
x,NFFT,axis=0)[::NFFT-noverlap].T
332+
ifsys.maxsize>2**32:
333+
result=np.lib.stride_tricks.sliding_window_view(
334+
x,NFFT,axis=0)[::NFFT-noverlap].T
335+
else:
336+
# The NumPy version on 32-bit will OOM, so use old implementation.
337+
result=_stride_windows(x,NFFT,noverlap=noverlap)
309338
result=detrend(result,detrend_func,axis=0)
310339
result=result*window.reshape((-1,1))
311340
result=np.fft.fft(result,n=pad_to,axis=0)[:numFreqs, :]
312341
freqs=np.fft.fftfreq(pad_to,1/Fs)[:numFreqs]
313342

314343
ifnotsame_data:
315344
# if same_data is False, mode must be 'psd'
316-
resultY=np.lib.stride_tricks.sliding_window_view(
317-
y,NFFT,axis=0)[::NFFT-noverlap].T
345+
ifsys.maxsize>2**32:
346+
resultY=np.lib.stride_tricks.sliding_window_view(
347+
y,NFFT,axis=0)[::NFFT-noverlap].T
348+
else:
349+
# The NumPy version on 32-bit will OOM, so use old implementation.
350+
resultY=_stride_windows(y,NFFT,noverlap=noverlap)
318351
resultY=detrend(resultY,detrend_func,axis=0)
319352
resultY=resultY*window.reshape((-1,1))
320353
resultY=np.fft.fft(resultY,n=pad_to,axis=0)[:numFreqs, :]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp