Skip to content

Commit

Permalink
amended ListOf2DArrayToOne2DArray method, so that it can merge matric…
Browse files Browse the repository at this point in the history
…es with different number of rows
  • Loading branch information
mkholghi committed Jan 7, 2019
1 parent 9981af3 commit 8f9dac6
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions src/AudioAnalysisTools/DSP/PatchSampling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace AudioAnalysisTools.DSP
using Accord.Math;
using TowseyLibrary;
using WavTools;
using YamlDotNet.Core.Tokens;

public static class PatchSampling
{
Expand Down Expand Up @@ -262,21 +263,25 @@ public static List<double[,]> GetFreqBandMatrices(double[,] matrix, int numberOf
}

/// <summary>
/// convert a list of patch matrices to one matrix
/// convert a list of patch matrices to one matrix by row
/// patch matrices can have different row numbers but must have the same column number
/// </summary>
public static double[,] ListOf2DArrayToOne2DArray(List<double[,]> listOfPatchMatrices)
{
int numberOfPatches = listOfPatchMatrices[0].GetLength(0);
double[,] allPatchesMatrix = new double[listOfPatchMatrices.Count * numberOfPatches, listOfPatchMatrices[0].GetLength(1)];
int sumNumberOfPatches = 0;
foreach (var matrix in listOfPatchMatrices)
{
sumNumberOfPatches = matrix.GetLength(0) + sumNumberOfPatches;
}

double[,] allPatchesMatrix = new double[sumNumberOfPatches, listOfPatchMatrices[0].GetLength(1)];
int start = 0;

for (int i = 0; i < listOfPatchMatrices.Count; i++)
{
var m = listOfPatchMatrices[i];
if (m.GetLength(0) != numberOfPatches)
{
throw new ArgumentException("All arrays must be the same length");
}

allPatchesMatrix.AddToArray(m, DoubleSquareArrayExtensions.MergingDirection.Row, i * m.GetLength(0));
allPatchesMatrix.AddToArray(m, DoubleSquareArrayExtensions.MergingDirection.Row, start);
start = m.GetLength(0) + start;
}

return allPatchesMatrix;
Expand Down

0 comments on commit 8f9dac6

Please sign in to comment.