Skip to content

Commit

Permalink
Refactor code for the recognition of blob events
Browse files Browse the repository at this point in the history
Issue #370 Have yet to check whether unit tests are passing after this change.
  • Loading branch information
towsey authored and atruskie committed Oct 14, 2020
1 parent cbab908 commit ee3707f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 33 deletions.
42 changes: 10 additions & 32 deletions src/AnalysisPrograms/Recognizers/GenericRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,47 +167,25 @@ public override RecognizerResults Recognize(

if (profileConfig is BlobParameters bp)
{
var thresholdArray = bp.DecibelThresholds;
var threshold = thresholdArray[0].Value;

//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(
spectrogram.Data,
bp.MinHertz.Value,
bp.MaxHertz.Value,
bp.BottomHertzBuffer.Value,
bp.TopHertzBuffer.Value,
spectrogram.NyquistFrequency);

// prepare plot of resultant blob decibel array.
var plot = Plot.PreparePlot(decibelArray, $"{profileName} (Blob:db Intensity)", threshold);
plots.Add(plot);

// iii: CONVERT blob decibel SCORES TO ACOUSTIC EVENTS.
// Note: This method does NOT do prior smoothing of the dB array.
var acEvents = AcousticEvent.GetEventsAroundMaxima(
decibelArray,
List<Plot> decibelPlots;
(spectralEvents, decibelPlots) = BlobEvent.GetBlobEvents(
spectrogram,
bp,
segmentStartOffset,
bp.MinHertz.Value,
bp.MaxHertz.Value,
threshold,
TimeSpan.FromSeconds(bp.MinDuration.Value),
TimeSpan.FromSeconds(bp.MaxDuration.Value),
spectrogram.FramesPerSecond,
spectrogram.FBinWidth);
spectralEvents = acEvents.ConvertAcousticEventsToSpectralEvents();
profileName);

plots.AddRange(decibelPlots);
}
else if (profileConfig is OnebinTrackParameters wp)
{
List<Plot> dbPlots;
(spectralEvents, dbPlots) = OnebinTrackAlgorithm.GetOnebinTracks(
List<Plot> decibelPlots;
(spectralEvents, decibelPlots) = OnebinTrackAlgorithm.GetOnebinTracks(
spectrogram,
wp,
segmentStartOffset,
profileName);

plots.AddRange(dbPlots);
plots.AddRange(decibelPlots);
}
else if (profileConfig is ForwardTrackParameters tp)
{
Expand Down
54 changes: 53 additions & 1 deletion src/AudioAnalysisTools/Events/Types/BlobEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ namespace AudioAnalysisTools
{
using System;
using System.Collections.Generic;
using AnalysisPrograms.Recognizers.Base;
using AudioAnalysisTools.DSP;
using AudioAnalysisTools.Events;
using AudioAnalysisTools.Events.Drawing;
using AudioAnalysisTools.Events.Types;
using AudioAnalysisTools.StandardSpectrograms;
using SixLabors.ImageSharp.Processing;
using TowseyLibrary;

/// <summary>
/// Am acoustic event that also includes data about the content identified by the event.
/// An acoustic event that also includes data about the content identified by the event.
/// </summary>
public class BlobEvent : SpectralEvent, IPointData
{
Expand All @@ -22,6 +27,53 @@ public BlobEvent()

public ICollection<ISpectralPoint> Points { get; } = new HashSet<ISpectralPoint>();

public static (List<EventCommon> Events, List<Plot> DecibelPlots) GetBlobEvents(
SpectrogramStandard spectrogram,
BlobParameters bp,
TimeSpan segmentStartOffset,
string profileName)
{
var decibelThresholds = bp.DecibelThresholds;
var spectralEvents = new List<EventCommon>();
var plots = new List<Plot>();

foreach (var threshold in decibelThresholds)
{
//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(
spectrogram.Data,
bp.MinHertz.Value,
bp.MaxHertz.Value,
bp.BottomHertzBuffer.Value,
bp.TopHertzBuffer.Value,
spectrogram.NyquistFrequency);

// prepare plot of resultant blob decibel array.
var plot = Plot.PreparePlot(decibelArray, $"{profileName} (Blobs:{threshold.Value:F0}dB)", threshold.Value);
plots.Add(plot);

// iii: CONVERT blob decibel SCORES TO ACOUSTIC EVENTS.
// Note: This method does NOT do prior smoothing of the dB array.
var acEvents = AcousticEvent.GetEventsAroundMaxima(
decibelArray,
segmentStartOffset,
bp.MinHertz.Value,
bp.MaxHertz.Value,
threshold.Value,
TimeSpan.FromSeconds(bp.MinDuration.Value),
TimeSpan.FromSeconds(bp.MaxDuration.Value),
spectrogram.FramesPerSecond,
spectrogram.FBinWidth);

var events = acEvents.ConvertAcousticEventsToSpectralEvents();
spectralEvents.AddRange(events);
plots.Add(plot);
}

return (spectralEvents, plots);
}

public override void Draw(IImageProcessingContext graphics, EventRenderingOptions options)
{
((IPointData)this).DrawPointsAsFill(graphics, options);
Expand Down

0 comments on commit ee3707f

Please sign in to comment.