Skip to content

Commit

Permalink
Try new test file for Powerful Owl
Browse files Browse the repository at this point in the history
Issue #370 Exploring possibility of doing amplitude normalisation on detected events.
  • Loading branch information
towsey authored and atruskie committed Oct 14, 2020
1 parent 5db186e commit c09d8c4
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Profiles:

# The first two post-processing steps are to combine overlapping/proximal/sequential events
# 1: Combine overlapping events
CombineOverlappingEvents: true
CombineOverlappingEvents: false

# 2: Combine possible syllable sequences
SyllableSequence:
Expand Down
7 changes: 0 additions & 7 deletions src/AnalysisPrograms/Recognizers/GenericRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,6 @@ public override RecognizerResults Recognize(
Log.Debug($"Event count after combining overlapped events = {allResults.NewEvents.Count}");
}

// ########################## NEW WORK - FILTER EVENTS USING DYNAMIC GAIN
//allResults.NewEvents = EventExtentions.FilterEventsUsingDynamicGain(
// allResults.NewEvents.Cast<EventCommon>().ToList(),
// allResults.Sonogram.Data,
// decibelThreshold);
//Log.Debug($"Event count after adjusting gain = {allResults.NewEvents.Count}");

// 2: Combine proximal events, that is, events that may be a sequence of syllables in the same strophe.
// Can also use this parameter to combine events that are in the upper or lower neighbourhood.
// Such combinations will increase bandwidth of the event and this property can be used later to weed out unlikely events.
Expand Down
48 changes: 45 additions & 3 deletions src/AudioAnalysisTools/Events/EventExtentions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public static List<SpectralEvent> FilterLongEvents(List<SpectralEvent> events, d
}

/// <summary>
/// Removes long events from a list of events.
/// Remove events from a list of events whose time duration is either too short or too long.
/// </summary>
public static List<EventCommon> FilterOnDuration(List<EventCommon> events, double minimumDurationSeconds, double maximumDurationSeconds)
{
Expand All @@ -109,7 +109,7 @@ public static List<EventCommon> FilterOnDuration(List<EventCommon> events, doubl
/// <param name="spectrogramData">The spectrogramin decibels.</param>
/// <param name="converter">Converter between real values and spectrogram frames/bins.</param>
/// <returns>The average decibel value.</returns>
public static double GetAverageDecibelsInEvent(SpectralEvent ev, double[,] spectrogramData, UnitConverters converter)
public static double[] GetDecibelArrayFromEvent(SpectralEvent ev, double[,] spectrogramData, UnitConverters converter)
{
// Get the frame and bin counts of the spectrogram
var frameCount = spectrogramData.GetLength(0);
Expand All @@ -132,7 +132,7 @@ public static double GetAverageDecibelsInEvent(SpectralEvent ev, double[,] spect

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

// extract the decibel array.
// extract the decibel array. Get the maximum decibel value in each frame.
int arrayLength = subMatrix.GetLength(0);
var decibelArray = new double[arrayLength];
for (int i = 0; i < arrayLength; i++)
Expand All @@ -141,6 +141,12 @@ public static double GetAverageDecibelsInEvent(SpectralEvent ev, double[,] spect
decibelArray[i] = spectralBins.Max();
}

return decibelArray;
}

public static double GetAverageDecibelsInEvent(SpectralEvent ev, double[,] spectrogramData, UnitConverters converter)
{
var decibelArray = GetDecibelArrayFromEvent(ev, spectrogramData, converter);
double avDecibels = decibelArray.Average();
return avDecibels;
}
Expand Down Expand Up @@ -445,5 +451,41 @@ public static IEnumerable<ISpectralPoint> GetCompositeTrack<T>(IEnumerable<T> ev

return maxAmplitudePoints.OrderBy(p => p);
}

/// <summary>
/// Some events close to microphone. Reduces the signal amplitude.
/// </summary>
public static List<EventCommon> FilterEventsAfterAmplitudeNormalisation(
List<EventCommon> events,
double[,] spectrogramData,
UnitConverters converter,
double decibelThreshold)
{
// set maximum decibel value
var filteredEvents = new List<EventCommon>();

foreach (var ev in events)
{
/*
var decibelArray = GetDecibelArrayFromEvent(ev, spectrogramData, converter);
var tracks = GetCompositeTrack<IEnumerable<T>>(ev);
ev.Tracks;
var converter1 = ev.Converter;
// get the decibel profile
var decibelArray = GetDecibelArrayFromEvent(ev, spectrogramData, converter);
var maxDb = decibelArray.Max();
// adjust profile to new gain. First find the max decibel value in the event.
// if max decibels less than
filteredEvents.Add(ev);
*/
}

return filteredEvents;
}
}
}
19 changes: 13 additions & 6 deletions src/AudioAnalysisTools/Tracks/ForwardTrackAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ public static (List<EventCommon> Events, double[] CombinedIntensity) GetForwardT

List<EventCommon> returnEvents = events.Cast<EventCommon>().ToList();

// ########################## NEW WORK - NORMALISE AMPLITUDE AND THEN FILTER EVENTS Again.
//allResults.NewEvents = EventExtentions.FilterEventsUsingDynamicGain(
// allResults.NewEvents.Cast<EventCommon>().ToList(),
// allResults.Sonogram.Data,
// decibelThreshold);
//Log.Debug($"Event count after adjusting gain = {allResults.NewEvents.Count}");

// Combine coincident events that are stacked one above other.
// This will help in some cases to combine related events.
if (parameters.CombinePossibleHarmonics)
Expand All @@ -118,12 +125,12 @@ public static (List<EventCommon> Events, double[] CombinedIntensity) GetForwardT

// Combine events that are temporally close and in the same frequency band.
// This will help in some cases to combine related events.
if (parameters.CombinePossibleSyllableSequence)
{
List<SpectralEvent> se = events.Cast<SpectralEvent>().ToList();
var timeDiff = TimeSpan.FromSeconds(parameters.SyllableStartDifference);
returnEvents = CompositeEvent.CombineProximalEvents(se, timeDiff, parameters.SyllableHertzGap);
}
//if (parameters.CombinePossibleSyllableSequence)
//{
// List<SpectralEvent> se = events.Cast<SpectralEvent>().ToList();
// var timeDiff = TimeSpan.FromSeconds(parameters.SyllableStartDifference);
// returnEvents = CompositeEvent.CombineProximalEvents(se, timeDiff, parameters.SyllableHertzGap);
//}

return (returnEvents, combinedIntensityArray);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public class PowerfulOwlTests : OutputDirectoryTest
/// "C:\Ecoacoustics\WavFiles\PowerfulOwl_NinoxStrenua\XC269666_PowerfulOwl_NinoxStrenua.mp3".
/// </summary>
//private static readonly FileInfo TestAsset = PathHelper.ResolveAsset("Recordings", "gympie_np_1192_331618_20150818_054959_31_0.wav");
private static readonly FileInfo TestAsset = new FileInfo(@"C:\Ecoacoustics\WavFiles\PowerfulOwl_NinoxStrenua\XC269666_PowerfulOwl_NinoxStrenua.wav");
//private static readonly FileInfo TestAsset = new FileInfo(@"C:\Ecoacoustics\WavFiles\PowerfulOwl_NinoxStrenua\XC269666_PowerfulOwl_NinoxStrenua.wav");
private static readonly FileInfo TestAsset = new FileInfo(@"C:\Ecoacoustics\WavFiles\PowerfulOwl_NinoxStrenua\Powerful3AndBoobook0_ksh3_1773_510819_20171109_174311_30_0.wav");
private static readonly FileInfo ConfigFile = PathHelper.ResolveConfigFile("RecognizerConfigFiles", "Towsey.NinoxStrenua.yml");
private static readonly AudioRecording Recording = new AudioRecording(TestAsset);
private static readonly NinoxStrenua Recognizer = new NinoxStrenua();
Expand Down

0 comments on commit c09d8c4

Please sign in to comment.