Skip to content

Commit

Permalink
Write methods to calculate the periodicity of syllable sequence
Browse files Browse the repository at this point in the history
Issue #370
  • Loading branch information
towsey committed Sep 3, 2020
1 parent fb753a8 commit 6b1e478
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/AudioAnalysisTools/Events/EventExtentions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,36 @@ public static List<EventCommon> FilterEventsOnCompositeContent(
return filteredEvents;
}

/// <summary>
/// Removes composite events from a list of EventCommon where the component syllables do not have the correct periodicity.
/// </summary>
public static List<EventCommon> FilterEventsOnSyllablePeriodicity(
List<EventCommon> events,
double expectedPeriod,
double periodSd)
{
var filteredEvents = new List<EventCommon>();

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;
}

/// <summary>
/// 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.
Expand Down
16 changes: 16 additions & 0 deletions src/AudioAnalysisTools/Events/Types/CompositeEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,22 @@ public CompositeEvent(List<SpectralEvent> events)
public override double Score =>
this.ComponentEvents.Max(x => (x as SpectralEvent)?.Score) ?? double.PositiveInfinity;

public double CalculatePeriodicity()
{
var eventStarts = new List<double>();
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<ITrack> Tracks
{
get
Expand Down

0 comments on commit 6b1e478

Please sign in to comment.