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 authored and atruskie committed Oct 14, 2020
1 parent d8ec8a9 commit 7d0f6bf
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 7d0f6bf

Please sign in to comment.