I have a numpy.array
a
with the shape (8, 16000, 2)
which represents 8 stereo audios each 1s long. I want to sample a 0.5s subsample from each recording. Thus, I sample start and end indices in the following way:
a = np.random.rand(8, 16000, 2)
possible_starts = np.arange(8000)
start_idxs = np.random.choice(possible_starts, size=a.shape[0])
end_idxs = start_idxs + 8000
How to extract these different slices for each row in numpy efficienty? I obviously can do it in loop:
b = np.zeros((8, 8000, 2))
for i in range(a.shape[0]):
b[i] = a[i, start_idxs[i]: end_idxs[i], :]
but I want to do it in the vectorised way.