Skip to content

Commit

Permalink
Incorporate epsilon in to log transform
Browse files Browse the repository at this point in the history
Issue #332 In some cases, log being calculated without catching zeros. Incorporate e;silon into log transform.
  • Loading branch information
towsey committed Aug 17, 2020
1 parent b423044 commit 6dbe9ba
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 68 deletions.
4 changes: 3 additions & 1 deletion src/AudioAnalysisTools/DSP/DSP_Filters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public static void TestMethod_GenerateSignal2()
var freqScale = new FrequencyScale(FreqScaleType.OctaveCustom, nyquist, frameSize, linearBound, octaveToneCount, gridInterval);
string path = @"C:\SensorNetworks\Output\Sonograms\UnitTestSonograms\SineSignal2.png";
var recording = GenerateTestRecording(sampleRate, duration, harmonics, WaveType.Cosine);
var epsilon = recording.Epsilon;

// init the default sonogram config
var sonoConfig = new SonogramConfig
Expand All @@ -98,8 +99,9 @@ public static void TestMethod_GenerateSignal2()
NoiseReductionType = NoiseReductionType.None,
NoiseReductionParameter = 0.0,
};

var sonogram = new AmplitudeSonogram(sonoConfig, recording.WavReader);
sonogram.Data = OctaveFreqScale.ConvertAmplitudeSpectrogramToDecibelOctaveScale(sonogram.Data, freqScale);
sonogram.Data = OctaveFreqScale.ConvertAmplitudeSpectrogramToDecibelOctaveScale(sonogram.Data, freqScale, epsilon);

// pick a row, any row
var oneSpectrum = MatrixTools.GetRow(sonogram.Data, 40);
Expand Down
3 changes: 2 additions & 1 deletion src/AudioAnalysisTools/Indices/IndexMatrices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,8 @@ public static Dictionary<string, double[]> AddDerivedIndices(Dictionary<string,
summaryIndices.Add("SqrtTempEntropy", DataTools.SquareRootOfValues(summaryIndices["TemporalEntropy"]));

// insert some transformed data columns
summaryIndices.Add("LogTempEntropy", DataTools.LogTransform(summaryIndices["TemporalEntropy"]));
var epsilon = Acoustics.Tools.Wav.WavReader.CalculateEpsilonForRescaledInteger(16);
summaryIndices.Add("LogTempEntropy", DataTools.Log10Values(summaryIndices["TemporalEntropy"], epsilon));

// Calculate Normalised Difference Soundscape Index if not already done
// caluclate two ratios for three bands. DO NOT CHANGE THESE KEYS
Expand Down
73 changes: 13 additions & 60 deletions src/TowseyLibrary/DataTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ public static int[] Peaks_CropToFirstAndLast(double[] data, double severity)
}

/// <summary>
/// Finds the local peaks in a vector.
/// Finds the local maxima in a vector.
/// </summary>
public static bool[] GetPeaks(double[] data)
{
Expand Down Expand Up @@ -2961,38 +2961,6 @@ public static double DotProduct(double[,] m1, double[,] m2)
return sum;
}

/// <summary>
/// convert values to Decibels.
/// Assume that all values are positive.
/// </summary>
/// <param name="m">matrix of positive power values.</param>
public static double[,] DeciBels(double[,] m, out double min, out double max)
{
min = double.MaxValue;
max = -double.MaxValue;

int rows = m.GetLength(0);
int cols = m.GetLength(1);
double[,] ret = new double[rows, cols];

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
double dBels = 10 * Math.Log10(m[i, j]); // convert power to decibels

// NOTE: the decibels calculation should be a ratio.
// Here the ratio is implied ie relative to the power in the original normalised signal
// if (dBels <= min) min = dBels;
// else
// if (dBels >= max) max = dBels;
ret[i, j] = dBels;
}
}

return ret;
}

public static double[] Order(double[] array, double[] order)
{
int length = array.Length;
Expand Down Expand Up @@ -3254,8 +3222,10 @@ public static double[] NormaliseValues(int[] val, int normMin, int normMax)
}

/// <summary>
/// normalizes the passed array between 0,1.
/// Normalizes the passed array between 0,1.
/// Ensures all values are positive.
/// Minimum array value set = 0.0.
/// Maximum array value set = 1.0.
/// </summary>
public static double[] normalise(double[] v)
{
Expand Down Expand Up @@ -3676,30 +3646,13 @@ public static double[] SquareRootOfValues(double[] data)
return sqrtArray;
}

public static double[] LogTransform(double[] data)
{
if (data == null)
{
return null;
}

var logArray = new double[data.Length];
for (int i = 0; i < data.Length; i++)
{
if (data[i] <= 0.0)
{
logArray[i] = 0.0;
}
else
{
logArray[i] = Math.Log10(1 + data[i]);
}
}

return logArray;
}

public static double[] LogValues(double[] data)
/// <summary>
/// Gets the log10 of an array of values.
/// Assume that all values are non-zero and positive.
/// </summary>
/// <param name="data">The input data array.</param>
/// <param name="epsilon">The smallest accepted non-zero value.</param>
public static double[] Log10Values(double[] data, double epsilon)
{
if (data == null)
{
Expand All @@ -3709,9 +3662,9 @@ public static double[] LogValues(double[] data)
var logArray = new double[data.Length];
for (int i = 0; i < data.Length; i++)
{
if (data[i] <= 0.0)
if (data[i] <= epsilon)
{
logArray[i] = -1000000.0;
logArray[i] = Math.Log10(epsilon);
}
else
{
Expand Down
16 changes: 10 additions & 6 deletions src/TowseyLibrary/MatrixTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -522,11 +522,13 @@ public static double[] Matrix2Array(double[,] matrix)
/// Convert the power values in a matrix of spectrogram values to Decibels.
/// Assume that all matrix values are positive i.e. due to prior noise removal.
/// NOTE: This method also returns the min and max decibel values in the passed matrix.
/// NOTE: A decibel value should be a ratio.
/// Here the ratio is implied ie it is relative to the value of maximum power in the original normalised signal.
/// </summary>
/// <param name="m">matrix of positive power values.</param>
/// <param name="min">min value to be return by out.</param>
/// <param name="max">max value to be return by out.</param>
public static double[,] Power2DeciBels(double[,] m, out double min, out double max)
public static double[,] Power2DeciBels(double[,] m, double epsilon, out double min, out double max)
{
min = double.MaxValue;
max = -double.MaxValue;
Expand All @@ -539,16 +541,18 @@ public static double[] Matrix2Array(double[,] matrix)
{
for (int j = 0; j < cols; j++)
{
double dBels = 10 * Math.Log10(m[i, j]); //convert power to decibels
double dBels = 10 * Math.Log10(epsilon); //convert power to decibels
if (m[i, j] > epsilon)
{
dBels = 10 * Math.Log10(m[i, j]); //convert power to decibels
}

//NOTE: the decibel calculation should be a ratio.
// Here the ratio is implied ie relative to the power in the original normalised signal
if (dBels <= min)
if (dBels < min)
{
min = dBels;
}
else
if (dBels >= max)
if (dBels > max)
{
max = dBels;
}
Expand Down

0 comments on commit 6dbe9ba

Please sign in to comment.