Skip to content

Commit

Permalink
Fixing Unit tests
Browse files Browse the repository at this point in the history
Issue #332
  • Loading branch information
towsey committed Aug 20, 2020
1 parent 13968a3 commit 2b6821f
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 52 deletions.
38 changes: 23 additions & 15 deletions src/AudioAnalysisTools/LongDurationSpectrograms/LDSpectrogramRGB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ public LDSpectrogramRGB(LdSpectrogramConfig config, IndexGenerationData indexGen
this.FreqScale = new FrequencyScale(fst);
throw new ArgumentException("Mel Scale is not yet implemented");

//break;
case "OctaveStandard":
int linearBound = 1000;
int octaveToneCount = 1; // this is set automatically
Expand All @@ -202,6 +201,11 @@ public LDSpectrogramRGB(LdSpectrogramConfig config, IndexGenerationData indexGen
this.FreqScale = new FrequencyScale(fst, nyquist, frameSize, linearBound, octaveToneCount, gridInterval);
break;

case "OctaveDataReduction":
fst = FreqScaleType.OctaveDataReduction;
this.FreqScale = new FrequencyScale(fst);
break;

case "OctaveCustom":
throw new ArgumentException("OctaveCustom Scale is not yet implemented");

Expand Down Expand Up @@ -1268,20 +1272,24 @@ public static Tuple<Image<Rgb24>, string>[] DrawSpectrogramsFromSpectralIndices(
//NEXT produce SPECTROGRAM RIBBONS
// These are useful for viewing multiple consecutive days of recording.
// Get matrix for each of the three indices.
string[] keys1 = colorMap1.Split('-');
double[,] indices1 = cs1.GetNormalisedSpectrogramMatrix(keys1[0]);
double[,] indices2 = cs1.GetNormalisedSpectrogramMatrix(keys1[1]);
double[,] indices3 = cs1.GetNormalisedSpectrogramMatrix(keys1[2]);

var ribbon = LdSpectrogramRibbons.GetSpectrogramRibbon(indices1, indices2, indices3);
ribbon.Save(FilenameHelpers.AnalysisResultPath(outputDirectory, fileStem, colorMap1 + LdSpectrogramRibbons.SpectralRibbonTag, "png"));

string[] keys2 = colorMap2.Split('-');
indices1 = cs1.GetNormalisedSpectrogramMatrix(keys2[0]);
indices2 = cs1.GetNormalisedSpectrogramMatrix(keys2[1]);
indices3 = cs1.GetNormalisedSpectrogramMatrix(keys2[2]);
ribbon = LdSpectrogramRibbons.GetSpectrogramRibbon(indices1, indices2, indices3);
ribbon.Save(FilenameHelpers.AnalysisResultPath(outputDirectory, fileStem, colorMap2 + LdSpectrogramRibbons.SpectralRibbonTag, "png"));
// DO NOT draw ribbons if spectrograms are already reduced for data reduction.
if (image1.Height > 128)
{
string[] keys1 = colorMap1.Split('-');
double[,] indices1 = cs1.GetNormalisedSpectrogramMatrix(keys1[0]);
double[,] indices2 = cs1.GetNormalisedSpectrogramMatrix(keys1[1]);
double[,] indices3 = cs1.GetNormalisedSpectrogramMatrix(keys1[2]);

var ribbon = LdSpectrogramRibbons.GetSpectrogramRibbon(indices1, indices2, indices3);
ribbon.Save(FilenameHelpers.AnalysisResultPath(outputDirectory, fileStem, colorMap1 + LdSpectrogramRibbons.SpectralRibbonTag, "png"));

string[] keys2 = colorMap2.Split('-');
indices1 = cs1.GetNormalisedSpectrogramMatrix(keys2[0]);
indices2 = cs1.GetNormalisedSpectrogramMatrix(keys2[1]);
indices3 = cs1.GetNormalisedSpectrogramMatrix(keys2[2]);
ribbon = LdSpectrogramRibbons.GetSpectrogramRibbon(indices1, indices2, indices3);
ribbon.Save(FilenameHelpers.AnalysisResultPath(outputDirectory, fileStem, colorMap2 + LdSpectrogramRibbons.SpectralRibbonTag, "png"));
}

// only return images if chromeless
return imageChrome == ImageChrome.Without
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,12 @@ public static double[] ExtractModalNoiseSubband(double[] modalNoise, int minHz,
/// <param name="freqScale">freq Scale.</param>
public static void DrawGridLinesOnImage(Image<Rgb24> bmp, TimeSpan startOffset, TimeSpan fullDuration, TimeSpan xAxisTicInterval, FrequencyScale freqScale)
{
if (freqScale.ScaleType == FreqScaleType.OctaveDataReduction)
{
//do not want grid lines in this case.
return;
}

FrequencyScale.DrawFrequencyLinesOnImage(bmp, freqScale, includeLabels: true);

// We have stopped drawing temporal gridlines on these spectrograms. Create unnecessary clutter.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,13 @@ public void TestAnalyzeSr22050Recording()

/// <summary>
/// Tests the analysis of an artificial seven minute long recording consisting of five harmonics.
/// NOTE: The Acoustic indices are calculated from an Octave frequency scale spectrogram.
/// NOTE: The Acoustic indices are calculated from an Octave frequency scale used for data reduction.
/// Each vector of indices has only 19 elements.
/// </summary>
[TestMethod]
public void TestAnalyzeSr64000Recording()
public void TestAnalyzeSr22050RecordingDataReduction()
{
int sampleRate = 64000;
int sampleRate = 22050;
double duration = 420; // signal duration in seconds = 7 minutes
int[] harmonics = { 500, 1000, 2000, 4000, 8000 };
var recording = DspFilters.GenerateTestRecording(sampleRate, duration, harmonics, WaveType.Cosine);
Expand All @@ -156,31 +157,14 @@ public void TestAnalyzeSr64000Recording()
WavWriter.WriteWavFileViaFfmpeg(recordingPath, recording.WavReader);

//var fst = FreqScaleType.Linear125OctaveTones28Nyquist32000;
var fst = FreqScaleType.OctaveCustom;
int nyquist = sampleRate / 2;
int frameSize = 16384;
int linearBound = 125;
int octaveToneCount = 28;
int gridInterval = 1000;
var freqScale = new FrequencyScale(fst, nyquist, frameSize, linearBound, octaveToneCount, gridInterval);

/*
// draw the signal as spectrogram just for debugging purposes
// but can only draw a two minute spectrogram when sr=64000 - change duration above.
duration = 120; // if drawing sonogram, then set signal duration = 2 minutes
var sonogram = OctaveFreqScale.ConvertRecordingToOctaveScaleSonogram(recording, fst);
var sonogramImage = sonogram.GetImageFullyAnnotated(sonogram.GetImage(), "SPECTROGRAM", freqScale.GridLineLocations);
var outputImagePath = this.outputDirectory.CombineFile("SignalSpectrogram_OctaveFreqScale.png");
sonogramImage.Save(outputImagePath.FullName);
*/
//NOTE: As of 2020 August, the only debugged octave scale is for data reduction
var fst = FreqScaleType.OctaveDataReduction;
//int nyquist = sampleRate / 2;
var freqScale = new FrequencyScale(fst);

// Now need to rewrite the config file with new parameter settings
var configPath = PathHelper.ResolveConfigFile("Towsey.Acoustic.yml");

// Convert the Config config to IndexCalculateConfig class and merge in the unnecesary parameters.
//Config configuration = Yaml.Deserialise(configPath);
//IndexCalculateConfig config = IndexCalculateConfig.GetConfig(configuration, false);

// because of difficulties in dealing with Config config files, just edit the text file!!!!!
var configLines = File.ReadAllLines(configPath.FullName);
configLines[configLines.IndexOf(x => x.StartsWith("IndexCalculationDuration: "))] = "IndexCalculationDuration: 15.0";
Expand All @@ -190,7 +174,7 @@ public void TestAnalyzeSr64000Recording()

// the is the only octave scale currently functioning for IndexCalculate class
configLines[configLines.IndexOf(x => x.StartsWith("FrameLength"))] = $"FrameLength: {freqScale.WindowSize}";
configLines[configLines.IndexOf(x => x.StartsWith("ResampleRate: "))] = "ResampleRate: 64000";
configLines[configLines.IndexOf(x => x.StartsWith("ResampleRate: "))] = "ResampleRate: 22050";

// write the edited Config file to temporary output directory
var newConfigPath = this.outputDirectory.CombineFile("Towsey.Acoustic.yml");
Expand Down Expand Up @@ -238,7 +222,7 @@ public void TestAnalyzeSr64000Recording()
var array = MatrixTools.GetRow(actualBgn, 0);

Assert.AreEqual(28, actualBgn.RowLength());
Assert.AreEqual(256, array.Length);
Assert.AreEqual(19, array.Length);

// draw array just to check peaks are in correct places - just for debugging purposes
var ldsBgnSpectrumFile = this.outputDirectory.CombineFile("Spectrum2.png");
Expand Down Expand Up @@ -271,17 +255,17 @@ public void TestAnalyzeSr64000Recording()
analysisType: analysisType,
indexSpectrograms: dictionaryOfSpectra);

// test number of images - should now be 23
// test number of images - should now be 20 because not producing ribbons.
listOfFiles = resultsDirectory.EnumerateFiles().ToArray();
pngCount = listOfFiles.Count(f => f.Name.EndsWith(".png"));
Assert.AreEqual(22, pngCount);
Assert.AreEqual(20, pngCount);

var twoMapsImagePath = resultsDirectory.CombineFile(recordingName + "__2Maps.png");
var twoMapsImage = Image.Load<Rgb24>(twoMapsImagePath.FullName);

// image is (7*4) * 652
Assert.AreEqual(28, twoMapsImage.Width);
Assert.AreEqual(652, twoMapsImage.Height);
Assert.AreEqual(178, twoMapsImage.Height);
}

[TestMethod]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class SpectrogramGeneratorTests : GeneratedImageTest<Rgb24>
private const int SpectrogramMel = 118;
private const int Cepstral = 67;
private const int SpectrogramOctave = 157;
private const int RibbonSpectrogram = 110;
private const int SpectrogramAmplitude = 310;

private static readonly Dictionary<SpectrogramImageType, int> All = new Dictionary<SpectrogramImageType, int>()
Expand All @@ -42,6 +43,7 @@ public class SpectrogramGeneratorTests : GeneratedImageTest<Rgb24>
{ SpectrogramImageType.MelScaleSpectrogram, SpectrogramMel },
{ SpectrogramImageType.CepstralSpectrogram, Cepstral },
{ SpectrogramImageType.OctaveScaleSpectrogram, SpectrogramOctave },
{ SpectrogramImageType.RibbonSpectrogram, RibbonSpectrogram },
{ SpectrogramImageType.AmplitudeSpectrogramLocalContrastNormalization, SpectrogramAmplitude },
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ public void TestOfSpectralIndices_OctaveDataReduction()
image.Save(outputImagePath1);

// TEST the BGN SPECTRAL INDEX
Assert.AreEqual(20, spectralIndices.BGN.Length);
Assert.AreEqual(19, spectralIndices.BGN.Length);

var expectedSpectrumFile1 = PathHelper.ResolveAsset("Indices", "BGN_OctaveScale.bin");

Expand All @@ -341,7 +341,7 @@ public void TestOfSpectralIndices_OctaveDataReduction()

var expectedSpectrumFile2 = PathHelper.ResolveAsset("Indices", "CVR_OctaveScale.bin");

//Binary.Serialize(expectedSpectrumFile2, spectralIndices.CVR);
// Binary.Serialize(expectedSpectrumFile2, spectralIndices.CVR);
expectedVector = Binary.Deserialize<double[]>(expectedSpectrumFile2);
CollectionAssert.That.AreEqual(expectedVector, spectralIndices.CVR, AllowedDelta);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/Indices/BGN_OctaveScale.bin
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/Fixtures/Indices/CVR_OctaveScale.bin
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/Fixtures/LongDuration/BgnMatrix.OctaveScale.csv
Git LFS file not shown

0 comments on commit 2b6821f

Please sign in to comment.