How to convert an array of frames into array of frame pairs?

Viewed 33

I have a numpy array of arrays, say 400x80. I want to turn it into an array 400x160 so that each item would be formed like this: enter image description here

Here each frame of 80 is copied into the beginning of the next frame and the first frame gest 80 zeroes. So how to do such thing in numpy? Is there a mechanism that can generalize to three or more frames?

2 Answers

Lets assume that your data is in X, then

np.hstack((np.vstack((np.zeros(X.shape[1]), X[:-1])), X))

where:

np.vstack((np.zeros(X.shape[1]), X[:-1]))

creates the first column, we add a row of zeros, and cut the last row

and then with hstack we just combine the two "columns" together.

import numpy as np
X = np.random.normal(size=(400, 80))
print(np.hstack((np.vstack((np.zeros(X.shape[1]), X[:-1])), X)).shape)

gives (400, 160) as expected.

Or you can do things manually:

Y = []
previos = np.zeros(X.shape[1])
for row in X:
  Y.append(np.vstack((previous, row)))
  previous = row
Y = np.array(Y)

You are trying to create a sliding window. If you want a view that looks into the original buffer, indexing the same memory locations multiple times, you can make some adjustments.

m, n = a.shape
p = 2 * n

x = np.lib.stride_tricks.as_strided(a, shape=(m * ((n - 1) + p) // p, p), strides=a.strides)

This is the totally general approach. If you're guaranteed p % n == 0, then for k = p // n, you can do

x = np.lib.stride_tricks.as_strided(a, shape=(m - k + 1, n * k), strides=a.strides)

In either case, to avoid memory issues, you can use x.copy()

Related