将一维数组转换为重叠的二维数组

Converting 1-d array to 2-d array with overlapping

我需要将大小为 N 的一维数组转换为大小为 A*B > N 的二维数组。让我们以这种情况为例:

int oneDimensionalArray[6] = {7, 8, 10, 11, 12, 15};
//then the second array would be
int twoDimensionalArray[2][4] = {{7, 8, 10, 11},
                                 {10, 11, 12, 15}};

这用于数字声音处理中所谓的重叠相加方法。我试过这种方法,但结果不正确:

   for(unsigned long i = 0; i < amountOfWindows; i++)
   {
       for(unsigned long j = hopSize; j < windowLength; j++)
       {
           //buffer without the overlapping
           if( (i * amountOfWindows + j) >= bufferLength)
               break;

           windowedBuffer[i][j] = unwindowedBuffer[i * amountOfWindows + j];
       }
   }

   for(unsigned long i = 1; i < amountOfWindows; i++ )
   {
       for(unsigned long j = 0; j < hopSize; j++)
       {
           // Filling the overlapping region
           windowedBuffer[i][j] = windowedBuffer[i-1][windowLength - hopSize + i];
       }
   }

我也试过使用取模运算找到关系,但我找不到正确的。这是我试过的:

windowedBuffer[m][n % (windowLength - hopSize)] = unwindowedBuffer[n];

既然你已经知道hopSize(从你的评论),你想要的只是:

for (size_t i = 0; i < amountOfWindows; ++i) {
    for (size_t j = 0; j < windowLength; ++j) {
        windowedBuffer[i][j] = unwindowedBuffer[i * hopSize + j];
    }
}

其中 amountOfWindowswindowLengthhopSize 是您的参数(在您的示例中分别为 2、4 和 2)。