Skip to content

Commit

Permalink
Mostly remove unused code and tidy comments.
Browse files Browse the repository at this point in the history
Issue #332
  • Loading branch information
towsey committed Aug 11, 2020
1 parent 3efb633 commit 277a0de
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 84 deletions.
35 changes: 20 additions & 15 deletions src/AnalysisPrograms/PlanesTrainsAndAutomobiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,18 +185,23 @@ public static Tuple<BaseSonogram, double[,], double[], List<AcousticEvent>> Dete
TimeSpan duration = recording.Duration;
NoiseReductionType nrt = SNR.KeyToNoiseReductionType("STANDARD");

var sonogram = (BaseSonogram)SpectrogramStandard.GetSpectralSonogram(
recording.BaseName,
windowSize,
windowOverlap,
bitsPerSample,
windowPower,
sr,
duration,
nrt,
matrix);

sonogram.DecibelsNormalised = new double[rowCount];
//Set the default values config
SonogramConfig sonoConfig = new SonogramConfig
{
SourceFName = recording.BaseName,
WindowSize = windowSize,
WindowOverlap = windowOverlap,
NoiseReductionType = nrt,
epsilon = Math.Pow(0.5, bitsPerSample - 1),
WindowPower = windowPower,
SampleRate = sr,
Duration = duration,
};

var sonogram = new SpectrogramStandard(sonoConfig, matrix)
{
DecibelsNormalised = new double[rowCount],
};

//foreach frame or time step
for (int i = 0; i < rowCount; i++)
Expand All @@ -205,7 +210,7 @@ public static Tuple<BaseSonogram, double[,], double[], List<AcousticEvent>> Dete
}

sonogram.DecibelsNormalised = DataTools.normalise(sonogram.DecibelsNormalised);
return Tuple.Create(sonogram, hits, scoreArray, predictedEvents);
} //end Execute_HDDetect
return Tuple.Create((BaseSonogram)sonogram, hits, scoreArray, predictedEvents);
}
}
} //end class PlanesTrainsAndAutomobiles
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace AudioAnalysisTools.StandardSpectrograms
public class AmplitudeSonogram : BaseSonogram
{
public AmplitudeSonogram(SonogramConfig config, WavReader wav)
: base(config, wav, false)
: base(config, wav)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,6 @@ public SpectrogramStandard(SpectrogramStandard sg, double startTime, double endT
this.DecibelsPerFrame[i] = sg.DecibelsPerFrame[startFrame + i];
}

/*
// Subband functionality no longer available. Discontinued March 2017 because not being used
// this.subBandMinHz = sg.subBandMinHz; //min freq (Hz) of the required subband
// this.subBandMaxHz = sg.subBandMaxHz; //max freq (Hz) of the required subband
//sg.SnrSubband { get; private set; }
//this.DecibelsInSubband = new double[frameCount]; // Normalised decibels in extracted freq band
//for (int i = 0; i < frameCount; i++) this.DecibelsInSubband[i] = sg.DecibelsInSubband[startFrame + i];
*/

this.DecibelReference = sg.DecibelReference; // Used to NormaliseMatrixValues the dB values for MFCCs
this.DecibelsNormalised = new double[frameCount];
for (int i = 0; i < frameCount; i++)
Expand Down Expand Up @@ -142,52 +133,5 @@ public override void Make(double[,] amplitudeM)
this.SnrData.ModalNoiseProfile = tuple.Item2; // store the full bandwidth modal noise profile
}
}

/// <summary>
/// Normalise the dynamic range of spectrogram between 0dB and value of DynamicRange.
/// Also must adjust the SNR.DecibelsInSubband and this.DecibelsNormalised.
/// </summary>
public void NormaliseDynamicRange(double dynamicRange)
{
int frameCount = this.Data.GetLength(0);
int featureCount = this.Data.GetLength(1);
DataTools.MinMax(this.Data, out var minIntensity, out var maxIntensity);
double[,] newMatrix = new double[frameCount, featureCount];

//each row of matrix is a frame
for (int i = 0; i < frameCount; i++)
{
//each col of matrix is a feature
for (int j = 0; j < featureCount; j++)
{
newMatrix[i, j] = this.Data[i, j];
}
}

this.Data = newMatrix;
}

//##################################################################################################################################
//####### STATIC METHODS ###########################################################################################################
//##################################################################################################################################

public static SpectrogramStandard GetSpectralSonogram(string recordingFileName, int frameSize, double windowOverlap, int bitsPerSample, double windowPower, int sr,
TimeSpan duration, NoiseReductionType nrt, double[,] amplitudeSpectrogram)
{
SonogramConfig sonoConfig = new SonogramConfig
{
SourceFName = recordingFileName,
WindowSize = frameSize,
WindowOverlap = windowOverlap,
NoiseReductionType = nrt,
epsilon = Math.Pow(0.5, bitsPerSample - 1),
WindowPower = windowPower,
SampleRate = sr,
Duration = duration,
}; //default values config
var sonogram = new SpectrogramStandard(sonoConfig, amplitudeSpectrogram);
sonogram.SetTimeScale(duration);
return sonogram;
}
} //end of class SpectralSonogram : BaseSonogram
}
}
26 changes: 15 additions & 11 deletions src/TowseyLibrary/FFT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public FFT(int windowSize)

/// <summary>
/// Initializes a new instance of the <see cref="FFT"/> class.
/// wrapper for FFT.
/// Window Power equals sum of squared window values. Default window is Hamming.
/// It is a wrapper for calling method that does the FFT.
/// Window Power equals sum of squared window values.
/// </summary>
public FFT(int windowSize, WindowFunc w)
{
Expand Down Expand Up @@ -75,7 +75,7 @@ public FFT(int windowSize, WindowFunc w)
/// <summary>
/// Invokes an FFT on the given data array.
/// cdata contains the real and imaginary terms of the coefficients representing cos and sin components respectively.
/// cdata is symmetrical about terms 512 &amp; 513. Can ignore all coefficients 512 and above .
/// cdata is symmetrical about terms 512 &amp; 513. Can ignore all coefficients 512 and above.
/// </summary>
/// <param name="data">a single frame of signal values.</param>
public double[] Invoke(double[] data)
Expand All @@ -99,7 +99,8 @@ public double[] Invoke(double[] data)
}

//do the FFT
four1(cdata); //array contains real and imaginary values
//The cdata array contains real and imaginary values
Four1(cdata);

double[] f = new double[this.CoeffCount]; //array to contain amplitude data

Expand Down Expand Up @@ -135,26 +136,26 @@ public double[] Invoke(double[] data, int offset)
}

//do the FFT
four1(cdata);
Four1(cdata);

double[] f = new double[this.CoeffCount]; //array to contain amplitude data

// calculate amplitude
for (int i = 0; i < this.CoeffCount; i++)
{
f[i] = hypot(cdata[2 * i], cdata[(2 * i) + 1]);
f[i] = Hypotenuse(cdata[2 * i], cdata[(2 * i) + 1]);
}

return f;
}

private static double hypot(double x, double y)
private static double Hypotenuse(double x, double y)
{
return Math.Sqrt((x * x) + (y * y));
}

// from http://www.nrbook.com/a/bookcpdf/c12-2.pdf
private static void four1(double[] data)
private static void Four1(double[] data)
{
int nn = data.Length / 2;
int n = nn << 1;
Expand Down Expand Up @@ -273,10 +274,8 @@ public double[] InvokeDotNetFFT(double[] data)
return amplitude;
}

// from http://en.wikipedia.org/wiki/Window_function

/// <summary>
/// The Hamming window reduces the immediate adjacent sidelobes (conmpared to the Hanning) but at the expense of increased
/// The Hamming window reduces the immediate adjacent sidelobes (compared to the Hanning) but at the expense of increased
/// distal side-lobes. <see href="https://en.wikipedia.org/wiki/Window_function" />.
/// </summary>
public static readonly WindowFunc Hamming = (n, N) =>
Expand All @@ -287,6 +286,11 @@ public double[] InvokeDotNetFFT(double[] data)
return 0.54 - (0.46 * Math.Cos(x)); //MATLAB code uses these value and says it is better!
};

/// <summary>
/// See comment for Hanning that compares Hamming with Hanning.
/// Our experience with analysis of environmental recordings, where we often up or down sample in order to calculate indices,
/// is that the HANNING window is to be preferred and it was made the default in July 2020.
/// </summary>
public static readonly WindowFunc Hanning = (n, N) =>
{
double x = 2.0 * Math.PI * n / (N - 1);
Expand Down

0 comments on commit 277a0de

Please sign in to comment.