Skip to content

Commit

Permalink
Fixing bugs in merging of acoustic events
Browse files Browse the repository at this point in the history
Issue #300 The main difficulty here is confusing terminology of temporal  fileds and properties. Changed some of these to be more explicit.
There appears to remain one more bug - the number of acoustic events in the ae.csv file does not match number that appears in the spectrogram images??
  • Loading branch information
towsey committed Mar 6, 2020
1 parent b3cf451 commit 3d1b263
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 133 deletions.
17 changes: 10 additions & 7 deletions src/AnalysisBase/ResultBases/EventBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// --------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="EventBase.cs" company="QutEcoacoustics">
// All code in this file and all associated files are the copyright and property of the QUT Ecoacoustics Research Group (formerly MQUTeR, and formerly QUT Bioacoustics Research Group).
// </copyright>
Expand All @@ -12,14 +12,14 @@ namespace AnalysisBase.ResultBases
using System;

/// <summary>
/// The base class for all Event style results
/// The base class for all Event style results.
/// </summary>
public abstract class EventBase : ResultBase
{
private double eventStartSeconds;

/// <summary>
/// Gets or sets the time the current audio segment is offset from the start of the file/recording.
/// Gets or sets the time (in seconds) from start of the file/recording to start of the current audio segment.
/// </summary>
/// <remarks>
/// <see cref="EventStartSeconds"/> will always be greater than or equal to <see cref="SegmentStartSeconds"/>.
Expand All @@ -36,7 +36,7 @@ public abstract class EventBase : ResultBase
/// </summary>
/// <remarks>
/// 2017-09: This field USED to be offset relative to the current segment.
/// 2017-09: This field is NOW equivalent to <see cref="ResultBase.ResultStartSeconds"/>
/// 2017-09: This field is NOW equivalent to <see cref="ResultBase.ResultStartSeconds"/>.
/// </remarks>
public virtual double EventStartSeconds
{
Expand All @@ -60,10 +60,13 @@ public virtual double EventStartSeconds
/// </summary>
public virtual double? LowFrequencyHertz { get; protected set; }

protected void SetEventStartRelative(TimeSpan segmentStart, double eventStartSegmentRelative)
/// <summary>
/// Sets both the Segment start and the Event start wrt to recording.
/// </summary>
protected void SetSegmentAndEventStartsWrtRecording(TimeSpan segmentStartWrtRecording, double eventStartWrtSegment)
{
this.SegmentStartSeconds = segmentStart.TotalSeconds;
this.EventStartSeconds = this.SegmentStartSeconds + eventStartSegmentRelative;
this.SegmentStartSeconds = segmentStartWrtRecording.TotalSeconds;
this.EventStartSeconds = this.SegmentStartSeconds + eventStartWrtSegment;
}
}
}
37 changes: 5 additions & 32 deletions src/AnalysisPrograms/Recognizers/Base/WhistleParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static (List<AcousticEvent>, double[]) GetWhistles(
double decibelThreshold,
double minDuration,
double maxDuration,
TimeSpan segmentStartOffset)
TimeSpan segmentStartWrtRecording)
{
var sonogramData = sonogram.Data;
int frameCount = sonogramData.GetLength(0);
Expand All @@ -39,10 +39,6 @@ public static (List<AcousticEvent>, double[]) GetWhistles(
double binWidth = nyquist / (double)binCount;
int minBin = (int)Math.Round(minHz / binWidth);
int maxBin = (int)Math.Round(maxHz / binWidth);
//int binCountInBand = maxBin - minBin + 1;

// buffer zone around whistle is four bins wide.
int N = 4;

// list of accumulated acoustic events
var events = new List<AcousticEvent>();
Expand All @@ -54,7 +50,8 @@ public static (List<AcousticEvent>, double[]) GetWhistles(
// set up an intensity array for the frequency bin.
double[] intensity = new double[frameCount];

if (minBin < N)
// buffer zone around whistle is four bins wide.
if (minBin < 4)
{
// for all time frames in this frequency bin
for (int t = 0; t < frameCount; t++)
Expand Down Expand Up @@ -96,7 +93,7 @@ public static (List<AcousticEvent>, double[]) GetWhistles(
decibelThreshold,
minDuration,
maxDuration,
segmentStartOffset);
segmentStartWrtRecording);

// add to conbined intensity array
for (int t = 0; t < frameCount; t++)
Expand All @@ -110,33 +107,9 @@ public static (List<AcousticEvent>, double[]) GetWhistles(
} //end for all freq bins

// combine adjacent acoustic events
events = AcousticEvent.CombineOverlappingEvents(events);
events = AcousticEvent.CombineOverlappingEvents(events, segmentStartWrtRecording);

return (events, combinedIntensityArray);
}

/*
/// <summary>
/// Calculates the average intensity in a freq band having min and max freq,
/// AND then subtracts average intensity in the side/buffer bands, below and above.
/// THis method adds dB log values incorrectly but it is faster than doing many log conversions.
/// This method is used to find acoustic events and is accurate enough for the purpose.
/// </summary>
public static double[] CalculateFreqBandAvIntensityMinusBufferIntensity(double[,] sonogramData, int minHz, int maxHz, int nyquist)
{
var bandIntensity = SNR.CalculateFreqBandAvIntensity(sonogramData, minHz, maxHz, nyquist);
var bottomSideBandIntensity = SNR.CalculateFreqBandAvIntensity(sonogramData, minHz - bottomHzBuffer, minHz, nyquist);
var topSideBandIntensity = SNR.CalculateFreqBandAvIntensity(sonogramData, maxHz, maxHz + topHzBuffer, nyquist);
int frameCount = sonogramData.GetLength(0);
double[] netIntensity = new double[frameCount];
for (int i = 0; i < frameCount; i++)
{
netIntensity[i] = bandIntensity[i] - bottomSideBandIntensity[i] - topSideBandIntensity[i];
}
return netIntensity;
}
*/
}
}
2 changes: 1 addition & 1 deletion src/AnalysisPrograms/Recognizers/GenericRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ static void SaveDebugSpectrogram(RecognizerResults results, Config genericConfig
var image3 = SpectrogramTools.GetSonogramPlusCharts(results.Sonogram, results.Events, results.Plots, null);

//image3.Save(Path.Combine(outputDirectory.FullName, baseName + ".profile.png"));
image3.Save(Path.Combine("C:\\temp", baseName + ".profile.png"));
//image3.Save(Path.Combine("C:\\temp", baseName + ".profile.png"));

//sonogram.GetImageFullyAnnotated("test").Save("C:\\temp\\test.png");
}
Expand Down
Loading

0 comments on commit 3d1b263

Please sign in to comment.