Skip to content

Commit

Permalink
Add new filter method to generic filters
Browse files Browse the repository at this point in the history
Issue #370 To filter on event duration during generic post-processing
  • Loading branch information
towsey authored and atruskie committed Oct 14, 2020
1 parent 53e9c4d commit 2b41782
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 6 deletions.
45 changes: 39 additions & 6 deletions src/AnalysisPrograms/Recognizers/GenericRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,25 @@ public override RecognizerResults Recognize(
}
}

// 3: Filter the events for bandwidth in Hertz
var expectedEventBandwidth = postprocessingConfig.Bandwidth.ExpectedBandwidth;
var sd = postprocessingConfig.Bandwidth.BandwidthStandardDeviation;
allResults.NewEvents = EventFilters.FilterOnBandwidth(allResults.NewEvents, expectedEventBandwidth, sd, sigmaThreshold: 3.0);
Log.Debug($"Event count after filtering on bandwidth = {allResults.NewEvents.Count}");
// 3: Filter the events for time duration (seconds)
if (postprocessingConfig.Duration != null)
{
var expectedEventDuration = postprocessingConfig.Duration.ExpectedDuration;
var sdEventDuration = postprocessingConfig.Duration.DurationStandardDeviation;
allResults.NewEvents = EventFilters.FilterOnDuration(allResults.NewEvents, expectedEventDuration, sdEventDuration, sigmaThreshold: 3.0);
Log.Debug($"Event count after filtering on duration = {allResults.NewEvents.Count}");
}

// 4: Filter the events for bandwidth in Hertz
if (postprocessingConfig.Bandwidth != null)
{
var expectedEventBandwidth = postprocessingConfig.Bandwidth.ExpectedBandwidth;
var sdBandwidth = postprocessingConfig.Bandwidth.BandwidthStandardDeviation;
allResults.NewEvents = EventFilters.FilterOnBandwidth(allResults.NewEvents, expectedEventBandwidth, sdBandwidth, sigmaThreshold: 3.0);
Log.Debug($"Event count after filtering on bandwidth = {allResults.NewEvents.Count}");
}

// 4: Filter events on the amount of acoustic activity in their upper and lower neighbourhoods - their buffer zone.
// 5: Filter events on the amount of acoustic activity in their upper and lower neighbourhoods - their buffer zone.
// The idea is that an unambiguous event should have some acoustic space above and below.
// The filter requires that the average acoustic activity in each frame and bin of the upper and lower buffer zones should not exceed the user specified decibel threshold.
var sidebandActivity = postprocessingConfig.SidebandActivity;
Expand Down Expand Up @@ -452,12 +464,33 @@ public class PostProcessingConfig
/// </summary>
public SidebandConfig SidebandActivity { get; set; }

/// <summary>
/// Gets or sets the parameters required to filter events on their duration.
/// </summary>
public DurationConfig Duration { get; set; }

/// <summary>
/// Gets or sets the parameters required to filter events on their bandwidth.
/// </summary>
public BandwidthConfig Bandwidth { get; set; }
}

/// <summary>
/// The next two properties determine filtering of events based on their duration.
/// </summary>
public class DurationConfig
{
/// <summary>
/// Gets or sets a value indicating the Expected duration of an event.
/// </summary>
public double ExpectedDuration { get; set; }

/// <summary>
/// Gets or sets a value indicating the standard deviation of the expected duration.
/// </summary>
public double DurationStandardDeviation { get; set; }
}

/// <summary>
/// The next two properties determine filtering of events based on their bandwidth.
/// </summary>
Expand Down
22 changes: 22 additions & 0 deletions src/AudioAnalysisTools/Events/EventFilters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,28 @@ public static List<EventCommon> FilterOnDuration(List<EventCommon> events, doubl
return outputEvents;
}

/// <summary>
/// Filters lists of spectral events based on their duration.
/// Note: The typical sigma threshold would be 2 to 3 sds.
/// </summary>
/// <param name="events">The list of events.</param>
/// <param name="average">The expected value of the duration.</param>
/// <param name="sd">The standard deviation of the duration.</param>
/// <param name="sigmaThreshold">THe sigma value which determines the max and min thresholds.</param>
/// <returns>The filtered list of events.</returns>
public static List<EventCommon> FilterOnDuration(List<EventCommon> events, double average, double sd, double sigmaThreshold)
{
var minDuration = average - (sd * sigmaThreshold);
if (minDuration < 0.0)
{
throw new Exception("Invalid seconds duration passed to method EventExtentions.FilterOnDuration().");
}

var maxDuration = average + (sd * sigmaThreshold);
var outputEvents = events.Where(ev => ((SpectralEvent)ev).EventDurationSeconds > minDuration && ((SpectralEvent)ev).EventDurationSeconds < maxDuration).ToList();
return outputEvents;
}

/// <summary>
/// Removes composite events from a list of EventCommon that contain more than the specfied number of SpectralEvent components.
/// </summary>
Expand Down

0 comments on commit 2b41782

Please sign in to comment.