|
| 1 | +// -------------------------------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="PulseTrain.cs" company="QutEcoacoustics"> |
| 3 | +// 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). |
| 4 | +// </copyright> |
| 5 | +// <summary> |
| 6 | +// This class contains methods to recognise pulse trains. |
| 7 | +// It is an alternative to using the Oscillations class. |
| 8 | + |
| 9 | + |
| 10 | +using System; |
| 11 | +using System.Collections.Generic; |
| 12 | +using System.Linq; |
| 13 | +using System.Text; |
| 14 | +using System.Threading.Tasks; |
| 15 | + |
| 16 | +namespace TowseyLibrary |
| 17 | +{ |
| 18 | + using Acoustics.Shared.ConfigFile; |
| 19 | + using MathNet.Numerics.LinearAlgebra.Solvers; |
| 20 | + |
| 21 | + public static class PulseTrain |
| 22 | + { |
| 23 | + /// <summary> |
| 24 | + /// This method creates a template to recognise two pulses that are possibly part of a pulse train. |
| 25 | + /// The template is designed to detect pulse trains of at least 2 pulses! |
| 26 | + /// The template is bounded either end by silence and then a pulse. i.e. .:|:. ... .:|:. where .=zero or a negative residual value, := 0.5 and |= 1.0. |
| 27 | + /// Any number of residual values may separate the pulses at either end. In this method, templates are created with 6 non-zero values and the remainder are negative. |
| 28 | + /// The sum of the positive values = 4.0. |
| 29 | + /// The sum of the values in the template should = zero. |
| 30 | + /// Designed this way, the minimum pulse length is about 4 or 5 and the minimum template length is about 10; |
| 31 | + /// </summary> |
| 32 | + /// <param name="pulseLength">length or number of frames between two pulses.</param> |
| 33 | + /// <returns>the template.</returns> |
| 34 | + public static double[] GetPulseTrainTemplate(int pulseLength) |
| 35 | + { |
| 36 | + int templateLength = pulseLength + 5; |
| 37 | + double residual = 4 / (double)(templateLength - 6); |
| 38 | + |
| 39 | + var template = new double[templateLength]; |
| 40 | + template[0] = -residual; |
| 41 | + template[1] = 0.5; |
| 42 | + template[2] = 1.0; |
| 43 | + template[3] = 0.5; |
| 44 | + for (int i = 4; i < templateLength - 4; i++) |
| 45 | + { |
| 46 | + template[i] = -residual; |
| 47 | + } |
| 48 | + |
| 49 | + template[templateLength - 4] = 0.5; |
| 50 | + template[templateLength - 3] = 1.0; |
| 51 | + template[templateLength - 2] = 0.5; |
| 52 | + template[templateLength - 1] = -residual; |
| 53 | + template = DataTools.normalise2UnitLength(template); |
| 54 | + return template; |
| 55 | + } |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// Only three pulses included in the single template output by this method. |
| 59 | + /// Will generalise if it seems worthwhile. |
| 60 | + /// </summary> |
| 61 | + public static double[] GetPulseTrainTemplate(int pulseLength, int pulseCount) |
| 62 | + { |
| 63 | + int templateLength = (pulseLength * pulseCount) + 5; |
| 64 | + var template = new double[templateLength]; |
| 65 | + int templateHalfLength = templateLength / 2; |
| 66 | + |
| 67 | + for (int i = 0; i < templateLength; i++) |
| 68 | + { |
| 69 | + template[i] = -1.0; |
| 70 | + } |
| 71 | + |
| 72 | + template[1] = 0.3; |
| 73 | + template[2] = 1.0; |
| 74 | + template[3] = 0.3; |
| 75 | + |
| 76 | + template[templateHalfLength - 1] = 0.3; |
| 77 | + template[templateHalfLength] = 1.0; |
| 78 | + template[templateHalfLength + 1] = 0.3; |
| 79 | + |
| 80 | + template[templateLength - 4] = 0.3; |
| 81 | + template[templateLength - 3] = 1.0; |
| 82 | + template[templateLength - 2] = 0.3; |
| 83 | + //template = DataTools.normalise2UnitLength(template); |
| 84 | + return template; |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// returns the length of a pulse interval in frames given pulses and frame rates in seconds. |
| 89 | + /// </summary> |
| 90 | + /// <param name="pulsesPerSecond">number of pulses per second.</param> |
| 91 | + /// <param name="framesPerSecond">frames per second - i.e. assuming the application is applied to a sequence of spectral frames.</param> |
| 92 | + /// <returns>the template.</returns> |
| 93 | + public static double[] GetPulseTrainTemplate(double pulsesPerSecond, double framesPerSecond) |
| 94 | + { |
| 95 | + int frameCount = (int)Math.Round(framesPerSecond / pulsesPerSecond); |
| 96 | + return GetPulseTrainTemplate(frameCount); |
| 97 | + } |
| 98 | + |
| 99 | + public static double[] GetPulseTrainScore(double[] signal, double pulsesPerSecond, double framesPerSecond, double thresholdValue) |
| 100 | + { |
| 101 | + int pulseCount = 2; |
| 102 | + int frameCount = (int)Math.Round(framesPerSecond / pulsesPerSecond); |
| 103 | + var templates = new List<double[]> |
| 104 | + { |
| 105 | + GetPulseTrainTemplate(frameCount, pulseCount), |
| 106 | + GetPulseTrainTemplate(frameCount - 1, pulseCount), |
| 107 | + GetPulseTrainTemplate(frameCount + 1, pulseCount), |
| 108 | + }; |
| 109 | + int signalLength = signal.Length; |
| 110 | + |
| 111 | + var scores = new double[signalLength]; |
| 112 | + |
| 113 | + for (int i = 2; i < signalLength - templates[2].Length; i++) |
| 114 | + { |
| 115 | + // skip if value is below threshold |
| 116 | + if (signal[i] < thresholdValue) |
| 117 | + { |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + // skip if value is not maximum |
| 122 | + if (signal[i] < signal[i - 1] || signal[i] < signal[i + 1]) |
| 123 | + { |
| 124 | + continue; |
| 125 | + } |
| 126 | + |
| 127 | + // get Cosine similarity for each of three templates. |
| 128 | + var templateScores = new double[3]; |
| 129 | + |
| 130 | + // get the local nh of signal for template 0 and get score |
| 131 | + var nh = DataTools.Subarray(signal, i, templates[0].Length); |
| 132 | + nh = DataTools.normalise2UnitLength(nh); |
| 133 | + templateScores[0] = DataTools.DotProduct(nh, templates[0]); |
| 134 | + |
| 135 | + // get the local nh of signal for template 1 |
| 136 | + nh = DataTools.Subarray(signal, i, templates[1].Length); |
| 137 | + nh = DataTools.normalise2UnitLength(nh); |
| 138 | + templateScores[1] = DataTools.DotProduct(nh, templates[1]); |
| 139 | + |
| 140 | + // get the local nh of signal for template 2 |
| 141 | + nh = DataTools.Subarray(signal, i, templates[2].Length); |
| 142 | + nh = DataTools.normalise2UnitLength(nh); |
| 143 | + templateScores[2] = DataTools.DotProduct(nh, templates[2]); |
| 144 | + |
| 145 | + double maxScore = templateScores.Max(); |
| 146 | + if (maxScore > 0.0) |
| 147 | + { |
| 148 | + for (int j = 0; j < templates[0].Length - 1; j++) |
| 149 | + { |
| 150 | + if (maxScore > scores[i + j]) |
| 151 | + { |
| 152 | + scores[i + j] = maxScore; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + return scores; |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments