Skip to content

Commit

Permalink
More work on calculation and normalisation of event scores.
Browse files Browse the repository at this point in the history
Issue #297
  • Loading branch information
towsey committed Apr 25, 2020
1 parent f2f8afd commit e464f63
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 14 deletions.
7 changes: 7 additions & 0 deletions src/AudioAnalysisTools/CommonParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,12 @@ public abstract class CommonParameters
/// Gets or sets the threshold of "loudness" of a component. Units are decibels.
/// </summary>
public double? DecibelThreshold { get; set; } = 6;

/// <summary>
/// Gets or sets the maximum score for an event.
/// Setting this value sets a normalised score value for the event.
/// The normalised score is a linear conversion from 0 - maxScore to [0, 1].
/// </summary>
public double? MaxScore { get; set; }
}
}
28 changes: 21 additions & 7 deletions src/AudioAnalysisTools/Events/Types/ChirpEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,20 @@ namespace AudioAnalysisTools

public class ChirpEvent : SpectralEvent, ITracks<Track>
{
public ChirpEvent(Track chirp)
/// <summary>
/// Initializes a new instance of the <see cref="ChirpEvent"/> class.
/// MaxScore establishes a scale for the chirp score. Typically the amplitude of track points is decibels.
/// A satisfactory maxScore is 12.0 decibels, since this is a high SNR in enviornmental recordings.
/// The normalised score is a linear conversion from 0 - maxScore to [0, 1].
/// </summary>
/// <param name="chirp">A chirp track consisting of a sequence of spectral points.</param>
/// <param name="maxScore">A maximum score used to normalise the track score.</param>
public ChirpEvent(Track chirp, double maxScore)
{
this.Tracks.Add(chirp);

// set score = to aaverage normalised amplitude score.
//this.Score =
// set score = to average normalised amplitude score.
this.SetTrackScore(maxScore);
}

public List<Track> Tracks { get; private set; } = new List<Track>(1);
Expand All @@ -37,12 +45,18 @@ public ChirpEvent(Track chirp)
public override double HighFrequencyHertz =>
this.Tracks.Max(x => x.HighFreqHertz);

public override void Draw(IImageProcessingContext graphics, EventRenderingOptions options)
/// <summary>
/// Sets a normalised value for the chirp's track score.
/// NOTE: It is assumed that the minimum value of the score range = zero.
/// </summary>
/// <param name="maxScore">The max score value which sets the scale.</param>
public void SetTrackScore(double maxScore)
{
// foreach (var track in tracks) {
// track.Draw(...)
// }
this.Score = this.Tracks[0].GetAverageTrackAmplitude() / maxScore;
}

public override void Draw(IImageProcessingContext graphics, EventRenderingOptions options)
{
this.Tracks.First().Draw(graphics, options);

// base drawing (border)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ public static Image<Rgb24> GetSonogramPlusCharts(
foreach (SpectralEvent ev in events)
{
var options = new EventRenderingOptions(new UnitConverters(segmentStartTime.TotalSeconds, segmentDuration.TotalSeconds, nyquist, width, height));
spectrogram.Mutate(x => ev.Draw(x, options));

//spectrogram.Mutate(x => ev.Draw(x, options));
spectrogram.Mutate(x => ev.DrawWithAnnotation(x, options));
}
}

Expand Down
17 changes: 14 additions & 3 deletions src/AudioAnalysisTools/Tracks/ForwardTrackParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,19 @@ public class ForwardTrackParameters : CommonParameters
/// </summary>
public bool CombinePossibleHarmonics { get; set; }

public TimeSpan StartDifference { get; set; }
public TimeSpan HarmonicsStartDifference { get; set; }

public int HertzGap { get; set; }
}
public int HarmonicsHertzGap { get; set; }

/// <summary>
/// Gets or sets a value indicating whether a short sequence of chirp tracks are to be considered a combined event.
/// To qualify for combining, the event start times should not be greater than the specified seconds interval....
/// AND the difference in minimum frequency values (the Hertz gap) between consecutive tracks should not exceed the specified Hertz interval.
/// </summary>
public bool CombinePossibleChirpSequence { get; set; }

public TimeSpan ChirpStartDifference { get; set; }

public int ChirpHertzGap { get; set; }
}
}
16 changes: 15 additions & 1 deletion src/AudioAnalysisTools/Tracks/Track.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,6 @@ public double[] GetTrackFrequencyProfile()

/// <summary>
/// Returns the maximum amplitude in each time frame.
/// TODO ############################################
/// </summary>
public double[] GetAmplitudeOverTimeFrames()
{
Expand All @@ -210,6 +209,21 @@ public double[] GetAmplitudeOverTimeFrames()
return sorted;
}

/// <summary>
/// Returns the maximum amplitude in each time frame.
/// </summary>
public double GetAverageTrackAmplitude()
{
var sum = 0.0;
foreach (var point in this.Points)
{
sum += point.Value;
}

var av = sum / this.Points.Count;
return av;
}

/// <summary>
/// Draws the track on an image given by its processing context.
/// </summary>
Expand Down
5 changes: 3 additions & 2 deletions src/AudioAnalysisTools/Tracks/TrackExtractor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ public static Track GetOneFrameTrack(double[,] peaks, int startRow, int startBin
}

/// <summary>
/// This method returns foward (spectral peak) tracks enclosed in acoustic events.
/// 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.
/// </summary>
/// <param name="sonogram">The spectrogram to be searched.</param>
Expand All @@ -429,6 +429,7 @@ public static (List<EventCommon> Events, double[] CombinedIntensity) GetForwardT
double binWidth = nyquist / (double)binCount;
int minBin = (int)Math.Round(parameters.MinHertz.Value / binWidth);
int maxBin = (int)Math.Round(parameters.MaxHertz.Value / binWidth);
double maxScore = parameters.MaxScore.Value;

var converter = new UnitConverters(
segmentStartOffset: segmentStartOffset.TotalSeconds,
Expand Down Expand Up @@ -464,7 +465,7 @@ public static (List<EventCommon> Events, double[] CombinedIntensity) GetForwardT
var combinedIntensityArray = new double[frameCount];
foreach (var track in tracks)
{
var ae = new ChirpEvent(track)
var ae = new ChirpEvent(track, maxScore)
{
SegmentDurationSeconds = frameCount * converter.StepSize,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ public void TestForwardTrackAlgorithm()
MinDuration = 0.2,
MaxDuration = 1.1,
DecibelThreshold = 2.0,
MaxScore = 12.0,
CombinePossibleHarmonics = false,
};

Expand Down

0 comments on commit e464f63

Please sign in to comment.