Skip to content

Commit

Permalink
Debug UnitConverters
Browse files Browse the repository at this point in the history
Issue #297 Also make changes ready to store multiple tracks in one AcousticEvent.
  • Loading branch information
towsey committed Apr 17, 2020
1 parent 1cd8412 commit 83eb327
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 22 deletions.
8 changes: 4 additions & 4 deletions src/Acoustics.Shared/Interval.cs
Original file line number Diff line number Diff line change
Expand Up @@ -251,10 +251,10 @@ public override int GetHashCode()
/// <returns>
/// String representation.
/// </returns>
//public override string ToString(string v)
//{
// return this.ToString(false);
//}
public override string ToString()
{
return this.ToString(false);
}

/// <summary>
/// Gets string representation of the Interval.
Expand Down
8 changes: 7 additions & 1 deletion src/AudioAnalysisTools/AcousticEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,16 @@ public AcousticEventClassMap()
public double Bandwidth => this.HighFrequencyHertz - this.LowFrequencyHertz + 1;

/// <summary>
/// Gets or sets a horizontal or vertical spectral track.
/// Gets or sets a spectral track.
/// </summary>
public Track TheTrack { get; set; }

/// <summary>
/// Gets or sets a list of tracks.
/// This will be used when two or more events containing single tracks are merged into one combined event.
/// </summary>
public List<Track> Tracks { get; set; }

public bool IsMelscale { get; set; }

/// <summary>
Expand Down
6 changes: 5 additions & 1 deletion src/AudioAnalysisTools/Events/Tracks/Track.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class Track : ITrack

private readonly TrackType trackType;

private readonly List<ISpectralPoint> _pointCopy;

/// <summary>
/// Initializes a new instance of the <see cref="Track"/> class.
/// Constructor.
Expand All @@ -40,6 +42,7 @@ public Track(UnitConverters converter, TrackType aTrackType)
this.converter = converter;
this.trackType = aTrackType;
this.Points = new SortedSet<ISpectralPoint>();
this._pointCopy = new List<ISpectralPoint>();
}

/// <inheritdoc cref="Track.Track(UnitConverters, TrackType)"/>
Expand Down Expand Up @@ -105,6 +108,7 @@ public void SetPoint(int frame, int bin, double amplitude)
amplitude);

this.Points.Add(point);
this._pointCopy.Add(point);
}

/// <summary>
Expand All @@ -130,7 +134,7 @@ public string CheckPoint(int frame, int bin)
if (frame != outFrame || bin != outBin)
{
LoggedConsole.WriteWarnLine("WARNING" + info);
//throw new Exception("WARNING" + info);
throw new Exception("WARNING" + info);
}
else
{
Expand Down
3 changes: 2 additions & 1 deletion src/AudioAnalysisTools/Events/Tracks/TrackExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ public static Track GetUpwardTrack(double[,] peaks, int startRow, int startBin,
var maxValue = directionOptions[maxId] / 2.0;
if (maxValue < threshold)
{
peaks[row, bin] = 0.0;
return track;
}

Expand All @@ -230,7 +231,7 @@ public static Track GetUpwardTrack(double[,] peaks, int startRow, int startBin,
track.SetPoint(row, bin, maxValue);

// next line is for debug purposes
var info = track.CheckPoint(row, bin);
//var info = track.CheckPoint(row, bin);
}

return track;
Expand Down
14 changes: 7 additions & 7 deletions src/AudioAnalysisTools/UnitConverters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,14 @@ public SizeF GetSize(ISpectralEvent @event)

public double RecordingRelativeToSegmentRelative(double seconds) => seconds + this.SegmentStartOffset;

//public double SecondsDurationFromFrameCount(int frameCount)
//{
// return this.SecondsScale.GetSecondsDurationFromFrameCount(frameCount);
//}

public int FrameFromStartTime(double startTime)
{
return (int)(startTime / this.SecondsPerFrameStep);
return (int)Math.Round((startTime - this.SegmentStartOffset) / this.SecondsPerFrameStep, MidpointRounding.AwayFromZero);
}

public int FrameFromEndTime(double endTime)
{
return (int)Math.Round((endTime - this.SegmentStartOffset - this.SecondsPerFrame) / this.SecondsPerFrameStep, MidpointRounding.AwayFromZero);
}

public double GetStartTimeInSecondsOfFrame(int frameId)
Expand All @@ -238,7 +238,7 @@ public double GetStartTimeInSecondsOfFrame(int frameId)

public double GetEndTimeInSecondsOfFrame(int frameId)
{
return this.SegmentStartOffset + (this.GetStartTimeInSecondsOfFrame(frameId) + this.SecondsPerFrame);
return this.GetStartTimeInSecondsOfFrame(frameId) + this.SecondsPerFrame;
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ public void TestClickAlgorithm()
/// Tests the upward-track recognizer on the same artifical spectrogram as used for foward-tracks and harmonics.
/// </summary>
[TestMethod]
public void TestUpwardsTrackAlgorithm()
public void Test1UpwardsTrackAlgorithm()
{
// Set up the recognizer parameters.
var windowSize = 512;
Expand Down Expand Up @@ -585,12 +585,48 @@ public void TestUpwardsTrackAlgorithm()
Assert.AreEqual(11.24, @event.EventEndSeconds, 0.1);
Assert.AreEqual(6450, @event.LowFrequencyHertz);
Assert.AreEqual(7310, @event.HighFrequencyHertz);
}

/// <summary>
/// Tests the upward-track recognizer on the same artifical spectrogram as used for foward-tracks and harmonics.
/// </summary>
[TestMethod]
public void Test2UpwardsTrackAlgorithm()
{
// Set up the recognizer parameters.
var windowSize = 512;
var windowStep = 512;
var minHertz = 500;
var maxHertz = 6000;
var minBandwidthHertz = 100;
var maxBandwidthHertz = 5000;
var decibelThreshold = 2.0;
var combineProximalSimilarEvents = false;

//Set up the virtual recording.
var segmentStartOffset = TimeSpan.Zero;
int samplerate = 22050;
double signalDuration = 13.0; //seconds

// set up the config for a virtual spectrogram.
var sonoConfig = new SonogramConfig()
{
WindowSize = windowSize,
WindowStep = windowStep,
WindowOverlap = 0.0, // this must be set
WindowFunction = WindowFunctions.HANNING.ToString(),
NoiseReductionType = NoiseReductionType.Standard,
NoiseReductionParameter = 0.0,
Duration = TimeSpan.FromSeconds(signalDuration),
SampleRate = samplerate,
};

var spectrogram = this.CreateArtificialSpectrogramToTestTracksAndHarmonics(sonoConfig);
var plots = new List<Plot>();

// do a SECOND TEST of the vertical tracks
minHertz = 500;
maxHertz = 6000;
minBandwidthHertz = 100;
maxBandwidthHertz = 5000;
double[] dBArray;
List<AcousticEvent> acousticEvents;
(acousticEvents, dBArray) = UpwardTrackParameters.GetUpwardTracks(
spectrogram,
minHertz,
Expand All @@ -602,7 +638,9 @@ public void TestUpwardsTrackAlgorithm()
segmentStartOffset);

// draw a plot of max decibels in each frame
normalisedDecibelArray = DataTools.NormaliseInZeroOne(dBArray, 0, decibelNormalizationMax);
double decibelNormalizationMax = 3 * decibelThreshold;
var dBThreshold = decibelThreshold / decibelNormalizationMax;
var normalisedDecibelArray = DataTools.NormaliseInZeroOne(dBArray, 0, decibelNormalizationMax);
var plot2 = new Plot("decibel max", normalisedDecibelArray, dBThreshold);
plots.Add(plot2);

Expand All @@ -618,11 +656,10 @@ public void TestUpwardsTrackAlgorithm()
// combine the results i.e. add the events list of call events.
allResults2.Events.AddRange(acousticEvents);
allResults2.Plots.AddRange(plots);

// effectively keeps only the *last* sonogram produced
allResults2.Sonogram = spectrogram;

// DEBUG PURPOSES ONLY - COMMENT NEXT LINE
var outputDirectory = new DirectoryInfo("C:\\temp");
GenericRecognizer.SaveDebugSpectrogram(allResults2, null, outputDirectory, "UpwardTracks2");

Assert.AreEqual(5, allResults2.Events.Count);
Expand Down
40 changes: 40 additions & 0 deletions tests/Acoustics.Test/AudioAnalysisTools/UnitConverterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Acoustics.Test.AudioAnalysisTools
using global::AudioAnalysisTools;
using global::AudioAnalysisTools.Events;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;

[TestClass]
public class UnitConverterTests
Expand Down Expand Up @@ -35,5 +37,43 @@ public void TestGetPixelRectangle()
Assert.AreEqual(80, rect.Height);

}

public static IEnumerable<object[]> FrameAndBinData
{
get
{
var frames = Enumerable.Range(0, 10);
var bins = Enumerable.Range(0, 10);

var combinations = from f in frames from b in bins select new object[] { f, b };

return combinations;
}
}

[DataTestMethod]
[DynamicData(nameof(FrameAndBinData))]
public void TestBackwardsAndForwardsConversionSpectrograms(int frame, int bin)
{
UnitConverters converter = new UnitConverters(
60,
22050,
512,
256);

var endFrame = frame + 1;

var secondsStart = converter.GetStartTimeInSecondsOfFrame(frame);
var secondsEnd = converter.GetEndTimeInSecondsOfFrame(endFrame);
var hertz = converter.GetHertzFromFreqBin(bin);

var outFrame = converter.FrameFromStartTime(secondsStart);
var endOutFrame = converter.FrameFromEndTime(secondsEnd);
var outBin = converter.GetFreqBinFromHertz(hertz);

Assert.AreEqual(frame, outFrame, $"Frames do not match, expected: {frame}, actual: {outFrame}, seconds was: {secondsStart}");
Assert.AreEqual(endFrame, endOutFrame, $"Frame ends do not match, expected: {endFrame}, actual: {endOutFrame}, seconds was: {secondsEnd}");
Assert.AreEqual(bin, outBin, $"Bins do not match, expected: {bin}, actual: {outBin}, hertz was: {hertz}");
}
}
}

0 comments on commit 83eb327

Please sign in to comment.