From 5db186e746783b44332f9860ab1d85e72dc8f22e Mon Sep 17 00:00:00 2001 From: towsey Date: Wed, 9 Sep 2020 17:40:15 +1000 Subject: [PATCH] Small changes Issue #370 Refactoring and preparing way forsome methods involving dynamic gain. --- .../Recognizers/GenericRecognizer.cs | 39 +++++++++++-------- .../Tracks/ForwardTrackAlgorithm.cs | 8 ++-- 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/AnalysisPrograms/Recognizers/GenericRecognizer.cs b/src/AnalysisPrograms/Recognizers/GenericRecognizer.cs index 1ba5d461a..7b7d25628 100644 --- a/src/AnalysisPrograms/Recognizers/GenericRecognizer.cs +++ b/src/AnalysisPrograms/Recognizers/GenericRecognizer.cs @@ -150,7 +150,7 @@ public override RecognizerResults Recognize( //List acousticEvents; List spectralEvents; var plots = new List(); - SpectrogramStandard sonogram; + SpectrogramStandard spectrogram; Log.Debug($"Using the {profileName} algorithm... "); if (profileConfig is CommonParameters parameters) @@ -163,19 +163,19 @@ public override RecognizerResults Recognize( || profileConfig is UpwardTrackParameters || profileConfig is OneframeTrackParameters) { - sonogram = new SpectrogramStandard(ParametersToSonogramConfig(parameters), audioRecording.WavReader); + spectrogram = new SpectrogramStandard(ParametersToSonogramConfig(parameters), audioRecording.WavReader); if (profileConfig is BlobParameters bp) { //get the array of intensity values minus intensity in side/buffer bands. //i.e. require silence in side-bands. Otherwise might simply be getting part of a broader band acoustic event. var decibelArray = SNR.CalculateFreqBandAvIntensityMinusBufferIntensity( - sonogram.Data, + spectrogram.Data, bp.MinHertz.Value, bp.MaxHertz.Value, bp.BottomHertzBuffer.Value, bp.TopHertzBuffer.Value, - sonogram.NyquistFrequency); + spectrogram.NyquistFrequency); // prepare plot of resultant blob decibel array. var plot = PreparePlot(decibelArray, $"{profileName} (Blob:db Intensity)", bp.DecibelThreshold.Value); @@ -191,8 +191,8 @@ public override RecognizerResults Recognize( bp.DecibelThreshold.Value, TimeSpan.FromSeconds(bp.MinDuration.Value), TimeSpan.FromSeconds(bp.MaxDuration.Value), - sonogram.FramesPerSecond, - sonogram.FBinWidth); + spectrogram.FramesPerSecond, + spectrogram.FBinWidth); spectralEvents = acEvents.ConvertAcousticEventsToSpectralEvents(); } else if (profileConfig is OnebinTrackParameters wp) @@ -200,7 +200,7 @@ public override RecognizerResults Recognize( //get the array of intensity values minus intensity in side/buffer bands. double[] decibelArray; (spectralEvents, decibelArray) = OnebinTrackAlgorithm.GetOnebinTracks( - sonogram, + spectrogram, wp, segmentStartOffset); @@ -211,7 +211,7 @@ public override RecognizerResults Recognize( { double[] decibelArray; (spectralEvents, decibelArray) = ForwardTrackAlgorithm.GetForwardTracks( - sonogram, + spectrogram, tp, segmentStartOffset); @@ -222,7 +222,7 @@ public override RecognizerResults Recognize( { double[] decibelArray; (spectralEvents, decibelArray) = OneframeTrackAlgorithm.GetOneFrameTracks( - sonogram, + spectrogram, cp, segmentStartOffset); @@ -233,7 +233,7 @@ public override RecognizerResults Recognize( { double[] decibelArray; (spectralEvents, decibelArray) = UpwardTrackAlgorithm.GetUpwardTracks( - sonogram, + spectrogram, vtp, segmentStartOffset); @@ -245,10 +245,10 @@ public override RecognizerResults Recognize( double[] decibelMaxArray; double[] harmonicIntensityScores; (spectralEvents, decibelMaxArray, harmonicIntensityScores) = HarmonicParameters.GetComponentsWithHarmonics( - sonogram, + spectrogram, hp.MinHertz.Value, hp.MaxHertz.Value, - sonogram.NyquistFrequency, + spectrogram.NyquistFrequency, hp.DecibelThreshold.Value, hp.DctThreshold.Value, hp.MinDuration.Value, @@ -263,7 +263,7 @@ public override RecognizerResults Recognize( else if (profileConfig is OscillationParameters op) { Oscillations2012.Execute( - sonogram, + spectrogram, op.MinHertz.Value, op.MaxHertz.Value, op.DctDuration, @@ -313,10 +313,10 @@ public override RecognizerResults Recognize( NoiseReductionType = ac.NoiseReductionType, NoiseReductionParameter = ac.NoiseReductionParameter, }; - sonogram = new SpectrogramStandard(config, audioRecording.WavReader); + spectrogram = new SpectrogramStandard(config, audioRecording.WavReader); // GET THIS TO RETURN BLOB EVENTS. - spectralEvents = Aed.CallAed(sonogram, ac, segmentStartOffset, audioRecording.Duration).ToList(); + spectralEvents = Aed.CallAed(spectrogram, ac, segmentStartOffset, audioRecording.Duration).ToList(); } else { @@ -328,7 +328,7 @@ public override RecognizerResults Recognize( allResults.Plots.AddRange(plots); // effectively keeps only the *last* sonogram produced - allResults.Sonogram = sonogram; + allResults.Sonogram = spectrogram; Log.Debug($"{profileName} event count = {spectralEvents.Count}"); // DEBUG PURPOSES COMMENT NEXT LINE @@ -347,6 +347,13 @@ public override RecognizerResults Recognize( Log.Debug($"Event count after combining overlapped events = {allResults.NewEvents.Count}"); } + // ########################## NEW WORK - FILTER EVENTS USING DYNAMIC GAIN + //allResults.NewEvents = EventExtentions.FilterEventsUsingDynamicGain( + // allResults.NewEvents.Cast().ToList(), + // allResults.Sonogram.Data, + // decibelThreshold); + //Log.Debug($"Event count after adjusting gain = {allResults.NewEvents.Count}"); + // 2: Combine proximal events, that is, events that may be a sequence of syllables in the same strophe. // Can also use this parameter to combine events that are in the upper or lower neighbourhood. // Such combinations will increase bandwidth of the event and this property can be used later to weed out unlikely events. diff --git a/src/AudioAnalysisTools/Tracks/ForwardTrackAlgorithm.cs b/src/AudioAnalysisTools/Tracks/ForwardTrackAlgorithm.cs index 35f56c385..9bffbf499 100644 --- a/src/AudioAnalysisTools/Tracks/ForwardTrackAlgorithm.cs +++ b/src/AudioAnalysisTools/Tracks/ForwardTrackAlgorithm.cs @@ -69,11 +69,13 @@ public static (List Events, double[] CombinedIntensity) GetForwardT } } + // Get a list of Tracks var tracks = GetForwardTracks(peaks, minDuration, maxDuration, decibelThreshold, converter); - // initialise tracks as events and get the combined intensity array. - // list of accumulated acoustic events + // Initialise each track as an event and store it in a list of acoustic events var events = new List(); + + // Also get the combined decibel array. var combinedIntensityArray = new double[frameCount]; // The following lines are used only for debug purposes. @@ -96,7 +98,7 @@ public static (List Events, double[] CombinedIntensity) GetForwardT events.Add(ae); - // fill the intensity array + // fill the intensity array with decibel values var startRow = converter.FrameFromStartTime(track.StartTimeSeconds); var amplitudeTrack = track.GetAmplitudeOverTimeFrames(); for (int i = 0; i < amplitudeTrack.Length; i++)