我想将音频的扁平通道阵列重塑为4D阵列(因为音频有4个通道(。重塑示例如下:
输入示例:[a1,b1,c1,d1,a2,b2,c2,d2,…]
输出4D阵列:[[a1,a2,…],[b1,b2,…]、[c1,c2,…]和[d1,d2,…]]
4D阵列的每个子阵列必须是音频通道之一。我怎样才能以最快的方式完成它?
>>> data = np.arange(20)
>>> data
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19])
>>> data.reshape((4, -1), order='F')
array([[ 0, 4, 8, 12, 16],
[ 1, 5, 9, 13, 17],
[ 2, 6, 10, 14, 18],
[ 3, 7, 11, 15, 19]])
另一种可能性是使用np.lib.stride_tricks.as_strided
:
a = np.array([1, 2, 3, 4, 11, 22, 33, 44, 111, 222, 333, 444], dtype=np.int16)
x = np.lib.stride_tricks.as_strided(
a, shape=(4, len(a) // 4), strides=(1 * 2, 4 * 2)
)
print(x)
打印:
[[ 1 11 111]
[ 2 22 222]
[ 3 33 333]
[ 4 44 444]]