Skip to content

Commit

Permalink
Rework Ocatve scale when calculating acoustic indices
Browse files Browse the repository at this point in the history
Issue #332 Previously calculated an octave scale that was once used for a high SR marine recording. Now implement an octave scale that is used for spectral data reduction. This will be used subsequently for content description.
  • Loading branch information
towsey committed Aug 13, 2020
1 parent 28832b5 commit b423044
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 33 deletions.
23 changes: 8 additions & 15 deletions src/AudioAnalysisTools/Indices/IndexCalculate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,18 @@ public static IndexCalculateResult Analysis(
//dspOutput1.NyquistBin = dspOutput1.AmplitudeSpectrogram.GetLength(1) - 1;
//dspOutput1.FreqBinWidth = sampleRate / (double)dspOutput1.AmplitudeSpectrogram.GetLength(1) / 2;

// Linear or Octave or Mel frequency scale? Set Linear as default.
// Linear or Octave or Mel frequency scale?
// Set Linear as default.
var freqScale = new FrequencyScale(nyquist: nyquist, frameSize: frameSize, hertzGridInterval: 1000);
var freqScaleType = config.FrequencyScale;
bool octaveScale = freqScaleType == FreqScaleType.OctaveStandard || freqScaleType == FreqScaleType.OctaveCustom;
bool octaveScale = freqScaleType == FreqScaleType.OctaveDataReduction;
bool melScale = freqScaleType == FreqScaleType.Mel;
if (octaveScale)
{
throw new Exception("Octave scales not currently implemented.");

//TODO This is to be worked on.
// only allow one octave scale at the moment - for Jasco marine recordings.
// ASSUME fixed Occtave scale - USEFUL ONLY FOR JASCO 64000sr MARINE RECORDINGS
// If you wish to use other octave scale types then need to put in the config file and and set up recovery here.
freqScale = new FrequencyScale(freqScaleType);
// Only allow one octave scale at the moment, OctaveDataReduction.
// This is used to produced a reduced set of indices for content recognition.
// TODO: Much work required to generalise this for other Octave scales. Need to put scale type in the config file and and set up recovery here.
freqScale = new FrequencyScale(FreqScaleType.OctaveDataReduction);

// Recalculate the spectrogram according to octave scale. This option works only when have high SR recordings.
dspOutput1.AmplitudeSpectrogram = OctaveFreqScale.AmplitudeSpectra(
Expand Down Expand Up @@ -316,13 +314,8 @@ public static IndexCalculateResult Analysis(
if (octaveScale)
{
// the above frequency bin bounds do not apply with octave scale. Need to recalculate them suitable for Octave scale recording.
lowFreqBound = freqScale.LinearBound;
lowerBinBound = freqScale.GetBinIdForHerzValue(lowFreqBound);

midFreqBound = 8000; // This value appears suitable for Jasco Marine recordings. Not much happens above 8kHz.

//middleBinBound = freqScale.GetBinIdForHerzValue(midFreqBound);
middleBinBound = freqScale.GetBinIdInReducedSpectrogramForHerzValue(midFreqBound);
middleBinBound = freqScale.GetBinIdForHerzValue(midFreqBound);
midBandBinCount = middleBinBound - lowerBinBound + 1;
}

Expand Down
8 changes: 7 additions & 1 deletion src/AudioAnalysisTools/Indices/IndexCalculateConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,13 @@ public FreqScaleType FrequencyScale
switch (value)
{
case FreqScaleType.Linear:
case FreqScaleType.OctaveStandard:
//case FreqScaleType.OctaveStandard:
// this.frequencyScale = value;
// break;
//case FreqScaleType.OctaveCustom:
// this.frequencyScale = value;
// break;
case FreqScaleType.OctaveDataReduction:
this.frequencyScale = value;
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,14 +279,14 @@ public void TestOfSpectralIndices_ICD20()
}

/// <summary>
/// Test index calculation when the Hertz FreqScaleType = Octave.
/// Only test the BGN spectral index as reasonable to assume that the rest will work if ACI works.
/// Test index calculation when the Hertz FreqScaleType = OctaveDataReduction.
/// Only test the BGN and CVR spectral index as reasonable to assume that the rest will work if these work.
/// </summary>
[TestMethod]
public void TestOfSpectralIndices_Octave()
public void TestOfSpectralIndices_OctaveDataReduction()
{
// create a two-minute artificial recording containing five harmonics.
int sampleRate = 64000;
int sampleRate = 22050;
double duration = 120; // signal duration in seconds
int[] harmonics = { 500, 1000, 2000, 4000, 8000 };
var recording = DspFilters.GenerateTestRecording(sampleRate, duration, harmonics, WaveType.Sine);
Expand All @@ -309,15 +309,10 @@ public void TestOfSpectralIndices_Octave()

// CHANGE CONFIG PARAMETERS HERE IF REQUIRED
var indexCalculateConfig = ConfigFile.Deserialize<AcousticIndices.AcousticIndicesConfig>(configFile);
indexCalculateConfig.FrequencyScale = FreqScaleType.OctaveStandard;
int nyquist = sampleRate / 2;
int frameSize = 512;
int linearBound = 1000;
int octaveToneCount = 1; // this set automatically
int gridInterval = 1000;
var freqScale = new FrequencyScale(FreqScaleType.OctaveStandard, nyquist, frameSize, linearBound, octaveToneCount, gridInterval);
indexCalculateConfig.FrameLength = 512;

indexCalculateConfig.FrameLength = freqScale.WindowSize;
//WARNING: Currently only one octave scale is available.
indexCalculateConfig.FrequencyScale = FreqScaleType.OctaveDataReduction;

var results = IndexCalculate.Analysis(
subsegmentRecording,
Expand All @@ -336,14 +331,16 @@ public void TestOfSpectralIndices_Octave()
image.Save(outputImagePath1);

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

//Binary.Serialize(expectedSpectrumFile, spectralIndices.BGN);
var expectedVector = Binary.Deserialize<double[]>(PathHelper.ResolveAsset("Indices", "BGN_OctaveScale.bin"));
var expectedSpectrumFile1 = PathHelper.ResolveAsset("Indices", "BGN_OctaveScale.bin");
//Binary.Serialize(expectedSpectrumFile1, spectralIndices.BGN);
var expectedVector = Binary.Deserialize<double[]>(expectedSpectrumFile1);
CollectionAssert.That.AreEqual(expectedVector, spectralIndices.BGN, AllowedDelta);

//Binary.Serialize(expectedSpectrumFile, spectralIndices.CVR);
expectedVector = Binary.Deserialize<double[]>(PathHelper.ResolveAsset("Indices", "CVR_OctaveScale.bin"));
var expectedSpectrumFile2 = PathHelper.ResolveAsset("Indices", "CVR_OctaveScale.bin");
//Binary.Serialize(expectedSpectrumFile2, spectralIndices.CVR);
expectedVector = Binary.Deserialize<double[]>(expectedSpectrumFile2);
CollectionAssert.That.AreEqual(expectedVector, spectralIndices.CVR, AllowedDelta);
}
}
Expand Down

0 comments on commit b423044

Please sign in to comment.