私は必要なユースケースを処理するために以下のルーチンを書いたが、 "underlap"の実装/テストは行っていない。
改善のためのご意見をお寄せください。
def buffer(x, n, p=0, opt=None):
'''Mimic MATLAB routine to generate buffer array
MATLAB docs here: https://se.mathworks.com/help/signal/ref/buffer.html
Args
----
x: signal array
n: number of data segments
p: number of values to overlap
opt: initial condition options. default sets the first `p` values
to zero, while 'nodelay' begins filling the buffer immediately.
'''
import numpy
if p >= n:
raise ValueError('p ({}) must be less than n ({}).'.format(p,n))
# Calculate number of columns of buffer array
cols = int(numpy.ceil(len(x)/float(n-p)))
# Check for opt parameters
if opt == 'nodelay':
# Need extra column to handle additional values left
cols += 1
elif opt != None:
raise SystemError('Only `None` (default initial condition) and '
'`nodelay` (skip initial condition) have been '
'implemented')
# Create empty buffer array
b = numpy.zeros((n, cols))
# Fill buffer by column handling for initial condition and overlap
j = 0
for i in range(cols):
# Set first column to n values from x, move to next iteration
if i == 0 and opt == 'nodelay':
b[0:n,i] = x[0:n]
continue
# set first values of row to last p values
elif i != 0 and p != 0:
b[:p, i] = b[-p:, i-1]
# If initial condition, set p elements in buffer array to zero
else:
b[:p, i] = 0
# Get stop index positions for x
k = j + n - p
# Get stop index position for b, matching number sliced from x
n_end = p+len(x[j:k])
# Assign values to buffer array from x
b[p:n_end,i] = x[j:k]
# Update start index location for next iteration of x
j = k
return b
「np.split」を試しましたか?指定されたインデックスで分割されるので、不規則な間隔を処理する必要があります。範囲を使ってこれらのインデックスを作成するだけです。 – Divakar
またはこれはhttps://mail.scipy.org/pipermail/scipy-user/2006-November/009962.htmlとhttps://mail.scipy.org/pipermail/scipy-user/attachments/のコードに役立つかもしれません。 20061119/292f81e3/attachment.py –
'buffer'文書を一目見てみると、numpy''stride_tricks.as_strided'が思い出されます。特に重複やスキップを扱う能力があります。しかし、これはあまりにも強力で危険な場合があります。 – hpaulj