Skip to content

Commit

Permalink
Adding overlapping random patches
Browse files Browse the repository at this point in the history
  • Loading branch information
mkholghi authored and atruskie committed Jun 8, 2018
1 parent 89fe597 commit 61e48f6
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
37 changes: 37 additions & 0 deletions src/AudioAnalysisTools/DSP/PatchSampling.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,43 @@ public static double[][] GetPatches(double[,] spectrogram, int patchWidth, int p
patches.Add(MatrixTools.Matrix2Array(submatrix));
}
}
else
{
if (samplingMethod == "overlapped random")
{
int no = 0;
while (no < numOfPatches)
{
//First select a random patch
int rInt = rn.Next(0, rows - patchHeight); //selecting a random number from the height of the matrix
int cInt = rn.Next(0, cols - patchWidth); //selecting a random number from the width of the matrix
double[,] submatrix = MatrixTools.Submatrix(spectrogram, rInt, cInt,
rInt + patchHeight - 1, cInt + patchWidth - 1);
//convert a matrix to a vector by concatenating columns and
//store it to the array of vectors
patches.Add(MatrixTools.Matrix2Array(submatrix));
no++;

//shifting the row
//note that we don't shift column as we select full band patches
rInt = rInt + 1;
//Second, slide the patch window (rInt+1) to select the next patch
double[,] submatrix2 = MatrixTools.Submatrix(spectrogram, rInt, cInt,
rInt + patchHeight - 1, cInt + patchWidth - 1);
patches.Add(MatrixTools.Matrix2Array(submatrix2));
no++;

/*
rInt = rInt + 2;
//Second, slide the patch window (rInt+1) to select the next patch
double[,] submatrix3 = MatrixTools.Submatrix(spectrogram, rInt, cInt,
rInt + patchHeight - 1, cInt + patchWidth - 1);
patches.Add(MatrixTools.Matrix2Array(submatrix3));
no++;
*/
}
}
}
}

return patches.ToArray();
Expand Down
34 changes: 23 additions & 11 deletions tests/Acoustics.Test/AudioAnalysisTools/DSP/PcaWhiteningTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ public void TestPcaWhitening()
var outputNormAmpImagePath = Path.Combine(resultDir, "NormAmplitudeSpectrogram.png");
var outputMelImagePath = Path.Combine(resultDir, "MelScaleSpectrogram.png");
var outputNormMelImagePath = Path.Combine(resultDir, "NormalizedMelScaleSpectrogram.png");
var outputNoiseReducedImagePath = Path.Combine(resultDir, "NoiseReducedSpectrogram.png");
var outputNoiseReducedMelImagePath = Path.Combine(resultDir, "NoiseReducedMelSpectrogram.png");
var outputWhitenedSpectrogramPath = Path.Combine(resultDir, "WhitenedSpectrogram.png");
var outputReSpecImagePath = Path.Combine(resultDir, "ReconstrcutedSpectrogram.png");

var outputImagePath = Path.Combine(resultDir, "spec.png");
//var outputImagePath = Path.Combine(resultDir, "spec.png");

/* Exp5
var projectionMatrixPath = Path.Combine(resultDir, "ProjectionMatrix");
Expand Down Expand Up @@ -397,7 +397,7 @@ public void TestPcaWhitening()
//check whether there is any file in the folder/subfolders
if (Directory.GetFiles(folderPath, "*", SearchOption.AllDirectories).Length == 0)
{
throw new ArgumentException("The folder of recording is empty...");
throw new ArgumentException("The folder of recordings is empty...");
}

/*
Expand All @@ -414,10 +414,11 @@ public void TestPcaWhitening()

int nyquist = nq; //11025;
int frameSize = 1024;
int finalBinCount = 128; // 256;
int finalBinCount = 256; //100; //40; //200; // 128; //
int hertzInterval = 1000;
FreqScaleType scaleType = FreqScaleType.Mel;
var freqScale = new FrequencyScale(scaleType, nyquist, frameSize, finalBinCount, hertzInterval);
var fst = freqScale.ScaleType;

var sonoConfig = new SonogramConfig
{
Expand All @@ -428,13 +429,13 @@ public void TestPcaWhitening()
MelBinCount = (scaleType == FreqScaleType.Mel) ? finalBinCount : frameSize / 2,
//NoiseReductionType = NoiseReductionType.Standard,
NoiseReductionType = NoiseReductionType.None,
NoiseReductionParameter = 0.0,
//NoiseReductionParameter = 2.0, //0.0,
};

List<double[,]> randomPatches = new List<double[,]>();
int patchWidth = finalBinCount; //256; //16; //full band patches
int patchHeight = 4; //16; // 2; //
int noOfRandomPatches = 10; //10; //100; //500; //
int patchHeight = 4; //16; // 2; // 4; // 6; //
int noOfRandomPatches = 20; //10; //100; //500; //
//int fileCount = Directory.GetFiles(folderPath, "*.wav").Length;


Expand Down Expand Up @@ -466,7 +467,9 @@ public void TestPcaWhitening()
var noiseReducedImage = sonogram.GetImageFullyAnnotated(sonogram.GetImage(), "NOISEREDUCEDSPECTROGRAM: " + fst.ToString(), freqScale.GridLineLocations);
noiseReducedImage.Save(outputNoiseReducedImagePath, ImageFormat.Png);
*/
sonogram.Data = PcaWhitening.NoiseReduction(sonogram.Data);

//sonogram.Data = PcaWhitening.NoiseReduction(sonogram.Data); ****

//sonogram.Data = dataMatrix2;
//var noiseReducedImage = sonogram.GetImageFullyAnnotated(sonogram.GetImage(), "NOISEREDUCEDSPECTROGRAM: " + fst.ToString(), freqScale.GridLineLocations);
//noiseReducedImage.Save(outputNoiseReducedImagePath, ImageFormat.Png);
Expand All @@ -475,14 +478,15 @@ 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());
randomPatches.Add(PatchSampling.GetPatches(sonogram.Data, patchWidth, patchHeight, noOfRandomPatches, "overlapped random").ToMatrix());
}

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

var actual = PcaWhitening.Whitening(allPatchM);

//Processing the target spectrogram
var recording2Path = PathHelper.ResolveAsset("Recordings", "BAC2_20071008-085040.wav");
var recording2 = new AudioRecording(recording2Path);
/*
Expand All @@ -504,11 +508,19 @@ public void TestPcaWhitening()

var sonogram2 = new SpectrogramStandard(sonoConfig, recording2.WavReader);

// DO DRAW SPECTROGRAM
var image = sonogram2.GetImageFullyAnnotated(sonogram2.GetImage(), "MELSPECTROGRAM: " + fst.ToString(), freqScale.GridLineLocations);
image.Save(outputMelImagePath, ImageFormat.Png);

sonogram2.Data = PcaWhitening.RmsNormalization(sonogram2.Data);
var image2 = sonogram2.GetImageFullyAnnotated(sonogram2.GetImage(), "NORMALISEDMELSPECTROGRAM: " + fst.ToString(), freqScale.GridLineLocations);
image2.Save(outputNormMelImagePath, ImageFormat.Png);
//var sonogram2 = new SpectrogramStandard(amplitudeSpectrogram2);
//var standImage2 = sonogram2.GetImageFullyAnnotated(sonogram2.GetImage(), "SPECTROGRAM: " + fst2.ToString(), freqScale2.GridLineLocations);
//standImage2.Save(outputLinScaImagePath, ImageFormat.Png);
sonogram2.Data = PcaWhitening.NoiseReduction(sonogram2.Data);
var image3 = sonogram2.GetImageFullyAnnotated(sonogram2.GetImage(), "NOISEREDUCEDMELSPECTROGRAM: " + fst.ToString(), freqScale.GridLineLocations);
image3.Save(outputNoiseReducedMelImagePath, ImageFormat.Png);
//sonogram2.Data = dataMatrix;
//int patchWidth2 = 16; //32; //
//int patchHeight2 = 16; //32; //
Expand All @@ -524,8 +536,8 @@ public void TestPcaWhitening()

// DO DRAW SPECTROGRAM
//var fst = freqScale.ScaleType;
var respecImage = sonogram2.GetImageFullyAnnotated(sonogram2.GetImage(), "SPECTROGRAM: " + freqScale.ScaleType.ToString(), freqScale.GridLineLocations);
respecImage.Save(outputImagePath, ImageFormat.Png);
var respecImage = sonogram2.GetImageFullyAnnotated(sonogram2.GetImage(), "RECONSTRUCTEDSPECTROGRAM: " + freqScale.ScaleType.ToString(), freqScale.GridLineLocations);
respecImage.Save(outputReSpecImagePath, ImageFormat.Png);

//var respecImage2 = sonogram2.GetImageFullyAnnotated(sonogram2.GetImage(), "RECONSTRUCTEDSPECTROGRAM: " + fst2.ToString(), freqScale2.GridLineLocations);
//respecImage2.Save(outputReSpecImagePath, ImageFormat.Png);
Expand Down

0 comments on commit 61e48f6

Please sign in to comment.