将1-d数组转换为重叠

时间:2016-06-28 11:26:13

标签: c++ audio multidimensional-array overlap dimensions

我需要将大小为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];

1 个答案:

答案 0 :(得分:1)

由于您已经知道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)。

相关问题