Skip to content

Commit

Permalink
Add more debug messages
Browse files Browse the repository at this point in the history
Issue #500
Add more feed back to user on what is happening during the three processing steps so user can see what effect changes have during the processing.
Three processing steps are:
1: Detection of events using profiles.
2: Filter profile events using custom code for recogniser.
3: Do postprocessing.
  • Loading branch information
towsey committed Nov 2, 2021
1 parent 72286c2 commit 26a3b2d
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 25 deletions.
6 changes: 5 additions & 1 deletion src/AnalysisPrograms/Recognizers/GenericRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ public static RecognizerResults PostProcessAcousticEvents(
RecognizerResults results,
TimeSpan segmentStartOffset)
{
var eventCount = results.NewEvents.Count;
Log.Debug($"Start Postprocessing {eventCount} Acoustic Events.");
var postprocessingConfig = configuration.PostProcessing;

var postEvents = new List<EventCommon>();
Expand All @@ -415,7 +417,9 @@ public static RecognizerResults PostProcessAcousticEvents(
}
else
{
postEvents = PostProcessingOfSpectralEvents(
Log.Debug($" Postprocessing all {eventCount} events in one group.");

postEvents = EventPostProcessing.PostProcessingOfSpectralEvents(
results.NewEvents,
postprocessingConfig,
results.Sonogram,
Expand Down
21 changes: 14 additions & 7 deletions src/AnalysisPrograms/Recognizers/PhascolarctosCinereusMark3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ namespace AnalysisPrograms.Recognizers
/// reliance on oscillation detection alone produces a range of false-positive detections.
///
/// OBJECTIVES FOR THIS RECOGNIZER MARK 3:
/// The expectation is that detection of additional bellow components (additional to oscillations) will help to reduce false-positives.However this is unlikely to reduce false-negatives.
/// The expectation is that detection of additional bellow components (additional to oscillations) will help to reduce false-positives. However this is unlikely to reduce false-negatives.
///
/// REFERENCE: See following paper for more detail and links to other publications:
/// "Estimating the Active Space of Male Koala Bellows: Propagation of Cues to Size and Identity in a Eucalyptus Forest"
Expand Down Expand Up @@ -184,7 +184,7 @@ public static RecognizerResults RecognizeKoalaBellows(
var postprocessingConfig = configuration.PostProcessing;
if (postprocessingConfig is not null)
{
results = PostProcessAcousticEvents(configuration, results, segmentStartOffset);
results = GenericRecognizer.PostProcessAcousticEvents(configuration, results, segmentStartOffset);
}

//############################# COMMENT OUT THE FOLLOWING LINES IF WANT TO VIEW THE ORIGINAL SPECTROGRAM
Expand All @@ -202,6 +202,9 @@ private static RecognizerResults FilterProfileEvents(RecognizerResults results,
var spectrogram = results.Sonogram;
int count = 0;

Log.Info($"Begin filtering {events.Count} profile events");
LoggedConsole.WriteLine($" Spectrogram = {spectrogram.Data.GetLength(0)} frames X {spectrogram.Data.GetLength(1)} bins.");

// create new list of events
var newList = new List<EventCommon>();

Expand All @@ -224,25 +227,29 @@ private static RecognizerResults FilterProfileEvents(RecognizerResults results,
// s1 and s2 are measures of energy concentration. i.e. 1-entropy.
var s1 = stats.SpectralPeakCount;
var s2 = stats.SpectralEnergyDistribution;
var s3 = stats.SpectralCentroid;
var specCentroid = stats.SpectralCentroid;
var s4 = stats.DominantFrequency;

// var message = $" EVENT{count} starting {start.TotalSeconds:F1}sec: PeakCount {s1}; SpectEnergyDistr {s2:F4}; SpectralCentroid {s3}; DominantFreq {s4}";
var message1 = $" EVENT{count} starting {start.TotalSeconds:F1}sec: PeakCount {s1}; SpectEnergyDistr {s2:F4}; SpectralCentroid {specCentroid}; DominantFreq {s4}";
Log.Info(message1);

if (s3 < 700)
// FIlter on spectral centroid
if (specCentroid <= 700)
{
newList.Add(ev);
var message = $" EVENT{count} starting {start.TotalSeconds:F1}sec ACCEPTED: PeakCount {s1}; SpectralCentroid {s3} < 700; DominantFreq {s4} < 700";
var message = $" Event{count} ACCEPTED: because SpectralCentroid <= 700";
Log.Info(message);
}
else
{
var message = $" EVENT{count} starting {start.TotalSeconds:F1}sec REJECTED: PeakCount {s1}; SpectralCentroid {s3} >= 700; DominantFreq {s4} > 700";
var message = $" Event{count} REJECTED: because SpectralCentroid > 700";
Log.Info(message);
}
}

// return filtered list of events in results.
Log.Info($"Return {newList.Count} profile events");

results.NewEvents = newList;
return results;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,19 @@ public static EventStatistics CalculateEventStatstics(
}

// extract the required acoustic event and calculate the stats.
//LoggedConsole.WriteLine($"Extract frames {startFrame} to {endFrame} ---- Extract bins {bottomBin} to {topBin}");

var eventMatrix = MatrixTools.Submatrix(decibelSpectrogram.Data, startFrame, bottomBin, endFrame, topBin);
CalculateEventStatistics(eventMatrix, hertzBinWidth, spectralTarget, stats);
return stats;
}

public static void CalculateEventStatistics(double[,] eventMatrix, double hertzBinWidth, Interval<double> spectralTarget, EventStatistics stats)
{
// Get the SNR of the event. This is just the max value in the matrix because noise reduced
// ########## DEBUG ONLY
//LoggedConsole.WriteLine($"Matrix = {eventMatrix.GetLength(0)} frames X {eventMatrix.GetLength(1)} bins");

// Get the SNR of the event. This is same as the max value in the matrix because spectrogram is noise reduced
MatrixTools.MinMax(eventMatrix, out _, out double max);
stats.SnrDecibels = max;

Expand All @@ -257,7 +262,7 @@ public static void CalculateEventStatistics(double[,] eventMatrix, double hertzB
var rowAverages = MatrixTools.GetRowAverages(eventMatrix);

// ########## DEBUG ONLY - write array for debugging purposes.
LoggedConsole.WriteLine(DataTools.WriteArrayAsCsvLine(columnAverages, "0.00"));
//LoggedConsole.WriteLine("Freq Bin AVerages: " + DataTools.WriteArrayAsCsvLine(columnAverages, "0.00"));

// calculate the mean and temporal standard deviation in decibels
NormalDist.AverageAndSD(rowAverages, out double mean, out double stddev);
Expand Down
37 changes: 22 additions & 15 deletions src/AudioAnalysisTools/Events/Types/EventPostProcessing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ public static class EventPostProcessing
/// </summary>
/// <param name="newEvents">A list of events before post-processing.</param>
/// <param name="postprocessingConfig">The config file to be used for post-processing.</param>
/// <param name="decibelThreshold">Decibel threshold used to detect the passed events.</param>
/// <param name="spectrogram">A spectrogram of the events.</param>
/// <param name="segmentStartOffset">Time in seconds since beginning of the recording.</param>
/// <returns>A list of events after post-processing.</returns>
Expand All @@ -39,7 +38,8 @@ public static List<EventCommon> PostProcessingOfSpectralEvents(
// Step 4: Remove events whose bandwidth is too small or large.
// Step 5: Remove events that have excessive noise in their side-bands.

Log.DebugFormat($"\nBEFORE post-processing, event count: {0}.", newEvents.Count);
//Log.DebugFormat($"START post-processing Spectral Events, event count: {0}.", newEvents.Count);
Log.DebugFormat($"START post-processing {newEvents.Count} Spectral Events.");

// 1: Combine overlapping events.
// This will be necessary where many small events have been found - possibly because the dB threshold is set low.
Expand Down Expand Up @@ -71,21 +71,28 @@ public static List<EventCommon> PostProcessingOfSpectralEvents(
{
// filter on number of syllables and their periodicity.
var maxComponentCount = sequenceConfig.SyllableMaxCount;
var periodAv = sequenceConfig.ExpectedPeriod;
var periodSd = sequenceConfig.PeriodStandardDeviation;
var minPeriod = periodAv - (SigmaThreshold * periodSd);
var maxPeriod = periodAv + (SigmaThreshold * periodSd);
Log.Debug($"FILTER ON SYLLABLE SEQUENCE");
Log.Debug($" Expected Syllable Sequence: max={maxComponentCount}, Period: av={periodAv}s, sd={periodSd:F3} min={minPeriod:F3}s, max={maxPeriod:F3}s");
if (minPeriod <= 0.0)
if (maxComponentCount < 2)
{
Log.Error(
$"Expected period={periodAv};sd={periodSd:F3} => min={minPeriod:F3}s;max={maxPeriod:F3}",
new Exception("FATAL ERROR: This combination of values is invalid => negative minimum value."));
Log.Error($"{maxComponentCount} is an invalid number of components for a sequence. Must be >2. Abort filtering of sequences.");
}
else
{
var periodAv = sequenceConfig.ExpectedPeriod;
var periodSd = sequenceConfig.PeriodStandardDeviation;
var minPeriod = periodAv - (SigmaThreshold * periodSd);
var maxPeriod = periodAv + (SigmaThreshold * periodSd);
Log.Debug($"FILTER SYLLABLE SEQUENCES");
Log.Debug($" Expected Syllable Sequence: Max syllables={maxComponentCount}, Period: av={periodAv}s, sd={periodSd:F3} min={minPeriod:F3}s, max={maxPeriod:F3}s");
if (minPeriod <= 0.0)
{
Log.Error(
$"Expected period={periodAv};sd={periodSd:F3} => min={minPeriod:F3}s;max={maxPeriod:F3}",
new Exception("FATAL ERROR: This combination of values is invalid => negative minimum value."));
}

newEvents = EventFilters.FilterEventsOnSyllableCountAndPeriodicity(newEvents, maxComponentCount, periodAv, periodSd);
Log.Debug($" Event count after filtering on periodicity = {newEvents.Count}");
}

newEvents = EventFilters.FilterEventsOnSyllableCountAndPeriodicity(newEvents, maxComponentCount, periodAv, periodSd);
Log.Debug($" Event count after filtering on periodicity = {newEvents.Count}");
}
}

Expand Down

0 comments on commit 26a3b2d

Please sign in to comment.