コード例 #1
0
        public void TestOfSummaryIndices()
        {
            var sourceRecording       = PathHelper.ResolveAsset(@"Recordings\BAC2_20071008-085040.wav");
            var configFile            = PathHelper.ResolveConfigFile(@"Towsey.Acoustic.yml");
            var indexPropertiesConfig = PathHelper.ResolveConfigFile(@"IndexPropertiesConfig.yml");

            // var outputDir = this.outputDirectory;
            // Create temp directory to store output
            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 summaryIndices = results.SummaryIndexValues;

            Assert.AreEqual(0.6793287, summaryIndices.AcousticComplexity, AllowedDelta);
            Assert.AreEqual(0.484520, summaryIndices.Activity, AllowedDelta);
            Assert.AreEqual(0.000000, summaryIndices.AvgEntropySpectrum, AllowedDelta);
            Assert.AreEqual(-30.946519, summaryIndices.AvgSignalAmplitude, AllowedDelta);
            Assert.AreEqual(11.533420, summaryIndices.AvgSnrOfActiveFrames, AllowedDelta);
            Assert.AreEqual(-39.740775, summaryIndices.BackgroundNoise, AllowedDelta);
            Assert.AreEqual(21, summaryIndices.ClusterCount);
            Assert.AreEqual(0.153191, summaryIndices.EntropyOfAverageSpectrum, AllowedDelta);
            Assert.AreEqual(0.301929, summaryIndices.EntropyOfCoVSpectrum, AllowedDelta);
            Assert.AreEqual(0.260999, summaryIndices.EntropyOfPeaksSpectrum, AllowedDelta);
            Assert.AreEqual(0.522080, summaryIndices.EntropyOfVarianceSpectrum, AllowedDelta);
            Assert.AreEqual(0.0, summaryIndices.EntropyPeaks, AllowedDelta);
            Assert.AreEqual(2.0, summaryIndices.EventsPerSecond, AllowedDelta);
            Assert.AreEqual(0.140306, summaryIndices.HighFreqCover, AllowedDelta);
            Assert.AreEqual(0.137873, summaryIndices.MidFreqCover, AllowedDelta);
            Assert.AreEqual(0.055341, summaryIndices.LowFreqCover, AllowedDelta);
            Assert.AreEqual(0.957433, summaryIndices.Ndsi, AllowedDelta);
            Assert.AreEqual(27.877206, summaryIndices.Snr, AllowedDelta);
            Assert.AreEqual(6.240310, summaryIndices.SptDensity, AllowedDelta);
            Assert.AreEqual(0, summaryIndices.ResultStartSeconds);
            Assert.AreEqual(0.162216, summaryIndices.TemporalEntropy, AllowedDelta);
            Assert.AreEqual(401, summaryIndices.ThreeGramCount, AllowedDelta);
        }
コード例 #2
0
        private IList <IndexData> CalculateData(IndexDefinitionInfo define)
        {
            //获取算法

            var contextOld = GetTechContext(define);

            var contextNew           = "";
            IList <IndexData> result = IndexCalculate.GetIndexData(define.algorithm_script, define.parameter, contextOld, out contextNew, PriceList);

            SaveTechContext(contextNew, define);

            return(result);
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        public void Execute(IJobExecutionContext context)
        {
            LoggingExtensions.Logging.Log.InitializeWith <LoggingExtensions.log4net.Log4NetLog>();

            var cateService  = new StockCategoryService();
            var stockService = new StockService();

            string type = this.CycleType;

            if (string.IsNullOrEmpty(type))
            {
                this.Log().Error("周期类型cycle参数为设置");
                return;
            }
            var cycleType = (TechCycle)Enum.Parse(typeof(TechCycle), type, true);

            string            code      = string.IsNullOrEmpty(this.ObjectCode) ? "0600015" : this.ObjectCode;
            IList <PriceInfo> priceList = stockService.GetPriceInfo(code, cycleType);

            this.Log().Error("开始计算股票技术指数");

            string algorithm_script = File.ReadAllText(string.Format(@"Script\{0}.js", this.TechName.Trim()));

            string contextNew = "";

            IndexCalculate.IsDebugg = true;

            //计算技术数据
            IList <IndexData> result = IndexCalculate.GetIndexData(algorithm_script, "{}", "", out contextNew, priceList);

            //技术形态
            var stateResult = IndexCalculate.GetState(algorithm_script, result);

            var tagReuslt = IndexCalculate.GetTag(algorithm_script, result);

            this.Log().Info("计算结果:" + this.TechName + ":" + stateResult);
            this.Log().Error("计算结束");
        }
コード例 #5
0
        public static IndexCalculateResult[] CalculateIndicesInSubsegments(
            AudioRecording recording,
            TimeSpan segmentStartOffset,
            TimeSpan segmentDuration,
            TimeSpan indexCalculationDuration,
            Dictionary <string, IndexProperties> indexProperties,
            int sampleRateOfOriginalAudioFile,
            IndexCalculateConfig config)
        {
            if (recording.WavReader.Channels > 1)
            {
                throw new InvalidOperationException(
                          @"A multi-channel recording MUST be mixed down to MONO before calculating acoustic indices!");
            }

            double recordingDuration  = recording.Duration.TotalSeconds;
            double subsegmentDuration = indexCalculationDuration.TotalSeconds;

            // intentional possible null ref, throw if not null
            double segmentDurationSeconds = segmentDuration.TotalSeconds;
            double audioCuttingError      = subsegmentDuration - segmentDurationSeconds;

            // using the expected duration, each call to analyze will always produce the same number of results
            // round, we expect perfect numbers, warn if not
            double       subsegmentsInSegment = segmentDurationSeconds / subsegmentDuration;
            int          subsegmentCount      = (int)Math.Round(segmentDurationSeconds / subsegmentDuration);
            const double warningThreshold     = 0.01; // 1%
            double       fraction             = subsegmentsInSegment - subsegmentCount;

            if (Math.Abs(fraction) > warningThreshold)
            {
                Log.Warn(
                    string.Format(
                        "The IndexCalculationDuration ({0}) does not fit well into the provided segment ({1}). This means a partial result has been {3}, {2} results will be calculated",
                        subsegmentDuration,
                        segmentDurationSeconds,
                        subsegmentCount,
                        fraction >= 0.5 ? "added" : "removed"));
            }

            Log.Trace(subsegmentCount + " sub segments will be calculated");

            var indexCalculateResults = new IndexCalculateResult[subsegmentCount];

            // calculate indices for each subsegment
            for (int i = 0; i < subsegmentCount; i++)
            {
                var subsegmentOffset     = segmentStartOffset + TimeSpan.FromSeconds(i * subsegmentDuration);
                var indexCalculateResult = IndexCalculate.Analysis(
                    recording,
                    subsegmentOffset,
                    indexProperties,
                    sampleRateOfOriginalAudioFile,
                    segmentStartOffset,
                    config);

                indexCalculateResults[i] = indexCalculateResult;
            }

            return(indexCalculateResults);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        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);
        }
コード例 #8
0
        private string CalculateTag(IndexDefinitionInfo define)
        {
            var result = IndexCalculate.GetTag(define.algorithm_script, GetLastIndexData(define));

            return(result);
        }
コード例 #9
0
        private IndexState CalculateState(IndexDefinitionInfo define)
        {
            var result = IndexCalculate.GetState(define.algorithm_script, GetLastIndexData(define));

            return(result);
        }