Skip to content

Commit

Permalink
Create new unit test for spectral data reduction
Browse files Browse the repository at this point in the history
Issue #332 Also have one change required in IndexCalculateTests.
This may cause the test to fail.
  • Loading branch information
towsey committed Aug 5, 2020
1 parent c16ab0d commit 0347e3f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
44 changes: 44 additions & 0 deletions tests/Acoustics.Test/AudioAnalysisTools/DSP/FrequencyScaleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,50 @@ public void OctaveFrequencyScale2()
Assert.AreEqual(310, image.Height);
}

/// <summary>
/// Test of frequency scale used for spectral data reduction.
/// Reduces a 256 spectrum to 20 value vector.
/// Check it on artificial spectrum with three tones.
/// By default, the split between linear and octave is at 1000 Hz.
/// </summary>
[TestMethod]
public void TestSpectralReductionScale()
{
var fst = FreqScaleType.OctaveDataReduction;
var freqScale = new FrequencyScale(fst);
Assert.AreEqual(freqScale.ScaleType, FreqScaleType.OctaveDataReduction);

// test contents of the octave bin bounds matrix.
Assert.AreEqual(20, freqScale.BinBounds.GetLength(0));

var expectedBinBounds = new[,]
{
{ 0, 0 }, { 1, 258 }, { 2, 517 }, { 3, 775 }, { 4, 1034 }, { 5, 1292 }, { 6, 1550 }, { 47, 2024 },
{ 54, 2326 }, { 62, 2670 }, { 71, 3058 }, { 81, 3488 }, { 93, 4005 }, { 107, 4608 }, { 123, 5297 }, { 141, 6072 },
{ 162, 6977 }, { 186, 8010 }, { 214, 9216 }, { 255, 10982 },
};

Assert.That.MatricesAreEqual(expectedBinBounds, freqScale.BinBounds);

// generate pure tone spectrum.
double[] linearSpectrum = new double[256];
linearSpectrum[0] = 1.0;
linearSpectrum[128] = 1.0;
linearSpectrum[255] = 1.0;

double[] octaveSpectrum = OctaveFreqScale.OctaveSpectrum(freqScale.BinBounds, linearSpectrum);

Assert.AreEqual(20, octaveSpectrum.Length);
Assert.AreEqual(1.0, octaveSpectrum[0]);
Assert.AreEqual(0.0, octaveSpectrum[1]);
Assert.AreEqual(0.0, octaveSpectrum[13]);
Assert.AreEqual(0.042483660130718942, octaveSpectrum[14]);
Assert.AreEqual(0.014245014245014251, octaveSpectrum[15]);
Assert.AreEqual(0.0, octaveSpectrum[16]);
Assert.AreEqual(0.0, octaveSpectrum[18]);
Assert.AreEqual(0.047619047619047616, octaveSpectrum[19], 0.000001);
}

/// <summary>
/// Tests linear freq scale using an artificial recording containing five sine waves.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public void TestOfSpectralIndices_Octave()

// CHANGE CONFIG PARAMETERS HERE IF REQUIRED
var indexCalculateConfig = ConfigFile.Deserialize<AcousticIndices.AcousticIndicesConfig>(configFile);
indexCalculateConfig.FrequencyScale = FreqScaleType.Octave;
indexCalculateConfig.FrequencyScale = FreqScaleType.LinearOctaveStandard;

var freqScale = new FrequencyScale(indexCalculateConfig.FrequencyScale);
indexCalculateConfig.FrameLength = freqScale.WindowSize;
Expand Down

0 comments on commit 0347e3f

Please sign in to comment.