Skip to content

Commit

Permalink
Create Australasian Bittern recognizer
Browse files Browse the repository at this point in the history
  • Loading branch information
towsey committed May 2, 2020
1 parent c90f8fc commit e79f6e2
Show file tree
Hide file tree
Showing 2 changed files with 197 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
---

# Resample rate must be 2 X the desired Nyquist
ResampleRate: 8000
# SegmentDuration: units=seconds;
SegmentDuration: 60
# SegmentOverlap: units=seconds;
SegmentOverlap: 0

# Each of these profiles will be analyzed
Profiles:
WhistleSyllable: !WhistleParameters
FrameSize: 1024
FrameStep: 512
BgNoiseThreshold: 0.0
WindowFunction: HANNING
# min and max of the freq band to search
MinHertz: 100
MaxHertz: 200
MinDuration: 0.2
MaxDuration: 1.0
DecibelThreshold: 6.0
ComponentName: Whistle
SpeciesName: BotaurusPoiciloptilus

# Common settings
#Standard: &STANDARD
#EventThreshold: 0.2
#BgNoiseThreshold: 3.0

# This notation means the a profile has all of the settings that the Standard profile has,
# however, the DctDuration parameter has been overridden.
# <<: *STANDARD
# DctDuration: 0.3

# Available options (case-sensitive): [False/Never | True/Always | WhenEventsDetected]
SaveIntermediateWavFiles: Never
SaveIntermediateCsvFiles: false
# Available options (case-sensitive): [False/Never | True/Always | WhenEventsDetected]
# "True" is useful when debugging but "WhenEventsDetected" is required for operational use.
SaveSonogramImages: True
#SaveSonogramImages: WhenEventsDetected
# DisplayCsvImage is obsolete - ensure it remains set to: false
DisplayCsvImage: false
## End section for AnalyzeLongRecording

# Other config files to reference

HighResolutionIndicesConfig: "../Towsey.Acoustic.HiResIndicesForRecognisers.yml"
...
147 changes: 147 additions & 0 deletions src/AnalysisPrograms/Recognizers/Birds/BotaurusPoiciloptilus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// <copyright file="BotaurusPoiciloptilus.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>

namespace AnalysisPrograms.Recognizers
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using Acoustics.Shared.ConfigFile;
using AnalysisBase;
using AnalysisPrograms.Recognizers.Base;
using AudioAnalysisTools;
using AudioAnalysisTools.DSP;
using AudioAnalysisTools.Events;
using AudioAnalysisTools.Events.Types;
using AudioAnalysisTools.Indices;
using AudioAnalysisTools.StandardSpectrograms;
using AudioAnalysisTools.WavTools;
using log4net;
using SixLabors.ImageSharp;
using TowseyLibrary;
using static AnalysisPrograms.Recognizers.GenericRecognizer;
using Path = System.IO.Path;

/// <summary>
/// A recognizer for the Australasian Bittern, Botaurus poiciloptilus, https://en.wikipedia.org/wiki/Australasian_bittern.
/// The Australasian bittern, also known as the brown bittern or matuku hūrepo, is a large bird in the heron family Ardeidae.
/// A secretive bird with a distinctive booming call, it is more often heard than seen.
/// Australasian bitterns are endangered in both Australia and New Zealand.
/// </summary>
internal class BotaurusPoiciloptilus : RecognizerBase
{
private static readonly ILog BitternLog = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

public override string Author => "Towsey";

public override string SpeciesName => "BotaurusPoiciloptilus";

public override string Description => "[ALPHA] Detects acoustic events for the Australasian Bittern.";

public override AnalyzerConfig ParseConfig(FileInfo file)
{
RuntimeHelpers.RunClassConstructor(typeof(BotaurusPoiciloptilusConfig).TypeHandle);
var config = ConfigFile.Deserialize<BotaurusPoiciloptilusConfig>(file);

// validation of configs can be done here
GenericRecognizer.ValidateProfileTagsMatchAlgorithms(config.Profiles, file);

// This call sets a restriction so that only one generic algorithm is used.
// CHANGE this to accept multiple generic algorithms as required.
//if (result.Profiles.SingleOrDefault() is ForwardTrackParameters)
if (config.Profiles?.Count == 1 && config.Profiles.First().Value is ForwardTrackParameters)
{
return config;
}

throw new ConfigFileException("Autralasian Bittern expects one and only one ForwardTrack algorithm.", file);
}

/// <summary>
/// This method is called once per segment (typically one-minute segments).
/// </summary>
/// <param name="audioRecording">one minute of audio recording.</param>
/// <param name="config">config file that contains parameters used by all profiles.</param>
/// <param name="segmentStartOffset">when recording starts.</param>
/// <param name="getSpectralIndexes">not sure what this is.</param>
/// <param name="outputDirectory">where the recognizer results can be found.</param>
/// <param name="imageWidth"> assuming ????.</param>
/// <returns>recognizer results.</returns>
public override RecognizerResults Recognize(
AudioRecording audioRecording,
Config config,
TimeSpan segmentStartOffset,
Lazy<IndexCalculateResult[]> getSpectralIndexes,
DirectoryInfo outputDirectory,
int? imageWidth)
{
//class BotaurusPoiciloptilusConfig is define at bottom of this file.
var genericConfig = (BotaurusPoiciloptilusConfig)config;
var recognizer = new GenericRecognizer();

RecognizerResults combinedResults = recognizer.Recognize(
audioRecording,
genericConfig,
segmentStartOffset,
getSpectralIndexes,
outputDirectory,
imageWidth);

// DO POST-PROCESSING of EVENTS

// Convert events to spectral events for possible combining.
// Combine overlapping events. If the dB threshold is set low, may get lots of little events.
var events = combinedResults.NewEvents;
var spectralEvents = events.Select(x => (SpectralEvent)x).ToList();
var newEvents = CompositeEvent.CombineOverlappingEvents(spectralEvents);

if (genericConfig.CombinePossibleSyllableSequence)
{
// convert events to spectral events for possible combining.
//var spectralEvents = events.Select(x => (SpectralEvent)x).ToList();
spectralEvents = newEvents.Cast<SpectralEvent>().ToList();
var startDiff = genericConfig.SyllableStartDifference;
var hertzDiff = genericConfig.SyllableHertzGap;
newEvents = CompositeEvent.CombineSimilarProximalEvents(spectralEvents, TimeSpan.FromSeconds(startDiff), (int)hertzDiff);
}

combinedResults.NewEvents = newEvents;

//UNCOMMENT following line if you want special debug spectrogram, i.e. with special plots.
// NOTE: Standard spectrograms are produced by setting SaveSonogramImages: "True" or "WhenEventsDetected" in <Towsey.PteropusSpecies.yml> config file.
//GenericRecognizer.SaveDebugSpectrogram(territorialResults, genericConfig, outputDirectory, audioRecording.BaseName);
return combinedResults;
}

/*
/// <summary>
/// Summarize your results. This method is invoked exactly once per original file.
/// </summary>
public override void SummariseResults(
AnalysisSettings settings,
FileSegment inputFileSegment,
EventBase[] events,
SummaryIndexBase[] indices,
SpectralIndexBase[] spectralIndices,
AnalysisResult2[] results)
{
// No operation - do nothing. Feel free to add your own logic.
base.SummariseResults(settings, inputFileSegment, events, indices, spectralIndices, results);
}
*/

/// <inheritdoc cref="BotaurusPoiciloptilusConfig"/> />
public class BotaurusPoiciloptilusConfig : GenericRecognizerConfig, INamedProfiles<object>
{
public bool CombinePossibleSyllableSequence { get; set; } = false;

public double SyllableStartDifference { get; set; } = 0.5;

public double SyllableHertzGap { get; set; } = 200;
}
}
}

0 comments on commit e79f6e2

Please sign in to comment.