Example #1
0
        /// <summary>
        /// This method calls IndexCalculateSixOnly.Analysis() to calculate six spectral indices
        /// and then calls ContentSignatures.AnalyzeOneMinute() to obtain a content description derived from those indices and an array of functional templates.
        /// </summary>
        public override AnalysisResult2 Analyze <T>(AnalysisSettings analysisSettings, SegmentSettings <T> segmentSettings)
        {
            // set the start time for the current recording segment. Default is zero.
            var elapsedTimeAtStartOfRecording = segmentSettings.SegmentStartOffset;

            var startMinuteId = (int)Math.Round(elapsedTimeAtStartOfRecording.TotalMinutes);

            var audioFile = segmentSettings.SegmentAudioFile;
            var recording = new AudioRecording(audioFile.FullName);

            // Calculate six spectral indices.
            var segmentResults = IndexCalculateSixOnly.Analysis(
                recording,
                segmentSettings.SegmentStartOffset,
                segmentSettings.Segment.SourceMetadata.SampleRate);

            // DO THE CONTENT DESCRIPTION FOR ONE MINUTE HERE
            // First get acoustic indices for one minute, convert to Dictionary and normalize the values.
            var indicesDictionary = segmentResults.AsArray().ToTwoDimensionalArray(SpectralIndexValuesForContentDescription.CachedSelectors);

            //var indicesDictionary = IndexCalculateSixOnly.ConvertIndicesToDictionary(segmentResults);
            foreach (string key in ContentSignatures.IndexNames)
            {
                var indexBounds      = ContentSignatures.IndexValueBounds[key];
                var indexArray       = indicesDictionary[key];
                var normalisedVector = DataTools.NormaliseInZeroOne(indexArray, indexBounds[0], indexBounds[1]);
                indicesDictionary[key] = normalisedVector;
            }

            // scan templates over one minute of indices to get content description
            var descriptionResultForOneMinute = ContentSignatures.AnalyzeOneMinute(
                this.functionalTemplates,
                this.templatesAsDictionary,
                indicesDictionary.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.GetRow(0)), // this line converts dictionary of one-row matrices to dictionary of arrays.
                startMinuteId);

            // set up the analysis results to return
            var analysisResults = new AnalysisResult2(analysisSettings, segmentSettings, recording.Duration)
            {
                AnalysisIdentifier = this.Identifier,
                SpectralIndices    = new SpectralIndexBase[]
                {
                    // Transfer the spectral index results to AnalysisResults
                    // TODO: consider not returning this value if it is not needed in summarize
                    segmentResults,
                },

                MiscellaneousResults =
                {
                    { nameof(DescriptionResult), descriptionResultForOneMinute },
                },
            };

            analysisResults.SpectralIndices[0].ResultStartSeconds = segmentSettings.SegmentStartOffset.TotalSeconds;

            //spectralIndexBase.ResultStartSeconds >= result.SegmentStartOffset.TotalSeconds,

            return(analysisResults);
        }
Example #2
0
        public static void TestTemplates(FileInfo listOfIndexFiles, FileInfo templatesFile, FileInfo imageOfLdfcSpectrogram)
        {
            var contentDictionary = ContentSignatures.ContentDescriptionOfMultipleRecordingFiles(listOfIndexFiles, templatesFile);

            // Write the results to a csv file
            var outputDirectory = templatesFile.DirectoryName;
            var filePath        = Path.Combine(outputDirectory ?? throw new InvalidOperationException("Output directory does not exist."), "AcousticSignatures.csv");

            FileTools.WriteDictionaryAsCsvFile(contentDictionary, filePath);

            // get content description plots and use to examine score distributions.
            var contentPlots = ContentSignatures.GetPlots(contentDictionary);
            var images       = GraphsAndCharts.DrawPlotDistributions(contentPlots);
            var plotsImage   = ImageTools.CombineImagesVertically(images);
            var path1        = Path.Combine(outputDirectory, "ScoreDistributions.png");

            plotsImage.Save(path1);

            // Attach plots to LDFC spectrogram and write to file
            var imageList = new List <Image <Rgb24> >();

            if (imageOfLdfcSpectrogram != null)
            {
                var ldfcSpectrogram = Image.Load <Rgb24>(imageOfLdfcSpectrogram.FullName);
                imageList.Add(ldfcSpectrogram);
            }

            if (contentPlots != null)
            {
                int plotHeight = 30;
                foreach (var plot in contentPlots)
                {
                    var imageOfPlot = plot.DrawAnnotatedPlot(plotHeight);
                    imageList.Add(imageOfPlot);
                }
            }

            if (imageList.Count != 0)
            {
                var opImage = ImageTools.CombineImagesVertically(imageList);
                var path2   = Path.Combine(outputDirectory, templatesFile.BaseName() + ".TestOfTemplates.png");
                opImage.Save(path2);
            }

            //Console.WriteLine("# Finished test of content description templates");
        }