Skip to content

Commit

Permalink
Update CompositeEvent.cs
Browse files Browse the repository at this point in the history
Issue #390 Add another means to filter events by removing an event which is entirely enclosed by another event. This circumstance now arises due to the new way of using decibel thresholds.
  • Loading branch information
towsey authored and atruskie committed Nov 1, 2020
1 parent aecccbc commit 631ec94
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions src/AudioAnalysisTools/Events/Types/CompositeEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,64 @@ public static SpectralEvent OverlapsEventInList(SpectralEvent anEvent, List<Spec
return null;
}

/// <summary>
/// Removes from a list of events, those events that are enclosed by another event in the list.
/// Returns a reduced list.
/// </summary>
public static List<EventCommon> RemoveEnclosedEvents(List<EventCommon> events)
{
if (events.Count < 2)
{
return events.Cast<EventCommon>().ToList();
}

for (int i = events.Count - 1; i >= 0; i--)
{
for (int j = i - 1; j >= 0; j--)
{
var a = events[i] as SpectralEvent;
var b = events[j] as SpectralEvent;

(SpectralEvent evAi, SpectralEvent evBj) = EnclosedEvents(a, b);

if (evAi != null && evBj == null)
{
events[j] = evAi;
events.RemoveAt(i);
break;
}
else
if (evAi == null && evBj != null)
{
events[j] = evBj;
events.RemoveAt(i);
break;
}
}
}

return events.Cast<EventCommon>().ToList();
}

public static (SpectralEvent EvAi, SpectralEvent EvBj) EnclosedEvents(SpectralEvent a, SpectralEvent b)
{
bool eventAEnclosedInTime = a.EventStartSeconds >= b.EventStartSeconds && a.EventEndSeconds <= b.EventEndSeconds;
bool eventAEnclosedInFreq = a.LowFrequencyHertz >= b.LowFrequencyHertz && a.HighFrequencyHertz <= b.HighFrequencyHertz;
if (eventAEnclosedInTime && eventAEnclosedInTime)
{
return (null, b);
}

bool eventBEnclosedInTime = a.EventStartSeconds <= b.EventStartSeconds && a.EventEndSeconds >= b.EventEndSeconds;
bool eventBEnclosedInFreq = a.LowFrequencyHertz <= b.LowFrequencyHertz && a.HighFrequencyHertz >= b.HighFrequencyHertz;
if (eventAEnclosedInTime && eventAEnclosedInTime)
{
return (a, null);
}

return (a, b);
}

/*
/// <summary>
/// This method not currently called but is POTENTIALLY USEFUL.
Expand Down

0 comments on commit 631ec94

Please sign in to comment.