Skip to content

Commit

Permalink
Shift method PreparePlot()
Browse files Browse the repository at this point in the history
Issue #370: shift PreparePlot() method from GenericRecognizer class to Plot class.
  • Loading branch information
towsey authored and atruskie committed Oct 14, 2020
1 parent 009ce06 commit 9c0abc7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 24 deletions.
31 changes: 7 additions & 24 deletions src/AnalysisPrograms/Recognizers/GenericRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public override RecognizerResults Recognize(
spectrogram.NyquistFrequency);

// prepare plot of resultant blob decibel array.
var plot = PreparePlot(decibelArray, $"{profileName} (Blob:db Intensity)", bp.DecibelThreshold.Value);
var plot = Plot.PreparePlot(decibelArray, $"{profileName} (Blob:db Intensity)", bp.DecibelThreshold.Value);
plots.Add(plot);

// iii: CONVERT blob decibel SCORES TO ACOUSTIC EVENTS.
Expand All @@ -204,7 +204,7 @@ public override RecognizerResults Recognize(
wp,
segmentStartOffset);

var plot = PreparePlot(decibelArray, $"{profileName} (Whistle:dB Intensity)", wp.DecibelThreshold.Value);
var plot = Plot.PreparePlot(decibelArray, $"{profileName} (Whistle:dB Intensity)", wp.DecibelThreshold.Value);
plots.Add(plot);
}
else if (profileConfig is ForwardTrackParameters tp)
Expand All @@ -215,7 +215,7 @@ public override RecognizerResults Recognize(
tp,
segmentStartOffset);

var plot = PreparePlot(decibelArray, $"{profileName} (Chirps:dB Intensity)", tp.DecibelThreshold.Value);
var plot = Plot.PreparePlot(decibelArray, $"{profileName} (Chirps:dB Intensity)", tp.DecibelThreshold.Value);
plots.Add(plot);
}
else if (profileConfig is OneframeTrackParameters cp)
Expand All @@ -226,7 +226,7 @@ public override RecognizerResults Recognize(
cp,
segmentStartOffset);

var plot = PreparePlot(decibelArray, $"{profileName} (Clicks:dB Intensity)", cp.DecibelThreshold.Value);
var plot = Plot.PreparePlot(decibelArray, $"{profileName} (Clicks:dB Intensity)", cp.DecibelThreshold.Value);
plots.Add(plot);
}
else if (profileConfig is UpwardTrackParameters vtp)
Expand All @@ -237,7 +237,7 @@ public override RecognizerResults Recognize(
vtp,
segmentStartOffset);

var plot = PreparePlot(decibelArray, $"{profileName} (VerticalTrack:dB Intensity)", vtp.DecibelThreshold.Value);
var plot = Plot.PreparePlot(decibelArray, $"{profileName} (VerticalTrack:dB Intensity)", vtp.DecibelThreshold.Value);
plots.Add(plot);
}
else if (profileConfig is HarmonicParameters hp)
Expand All @@ -257,7 +257,7 @@ public override RecognizerResults Recognize(
hp.MaxFormantGap.Value,
segmentStartOffset);

var plot = PreparePlot(harmonicIntensityScores, $"{profileName} (Harmonics:dct intensity)", hp.DctThreshold.Value);
var plot = Plot.PreparePlot(harmonicIntensityScores, $"{profileName} (Harmonics:dct intensity)", hp.DctThreshold.Value);
plots.Add(plot);
}
else if (profileConfig is OscillationParameters op)
Expand All @@ -281,7 +281,7 @@ public override RecognizerResults Recognize(
spectralEvents = new List<EventCommon>(oscillationEvents);

//plots.Add(new Plot($"{profileName} (:OscillationScore)", scores, op.EventThreshold));
var plot = PreparePlot(scores, $"{profileName} (:OscillationScore)", op.EventThreshold);
var plot = Plot.PreparePlot(scores, $"{profileName} (:OscillationScore)", op.EventThreshold);
plots.Add(plot);
}
else
Expand Down Expand Up @@ -438,23 +438,6 @@ public static string SaveDebugSpectrogram(RecognizerResults results, Config gene
return path;
}

/// <summary>
/// Prepares a plot of an array of score values.
/// To obtain a more useful display, the maximum display value is set to 3 times the threshold value.
/// </summary>
/// <param name="array">an array of double.</param>
/// <param name="title">to accompany the plot.</param>
/// <param name="threshold">A threshold value to be drawn on the plot.</param>
/// <returns>the plot.</returns>
private static Plot PreparePlot(double[] array, string title, double threshold)
{
double intensityNormalizationMax = 3 * threshold;
var eventThreshold = threshold / intensityNormalizationMax;
var normalisedIntensityArray = DataTools.NormaliseInZeroOne(array, 0, intensityNormalizationMax);
var plot = new Plot(title, normalisedIntensityArray, eventThreshold);
return plot;
}

private static SonogramConfig ParametersToSonogramConfig(CommonParameters common)
{
int windowSize = (int)common.FrameSize;
Expand Down
22 changes: 22 additions & 0 deletions src/AudioAnalysisTools/Tracks/ForwardTrackAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,28 @@ namespace AudioAnalysisTools.Tracks

public static class ForwardTrackAlgorithm
{
public static (List<EventCommon> Events, List<Plot> DecibelPlots) GetForwardTracks(
SpectrogramStandard spectrogram,
ForwardTrackParameters parameters,
TimeSpan segmentStartOffset,
string profileName)
{
var thresholds = parameters.DecibelThreshold;

double[] decibelArray;
List<EventCommon> spectralEvents;
var plots = new List<Plot>();

(spectralEvents, decibelArray) = ForwardTrackAlgorithm.GetForwardTracks(
spectrogram,
parameters,
segmentStartOffset);

var plot = Plot.PreparePlot(decibelArray, $"{profileName} (Chirps:dB Intensity)", thresholds.Value);
plots.Add(plot);
return (spectralEvents, plots);
}

/// <summary>
/// This method returns foward (spectral peak) tracks enclosed in spectral events.
/// It averages dB log values incorrectly but it is faster than doing many log conversions.
Expand Down
17 changes: 17 additions & 0 deletions src/TowseyLibrary/Plot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,23 @@ public Plot(string _title, double[] _data, double _threshold)
this.threshold = _threshold;
}

/// <summary>
/// Prepares a plot of an array of score values.
/// To obtain a more useful display, the maximum display value is set to 3 times the threshold value.
/// </summary>
/// <param name="array">an array of double.</param>
/// <param name="title">to accompany the plot.</param>
/// <param name="threshold">A threshold value to be drawn on the plot.</param>
/// <returns>the plot.</returns>
public static Plot PreparePlot(double[] array, string title, double threshold)
{
double intensityNormalizationMax = 3 * threshold;
var eventThreshold = threshold / intensityNormalizationMax;
var normalisedIntensityArray = DataTools.NormaliseInZeroOne(array, 0, intensityNormalizationMax);
var plot = new Plot(title, normalisedIntensityArray, eventThreshold);
return plot;
}

public string title { get; set; }

public double[] data { get; set; }
Expand Down

0 comments on commit 9c0abc7

Please sign in to comment.