Skip to content

Commit

Permalink
Update EventExtentions.cs
Browse files Browse the repository at this point in the history
Issue #370 Conditions inserted to provent potential out of range index exceptions.
  • Loading branch information
towsey committed Sep 8, 2020
1 parent 3018b4b commit f2ea271
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/AudioAnalysisTools/Events/EventExtentions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,25 @@ public static List<EventCommon> FilterOnDuration(List<EventCommon> events, doubl
/// <returns>The average decibel value.</returns>
public static double GetAverageDecibelsInEvent(SpectralEvent ev, double[,] spectrogramData, UnitConverters converter)
{
// Get the length of the spectrogram
var frameCount = spectrogramData.GetLength(0);
var binCount = spectrogramData.GetLength(1);

// extract the event from the spectrogram
var lowerBin = converter.GetFreqBinFromHertz(ev.LowFrequencyHertz);
var upperBin = converter.GetFreqBinFromHertz(ev.HighFrequencyHertz);
if (upperBin >= binCount)
{
upperBin = binCount - 1;
}

var frameStart = converter.FrameFromStartTime(ev.EventStartSeconds);
var frameEnd = converter.FrameFromStartTime(ev.EventEndSeconds);
if (frameEnd >= frameCount)
{
frameEnd = frameCount - 1;
}

var subMatrix = MatrixTools.Submatrix<double>(spectrogramData, frameStart, lowerBin, frameEnd, upperBin);

// extract the decibel array.
Expand Down

0 comments on commit f2ea271

Please sign in to comment.