Skip to content

Commit

Permalink
Small changes
Browse files Browse the repository at this point in the history
Issue #370 Refactoring and preparing way forsome methods involving dynamic gain.
  • Loading branch information
towsey committed Sep 9, 2020
1 parent b046a12 commit b82cef2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
39 changes: 23 additions & 16 deletions src/AnalysisPrograms/Recognizers/GenericRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public override RecognizerResults Recognize(
//List<AcousticEvent> acousticEvents;
List<EventCommon> spectralEvents;
var plots = new List<Plot>();
SpectrogramStandard sonogram;
SpectrogramStandard spectrogram;

Log.Debug($"Using the {profileName} algorithm... ");
if (profileConfig is CommonParameters parameters)
Expand All @@ -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);
Expand All @@ -191,16 +191,16 @@ 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)
{
//get the array of intensity values minus intensity in side/buffer bands.
double[] decibelArray;
(spectralEvents, decibelArray) = OnebinTrackAlgorithm.GetOnebinTracks(
sonogram,
spectrogram,
wp,
segmentStartOffset);

Expand All @@ -211,7 +211,7 @@ public override RecognizerResults Recognize(
{
double[] decibelArray;
(spectralEvents, decibelArray) = ForwardTrackAlgorithm.GetForwardTracks(
sonogram,
spectrogram,
tp,
segmentStartOffset);

Expand All @@ -222,7 +222,7 @@ public override RecognizerResults Recognize(
{
double[] decibelArray;
(spectralEvents, decibelArray) = OneframeTrackAlgorithm.GetOneFrameTracks(
sonogram,
spectrogram,
cp,
segmentStartOffset);

Expand All @@ -233,7 +233,7 @@ public override RecognizerResults Recognize(
{
double[] decibelArray;
(spectralEvents, decibelArray) = UpwardTrackAlgorithm.GetUpwardTracks(
sonogram,
spectrogram,
vtp,
segmentStartOffset);

Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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
{
Expand All @@ -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
Expand All @@ -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<EventCommon>().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.
Expand Down
8 changes: 5 additions & 3 deletions src/AudioAnalysisTools/Tracks/ForwardTrackAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,13 @@ public static (List<EventCommon> 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<SpectralEvent>();

// Also get the combined decibel array.
var combinedIntensityArray = new double[frameCount];

// The following lines are used only for debug purposes.
Expand All @@ -96,7 +98,7 @@ public static (List<EventCommon> 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++)
Expand Down

0 comments on commit b82cef2

Please sign in to comment.