Skip to content

Commit

Permalink
selecting patches from different frequency bands
Browse files Browse the repository at this point in the history
  • Loading branch information
mkholghi authored and atruskie committed Jun 8, 2018
1 parent 93cf0da commit 2197801
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 17 deletions.
50 changes: 42 additions & 8 deletions src/AudioAnalysisTools/DSP/PatchSampling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,18 +206,18 @@ public static double[][] GetPatches(double[,] spectrogram, int patchWidth, int p
}

/*
* converts a sepctrogram matrix to 3 matrices by dividing the column (freq) into 3 parts
* currently the first 1/4 is the lower, the second and third 1/4 forms the mid, and the last 1/4 is the upper freq band.
* converts a sepctrogram matrix to 3 or 4 matrices by dividing the column (freq) into 3 or 4 parts
* noOfBand as an input parameter indicates how many output bands (3 or 4) are needed
* currently the first 1/4 is the lower, the second and third 1/4 forms the mid (or mid1 and mid2), and the last 1/4 is the upper freq band.
*/
public static List<double[,]> GetFreqBandMatrices(double[,] matrix)
public static List<double[,]> GetFreqBandMatrices(double[,] matrix, int noOfBands)
{
List<double[,]> allSubmatrices = new List<double[,]>();
int cols = matrix.GetLength(1); //number of freq bins
int rows = matrix.GetLength(0);
int newCol = cols / 4;

double[,] minFreqBandMatrix = new double[rows, newCol];
double[,] midFreqBandMatrix = new double[rows, newCol * 2];
double[,] maxFreqBandMatrix = new double[rows, newCol];

//Note that I am not aware of any faster way to copy a part of 2D-array
Expand All @@ -230,15 +230,48 @@ public static List<double[,]> GetFreqBandMatrices(double[,] matrix)
}

allSubmatrices.Add(minFreqBandMatrix);
for (int i = 0; i < rows; i++)

if (noOfBands == 3)
{
for (int j = 0; j < newCol*2; j++)
double[,] midFreqBandMatrix = new double[rows, newCol * 2];
for (int i = 0; i < rows; i++)
{
midFreqBandMatrix[i, j] = matrix[i, j + newCol];
for (int j = 0; j < newCol * 2; j++)
{
midFreqBandMatrix[i, j] = matrix[i, j + newCol];
}
}

allSubmatrices.Add(midFreqBandMatrix);
}
else
{
if (noOfBands == 4)
{
double[,] mid1FreqBandMatrix = new double[rows, newCol];
double[,] mid2FreqBandMatrix = new double[rows, newCol];
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < newCol; j++)
{
mid1FreqBandMatrix[i, j] = matrix[i, j + newCol];
}
}

allSubmatrices.Add(mid1FreqBandMatrix);

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < newCol; j++)
{
mid2FreqBandMatrix[i, j] = matrix[i, j + newCol * 2];
}
}

allSubmatrices.Add(mid2FreqBandMatrix);
}
}

allSubmatrices.Add(midFreqBandMatrix);
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < newCol; j++)
Expand All @@ -248,6 +281,7 @@ public static List<double[,]> GetFreqBandMatrices(double[,] matrix)
}

allSubmatrices.Add(maxFreqBandMatrix);

return allSubmatrices;
}

Expand Down
71 changes: 62 additions & 9 deletions tests/Acoustics.Test/AudioAnalysisTools/DSP/PcaWhiteningTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ public void TestPcaWhitening()
//+++++++++++++++++++++++++++++++++++++++++++++++++Exp3: different freq bands, different source-target, same patch size
/* Exp5
//First: creating 3 matrices from 3 different freq bands of the source spectrogram
List<double[,]> allSubmatrices = PatchSampling.GetFreqBandMatrices(sonogram.Data);
int noOfFreqBand = 3;
List<double[,]> allSubmatrices = PatchSampling.GetFreqBandMatrices(sonogram.Data, noOfFreqBand);
double[][,] matrices = allSubmatrices.ToArray();
//Second: creating 3 projection matrices from 3 different group of random patches
Expand Down Expand Up @@ -437,9 +438,16 @@ public void TestPcaWhitening()
//NoiseReductionParameter = 2.0, //0.0,
};

// +++++full band patches
List<double[,]> randomPatches = new List<double[,]>();
int patchWidth = finalBinCount; //256; //16; //full band patches
int patchHeight = 4; //2; // 16; // 6; //
// +++++OR patches from different freq bands
List<double[,]> randomPatches0 = new List<double[,]>();
List<double[,]> randomPatches1 = new List<double[,]>();
List<double[,]> randomPatches2 = new List<double[,]>();
List<double[,]> randomPatches3 = new List<double[,]>();

int patchWidth = finalBinCount / 4; // when selecting patches from four different freq bands //finalBinCount; //256; //16; //full band patches
int patchHeight = 2; //4; // 16; // 6; //
int noOfRandomPatches = 20; //20; // 100; //500; //
//int fileCount = Directory.GetFiles(folderPath, "*.wav").Length;

Expand Down Expand Up @@ -499,21 +507,66 @@ public void TestPcaWhitening()
//int rows = sonogram.Data.GetLength(0); //3247
//int cols = sonogram.Data.GetLength(1); //256

randomPatches.Add(PatchSampling.GetPatches(sonogram.Data, patchWidth, patchHeight, noOfRandomPatches, "random").ToMatrix());
// +++++full band patches
//randomPatches.Add(PatchSampling.GetPatches(sonogram.Data, patchWidth, patchHeight, noOfRandomPatches, "random").ToMatrix());
// +++++OR patches from four different freq bands (42 * 2)
//creating 4 matrices from 4 different freq bands of the source spectrogram
int noOfFreqBand = 4;
List<double[,]> allSubmatrices = PatchSampling.GetFreqBandMatrices(sonogram.Data, noOfFreqBand);
double[][,] matrices = allSubmatrices.ToArray();

//Second: creating 4 projection matrices from 4 different group of random patches
//obtained from 4 different freq bands of the source spectrogram
List<double[,]> projectionMatrices = new List<double[,]>();
List<double[,]> eigenVectors = new List<double[,]>();
List<int> noOfComponents = new List<int>();

int count = 0;
while (count < allSubmatrices.Count)
{
if (count == 0)
{
randomPatches0.Add(PatchSampling.GetPatches(matrices[count], patchWidth, patchHeight, noOfRandomPatches, "random").ToMatrix());
count++;
}
else
{
if (count == 1)
{
randomPatches1.Add(PatchSampling.GetPatches(matrices[count], patchWidth, patchHeight, noOfRandomPatches, "random").ToMatrix());
count++;
}
else
{
//continue this for 2 and 3
}
}
}
for (int i = 0; i < allSubmatrices.Count; i++)
{
var randomPatchList = randomPatches+i.ToString()
var randomPatch = PatchSampling.GetPatches(matrices[i], patchWidth, patchHeight, noOfRandomPatches, "random").ToMatrix();
// here the random patches of each band should be add to a list of random patches assigned to that band
}
}

}

var whitening = PcaWhitening.Whitening(randomPatch);
projectionMatrices.Add(whitening.Item1);
eigenVectors.Add(whitening.Item3);
noOfComponents.Add(whitening.Item4);

//convert list of random patches matrices to one matrix
double[,] allPatchM = PatchSampling.ListOf2DArrayToOne2DArray(randomPatches);

//Apply PCA Whitening
//var actual = PcaWhitening.Whitening(allPatchM);
var actual = PcaWhitening.Whitening(allPatchM);

//Do k-means clustering
int noOfClusters = 32; //64; //10; //50;
//var clusteringOutput = KmeansClustering.Clustering(actual.Item2, noOfClusters);
var clusteringOutput = KmeansClustering.Clustering(allPatchM, noOfClusters);
int noOfClusters = 64; //32; //10; //50;
var clusteringOutput = KmeansClustering.Clustering(actual.Item2, noOfClusters);
//var clusteringOutput = KmeansClustering.Clustering(allPatchM, noOfClusters);
int[] sortOrder = KmeansClustering.SortClustersBasedOnSize(clusteringOutput.Item2);

//Draw cluster image directly from centroid csv file: Michael's code (not working properly at the moment!)
Expand Down Expand Up @@ -641,7 +694,7 @@ public void TestPcaWhitening()
List<double[]> maxFeatureVectors = new List<double[]>();
List<double[]> stdFeatureVectors = new List<double[]>();
int c = 0;
int noFrames = 6; //12; // number of frames needs to be concatenated to form 1 second
int noFrames = 12; //6; // number of frames needs to be concatenated to form 1 second
while (c < featureTransVectors.GetLength(0))
{
//First, make a list of six patches that would be equal to 1 second
Expand Down

0 comments on commit 2197801

Please sign in to comment.