From 631ec94787e0ea414068c353286eb7901447954f Mon Sep 17 00:00:00 2001 From: towsey Date: Sat, 17 Oct 2020 16:45:57 +1100 Subject: [PATCH] Update CompositeEvent.cs 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. --- .../Events/Types/CompositeEvent.cs | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/AudioAnalysisTools/Events/Types/CompositeEvent.cs b/src/AudioAnalysisTools/Events/Types/CompositeEvent.cs index b12ab160a..8d7e1f157 100644 --- a/src/AudioAnalysisTools/Events/Types/CompositeEvent.cs +++ b/src/AudioAnalysisTools/Events/Types/CompositeEvent.cs @@ -343,6 +343,64 @@ public static SpectralEvent OverlapsEventInList(SpectralEvent anEvent, List + /// Removes from a list of events, those events that are enclosed by another event in the list. + /// Returns a reduced list. + /// + public static List RemoveEnclosedEvents(List events) + { + if (events.Count < 2) + { + return events.Cast().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().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); + } + /* /// /// This method not currently called but is POTENTIALLY USEFUL.