public void TestOfSpectralIndices_ICD20()
        {
            //var indexPropertiesConfig = PathHelper.ResolveConfigFile(@"IndexPropertiesConfig.yml");
            var sourceRecording = PathHelper.ResolveAsset(@"Recordings\BAC2_20071008-085040.wav");
            var configFile      = PathHelper.ResolveConfigFile(@"Towsey.Acoustic.yml");

            // var outputDir = this.outputDirectory;
            // Create temp directory to store output
            if (!this.outputDirectory.Exists)
            {
                this.outputDirectory.Create();
            }

            var recording = new AudioRecording(sourceRecording);

            // CHANGE CONFIG PARAMETERS HERE IF REQUIRED
            var indexCalculateConfig = ConfigFile.Deserialize <IndexCalculateConfig>(configFile);

            indexCalculateConfig.IndexCalculationDurationTimeSpan = TimeSpan.FromSeconds(20);

            var results = IndexCalculate.Analysis(
                recording,
                TimeSpan.FromSeconds(40), // assume that this is the third of three 20 second subsegments
                indexCalculateConfig.IndexProperties,
                22050,
                TimeSpan.Zero,
                indexCalculateConfig,
                returnSonogramInfo: true);

            var spectralIndices = results.SpectralIndexValues;

            // TEST the SPECTRAL INDICES
            var resourcesDir         = PathHelper.ResolveAssetPath("Indices");
            var expectedSpectrumFile = new FileInfo(resourcesDir + "\\BGN_ICD20.bin");

            //Binary.Serialize(expectedSpectrumFile, spectralIndices.BGN);
            var expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);

            CollectionAssert.That.AreEqual(expectedVector, spectralIndices.BGN, AllowedDelta);

            expectedSpectrumFile = new FileInfo(resourcesDir + "\\CVR_ICD20.bin");

            //Binary.Serialize(expectedSpectrumFile, spectralIndices.CVR);
            expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);
            CollectionAssert.That.AreEqual(expectedVector, spectralIndices.CVR, AllowedDelta);

            var outputImagePath1 = Path.Combine(this.outputDirectory.FullName, "SpectralIndices_ICD20.png");
            var image            = SpectralIndexValues.CreateImageOfSpectralIndices(spectralIndices);

            image.Save(outputImagePath1);
        }
        public void TestOfSpectralIndices_Octave()
        {
            // create a two-minute artificial recording containing five harmonics.
            int    sampleRate = 64000;
            double duration   = 120; // signal duration in seconds

            int[] harmonics = { 500, 1000, 2000, 4000, 8000 };
            var   recording = DspFilters.GenerateTestRecording(sampleRate, duration, harmonics, WaveType.Sine);

            // cut out one minute from 30 - 90 seconds and incorporate into AudioRecording
            int startSample           = sampleRate * 30; // start two minutes into recording
            int subsegmentSampleCount = sampleRate * 60; // get 60 seconds

            double[] subsamples          = DataTools.Subarray(recording.WavReader.Samples, startSample, subsegmentSampleCount);
            var      wr                  = new Acoustics.Tools.Wav.WavReader(subsamples, 1, 16, sampleRate);
            var      subsegmentRecording = new AudioRecording(wr);

            //var indexPropertiesConfig = PathHelper.ResolveConfigFile(@"IndexPropertiesConfig.yml");
            var configFile = PathHelper.ResolveConfigFile(@"Towsey.Acoustic.yml");

            // Create temp directory to store output
            if (!this.outputDirectory.Exists)
            {
                this.outputDirectory.Create();
            }

            // CHANGE CONFIG PARAMETERS HERE IF REQUIRED
            var indexCalculateConfig = ConfigFile.Deserialize <IndexCalculateConfig>(configFile);

            indexCalculateConfig.FrequencyScale = FreqScaleType.Octave;

            var freqScale = new FrequencyScale(indexCalculateConfig.FrequencyScale);

            indexCalculateConfig.FrameLength = freqScale.WindowSize;

            var results = IndexCalculate.Analysis(
                subsegmentRecording,
                TimeSpan.Zero,
                indexCalculateConfig.IndexProperties,
                sampleRate,
                TimeSpan.Zero,
                indexCalculateConfig,
                returnSonogramInfo: true);

            var spectralIndices = results.SpectralIndexValues;

            // draw the output image of all spectral indices
            var outputImagePath1 = Path.Combine(this.outputDirectory.FullName, "SpectralIndices_Octave.png");
            var image            = SpectralIndexValues.CreateImageOfSpectralIndices(spectralIndices);

            image.Save(outputImagePath1);

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

            var resourcesDir         = PathHelper.ResolveAssetPath("Indices");
            var expectedSpectrumFile = new FileInfo(resourcesDir + "\\BGN_OctaveScale.bin");

            //Binary.Serialize(expectedSpectrumFile, spectralIndices.BGN);
            var expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);

            CollectionAssert.That.AreEqual(expectedVector, spectralIndices.BGN, AllowedDelta);

            expectedSpectrumFile = new FileInfo(resourcesDir + "\\CVR_OctaveScale.bin");

            //Binary.Serialize(expectedSpectrumFile, spectralIndices.CVR);
            expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);
            CollectionAssert.That.AreEqual(expectedVector, spectralIndices.CVR, AllowedDelta);
        }
        public void TestOfSpectralIndices()
        {
            //var indexPropertiesConfig = PathHelper.ResolveConfigFile(@"IndexPropertiesConfig.yml");
            var sourceRecording = PathHelper.ResolveAsset(@"Recordings\BAC2_20071008-085040.wav");
            var configFile      = PathHelper.ResolveConfigFile(@"Towsey.Acoustic.yml");

            // var outputDir = this.outputDirectory;
            var resourcesDir = PathHelper.ResolveAssetPath("Indices");

            if (!this.outputDirectory.Exists)
            {
                this.outputDirectory.Create();
            }

            var indexCalculateConfig = ConfigFile.Deserialize <IndexCalculateConfig>(configFile);

            // CHANGE CONFIG PARAMETERS HERE IF REQUIRED
            //indexCalculateConfig.IndexCalculationDuration = TimeSpan.FromSeconds(20);
            //indexCalculateConfig.SetTypeOfFreqScale("Octave");

            var results = IndexCalculate.Analysis(
                new AudioRecording(sourceRecording),
                TimeSpan.Zero,
                indexCalculateConfig.IndexProperties,
                22050,
                TimeSpan.Zero,
                indexCalculateConfig,
                returnSonogramInfo: true);

            var spectralIndices = results.SpectralIndexValues;

            // TEST the SPECTRAL INDICES
            // After serializing the expected vector and writing to the resources directory, comment the Binary.Serialise line.

            // 1:ACI
            var expectedSpectrumFile = new FileInfo(resourcesDir + "\\ACI.bin");

            //Binary.Serialize(expectedSpectrumFile, spectralIndices.ACI);
            var expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);

            CollectionAssert.That.AreEqual(expectedVector, spectralIndices.ACI, AllowedDelta);

            // 2:BGN
            expectedSpectrumFile = new FileInfo(resourcesDir + "\\BGN.bin");

            // Binary.Serialize(expectedSpectrumFile, spectralIndices.BGN);
            expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);
            CollectionAssert.That.AreEqual(expectedVector, spectralIndices.BGN, AllowedDelta);

            // 3:CVR
            expectedSpectrumFile = new FileInfo(resourcesDir + "\\CVR.bin");

            // Binary.Serialize(expectedSpectrumFile, spectralIndices.CVR);
            expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);
            CollectionAssert.That.AreEqual(expectedVector, spectralIndices.CVR, AllowedDelta);

            // 4:ENT
            expectedSpectrumFile = new FileInfo(resourcesDir + "\\ENT.bin");

            // Binary.Serialize(expectedSpectrumFile, spectralIndices.ENT);
            expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);
            CollectionAssert.That.AreEqual(expectedVector, spectralIndices.ENT, AllowedDelta);

            // 5:EVN
            expectedSpectrumFile = new FileInfo(resourcesDir + "\\EVN.bin");

            // Binary.Serialize(expectedSpectrumFile, spectralIndices.EVN);
            expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);
            CollectionAssert.That.AreEqual(expectedVector, spectralIndices.EVN, AllowedDelta);

            // 6:OSC
            expectedSpectrumFile = new FileInfo(resourcesDir + "\\OSC.bin");

            //Binary.Serialize(expectedSpectrumFile, spectralIndices.OSC);
            expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);
            CollectionAssert.That.AreEqual(expectedVector, spectralIndices.OSC, AllowedDelta);

            // 7:PMN
            expectedSpectrumFile = new FileInfo(resourcesDir + "\\PMN.bin");

            // Binary.Serialize(expectedSpectrumFile, spectralIndices.PMN);
            expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);
            CollectionAssert.That.AreEqual(expectedVector, spectralIndices.PMN, AllowedDelta);

            // 8:RHZ
            expectedSpectrumFile = new FileInfo(resourcesDir + "\\RHZ.bin");

            // Binary.Serialize(expectedSpectrumFile, spectralIndices.RHZ);
            expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);
            CollectionAssert.That.AreEqual(expectedVector, spectralIndices.RHZ, AllowedDelta);

            // 9:RNG
            expectedSpectrumFile = new FileInfo(resourcesDir + "\\RNG.bin");

            // Binary.Serialize(expectedSpectrumFile, spectralIndices.RNG);
            expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);
            CollectionAssert.That.AreEqual(expectedVector, spectralIndices.RNG, AllowedDelta);

            // 10:RPS
            expectedSpectrumFile = new FileInfo(resourcesDir + "\\RPS.bin");

            // Binary.Serialize(expectedSpectrumFile, spectralIndices.RPS);
            expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);
            CollectionAssert.That.AreEqual(expectedVector, spectralIndices.RPS, AllowedDelta);

            // 11:RVT
            expectedSpectrumFile = new FileInfo(resourcesDir + "\\RVT.bin");

            // Binary.Serialize(expectedSpectrumFile, spectralIndices.RVT);
            expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);
            CollectionAssert.That.AreEqual(expectedVector, spectralIndices.RVT, AllowedDelta);

            // 12:SPT
            expectedSpectrumFile = new FileInfo(resourcesDir + "\\SPT.bin");

            // Binary.Serialize(expectedSpectrumFile, spectralIndices.SPT);
            expectedVector = Binary.Deserialize <double[]>(expectedSpectrumFile);
            CollectionAssert.That.AreEqual(expectedVector, spectralIndices.SPT, AllowedDelta);

            var outputImagePath = Path.Combine(this.outputDirectory.FullName, "SpectralIndices.png");
            var image           = SpectralIndexValues.CreateImageOfSpectralIndices(spectralIndices);

            image.Save(outputImagePath);
        }