Search code examples
c++audiomultidimensional-arrayoverlapdimensions

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


I need to convert one dimensional array of size N to two dimensional array of size A*B > N. Let us take such case:

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}};

This is used in so called overlap-add method used in digital sound processing. I have tried this approach which gives improper results:

   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];
       }
   }

I've also tried finding the relation using the modulo operation but I can't find the right one. This is the one that I've tried:

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

Solution

  • Since you already know hopSize (from your comment), what you want is simply:

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

    Where amountOfWindows, windowLength and hopSize are you parameters (resp. 2, 4 and 2 in your example).