diff --git a/src/AudioAnalysisTools/Events/EventExtentions.cs b/src/AudioAnalysisTools/Events/EventExtentions.cs index 88e5144da..7b7f70086 100644 --- a/src/AudioAnalysisTools/Events/EventExtentions.cs +++ b/src/AudioAnalysisTools/Events/EventExtentions.cs @@ -381,6 +381,36 @@ public static List FilterEventsOnCompositeContent( return filteredEvents; } + /// + /// Removes composite events from a list of EventCommon where the component syllables do not have the correct periodicity. + /// + public static List FilterEventsOnSyllablePeriodicity( + List events, + double expectedPeriod, + double periodSd) + { + var filteredEvents = new List(); + + foreach (var ev in events) + { + if (ev is CompositeEvent) + { + var actualPeriodicity = ((CompositeEvent)ev).CalculatePeriodicity(); + var minAllowedPeriodicity = expectedPeriod - (3 * periodSd); + var maxAllowedPeriodicity = expectedPeriod + (3 * periodSd); + if (actualPeriodicity < minAllowedPeriodicity || actualPeriodicity > maxAllowedPeriodicity) + { + // ignore composite events which do not have the correct periodicity + continue; + } + } + + filteredEvents.Add(ev); + } + + return filteredEvents; + } + /// /// Combines all the tracks in all the events in the passed list into a single track. /// Each frame in the composite event is assigned the spectral point having maximum amplitude. diff --git a/src/AudioAnalysisTools/Events/Types/CompositeEvent.cs b/src/AudioAnalysisTools/Events/Types/CompositeEvent.cs index 17149c9ac..546153deb 100644 --- a/src/AudioAnalysisTools/Events/Types/CompositeEvent.cs +++ b/src/AudioAnalysisTools/Events/Types/CompositeEvent.cs @@ -41,6 +41,22 @@ public CompositeEvent(List events) public override double Score => this.ComponentEvents.Max(x => (x as SpectralEvent)?.Score) ?? double.PositiveInfinity; + public double CalculatePeriodicity() + { + var eventStarts = new List(); + foreach (var ev in this.ComponentEvents) + { + eventStarts.Add(ev.EventStartSeconds); + } + + // Sort array in ascending order. + var array = eventStarts.ToArray(); + Array.Sort(array); + var periodicity = Math.Abs(array[0] - array[array.Length - 1]) / array.Length; + + return periodicity; + } + public IEnumerable Tracks { get