简体   繁体   English

3d 阵列的错误整形

[英]Incorrect reshaping of 3d array

I am doing some processing with a matrix of shape 1536 x 16 x 48. Here are some details about this dimensions:我正在对形状为 1536 x 16 x 48 的矩阵进行一些处理。以下是有关此尺寸的一些详细信息:

  • 1536: data collected in 6 seconds 1536:6秒内收集到的数据
  • 16: number of collectors (or electrodes in my case) 16:收集器的数量(在我的情况下是电极)
  • 48: number of samples 48:样本数

So, i have 48 samples of 1536 values (representing 6 seconds of data) from the perspective of 16 different collectors.因此,从 16 个不同的收集器的角度来看,我有 1536 个值的 48 个样本(代表 6 秒的数据)。

My goal is to do some processing with this matrix, but to do so some transformations are needed first.我的目标是对这个矩阵进行一些处理,但首先需要进行一些转换。

  1. Since 6 seconds is a large sequence, i want to split each of those samples into smaller sequences, specially 3 or 2 seconds.由于 6 秒是一个大序列,我想将每个样本分成更小的序列,特别是 3 或 2 秒。 For instance, 1 sample of 6 seconds (1536) can be split into 2 samples of 3 seconds (768) or 3 samples of 2 seconds (512).例如,1 个 6 秒样本 (1536) 可以拆分为 2 个 3 秒样本 (768) 或 3 个 2 秒样本 (512)。 The shape of this transformed matrix would go from 1536x16x48 to 768x16x96 (for 3 seconds) or 512x16x144 (for 2 seconds).这个变换矩阵的形状是 go 从1536x16x48768x16x96 (3 秒)或512x16x144 (2 秒)。

  2. Once i have this new matrix, i want to reshape it so i get one 2d matrix per observer and all values organized in columns instead of rows (eg for 2 seconds split: 512x16x144 => 144x512x16 ).一旦我有了这个新矩阵,我想重塑它,以便每个观察者得到一个 2d 矩阵,并且所有值都以列而不是行组织(例如 2 秒拆分: 512x16x144 => 144x512x16 )。

  3. Finally, i can now loop through 3rd dimension ( 16 ), do some computations (ie fast fourier transform) with each 2d matrix and reduce (sum) them all into a single one to get a final 144 x 512 matrix (in 2 seconds-split scenario).最后,我现在可以遍历第 3 维( 16 ),对每个 2d 矩阵进行一些计算(即快速傅立叶变换)并将它们全部归约(求和)为一个,以获得最终的144 x 512矩阵(在 2 秒内 -分割场景)。

The following code is what i made with numpy , but it is clearly wrong for me when i plot samples generated from this method.以下代码是我用numpy制作的,但是当我使用这种方法生成的 plot 样本时,这对我来说显然是错误的。

def generate_fft_data(data,labels, n_seconds_split=3):
    x = 256 * n_seconds_split
    y = 16
    z = 48 * int(6/n_seconds_split)
    data = data.transpose(2,0,1).reshape(x,y,z).transpose(2,0,1)
    fft_data = []
    for electrode in range(data.shape[2]):
        y_t = fft(data[:,:,electrode])
        fft_data.append(np.abs(y_t))
    sum_of_ffts = np.add.reduce(fft_data) 
    return sum_of_ffts

I can provide more details if needed.如果需要,我可以提供更多详细信息。 Thanks in advance.提前致谢。

You can try this:你可以试试这个:

def generate_fft_data(data,n_seconds_split=3):
    x_split = 6//n_seconds_split
    # split along 0 axis, stack pieces along the last axis and transpose
    arr = np.dstack(np.split(data, x_split)).transpose(2,0,1)
    fft_data = []
    for electrode in range(arr.shape[2]):
        y_t = fft(arr[:,:,electrode])
        fft_data.append(np.abs(y_t))
    sum_of_ffts = np.add.reduce(fft_data) 
    return sum_of_ffts
In [88]: data = np.ones((1536,16,48))
In [89]:  x = 256 * 3
    ...:  y = 16
    ...:  z = 48 * int(6/3)
In [90]: x,y,z
Out[90]: (768, 16, 96)
In [91]: data.transpose(2,0,1).shape
Out[91]: (48, 1536, 16)
In [92]: data.transpose(2,0,1).reshape(x,y,z).shape
Out[92]: (768, 16, 96)

Reshaping [91] to [92] does not make sense.将 [91] 重塑为 [92] 没有意义。 It works because the numbers 'add' up, but the distribution is messed up.它之所以有效,是因为数字“加起来”,但分布混乱。

I think you want to first split the 1536 dimension into (2,768) (or the 3 equivalent), then move that 2 dimension to the end, and recombine it with the 48.我认为您想首先将 1536 维度拆分为 (2,768)(或等效的 3),然后将该 2 维度移动到末尾,并将其与 48 重新组合。

The details could vary, but here's one such sequence:细节可能会有所不同,但这是一个这样的序列:

data1 = data.reshape(2,768,16,48).transpose(1,2,0,3).reshape(768,16,96)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM