-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up structure for two new spectrograms
Issue #332 Set up basic structure for including mel-scale and octave-scale spectrograms in the type of generated standard scale spectrograms..
- Loading branch information
Showing
5 changed files
with
368 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
src/AudioAnalysisTools/StandardSpectrograms/SpectrogramMelScale.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// <copyright file="SpectrogramCepstral.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 AudioAnalysisTools.StandardSpectrograms | ||
{ | ||
using System; | ||
using Acoustics.Tools.Wav; | ||
using AudioAnalysisTools.DSP; | ||
using AudioAnalysisTools.WavTools; | ||
using TowseyLibrary; | ||
|
||
public class SpectrogramMelScale : BaseSonogram | ||
{ | ||
public SpectrogramMelScale(string configFile, WavReader wav) | ||
: this(SonogramConfig.Load(configFile), wav) | ||
{ | ||
} | ||
|
||
public SpectrogramMelScale(SonogramConfig config, WavReader wav) | ||
: base(config, wav) | ||
{ | ||
} | ||
|
||
public SpectrogramMelScale(AmplitudeSonogram sg) | ||
: base(sg.Configuration) | ||
{ | ||
this.Configuration = sg.Configuration; | ||
this.DecibelsPerFrame = sg.DecibelsPerFrame; | ||
this.DecibelsNormalised = sg.DecibelsNormalised; | ||
this.Duration = sg.Duration; | ||
this.FrameCount = sg.FrameCount; | ||
this.DecibelReference = sg.DecibelReference; | ||
this.MaxAmplitude = sg.MaxAmplitude; | ||
this.SampleRate = sg.SampleRate; | ||
this.SigState = sg.SigState; | ||
this.SnrData = sg.SnrData; | ||
this.Data = sg.Data; | ||
|
||
//converts amplitude matrix to cepstral sonogram | ||
this.Make(this.Data); | ||
} | ||
|
||
public SpectrogramMelScale(AmplitudeSonogram sg, int minHz, int maxHz) | ||
: this(sg) | ||
{ | ||
this.DecibelsPerFrame = sg.DecibelsPerFrame; | ||
this.DecibelsNormalised = sg.DecibelsNormalised; | ||
this.Duration = sg.Duration; | ||
this.FrameCount = sg.FrameCount; | ||
this.DecibelReference = sg.DecibelReference; | ||
this.MaxAmplitude = sg.MaxAmplitude; | ||
this.SampleRate = sg.SampleRate; | ||
this.SigState = sg.SigState; | ||
this.SnrData = sg.SnrData; | ||
|
||
this.Data = SpectrogramTools.ExtractFreqSubband(sg.Data, minHz, maxHz, this.Configuration.DoMelScale, sg.Configuration.FreqBinCount, sg.FBinWidth); | ||
|
||
//converts amplitude matrix to mel-frequency scale spectrogram | ||
this.Make(this.Data); | ||
} | ||
|
||
/// <summary> | ||
/// Converts amplitude matrix to mel-frequency scale spectrogram. | ||
/// </summary> | ||
/// <param name="amplitudeM">Matrix of amplitude values.</param> | ||
public override void Make(double[,] amplitudeM) | ||
{ | ||
var tuple = MakeMelScaleSpectrogram(this.Configuration, amplitudeM, this.DecibelsNormalised, this.SampleRate); | ||
this.Data = tuple.Item1; | ||
this.ModalNoiseProfile = tuple.Item2; //store the full bandwidth modal noise profile | ||
} | ||
|
||
//################################################################################################################################## | ||
|
||
/// <summary> | ||
/// NOTE!!!! The decibel array has been normalised in 0 - 1. | ||
/// </summary> | ||
protected static Tuple<double[,], double[]> MakeMelScaleSpectrogram(SonogramConfig config, double[,] matrix, double[] decibels, int sampleRate) | ||
{ | ||
double[,] m = matrix; | ||
int nyquist = sampleRate / 2; | ||
double epsilon = config.epsilon; | ||
bool includeDelta = config.mfccConfig.IncludeDelta; | ||
bool includeDoubleDelta = config.mfccConfig.IncludeDoubleDelta; | ||
|
||
//(i) APPLY FILTER BANK | ||
int bandCount = config.mfccConfig.FilterbankCount; | ||
bool doMelScale = config.mfccConfig.DoMelScale; | ||
int ccCount = config.mfccConfig.CcCount; | ||
int fftBinCount = config.FreqBinCount; //number of Hz bands = 2^N +1. Subtract DC bin | ||
int minHz = config.MinFreqBand ?? 0; | ||
int maxHz = config.MaxFreqBand ?? nyquist; | ||
|
||
Log.WriteIfVerbose("ApplyFilterBank(): Dim prior to filter bank =" + matrix.GetLength(1)); | ||
|
||
//error check that filterBankCount < Number of FFT bins | ||
if (bandCount > fftBinCount) | ||
{ | ||
throw new Exception( | ||
"## FATAL ERROR in BaseSonogram.MakeCepstrogram():- Can't calculate cepstral coefficients. Filterbank Count > number of FFT bins. (" + | ||
bandCount + " > " + fftBinCount + ")\n\n"); | ||
} | ||
|
||
//this is the filter count for full bandwidth 0-Nyquist. This number is trimmed proportionately to fit the required bandwidth. | ||
m = doMelScale ? MFCCStuff.MelFilterBank(m, bandCount, nyquist, minHz, maxHz) : MFCCStuff.LinearFilterBank(m, bandCount, nyquist, minHz, maxHz); | ||
|
||
Log.WriteIfVerbose("\tDim after filter bank=" + m.GetLength(1) + " (Max filter bank=" + bandCount + ")"); | ||
|
||
//(ii) CONVERT AMPLITUDES TO DECIBELS | ||
m = MFCCStuff.DecibelSpectra(m, config.WindowPower, sampleRate, epsilon); //from spectrogram | ||
|
||
//(iii) NOISE REDUCTION | ||
var tuple1 = SNR.NoiseReduce(m, config.NoiseReductionType, config.NoiseReductionParameter); | ||
m = tuple1.Item1; | ||
|
||
//(iv) Normalize Matrix Values | ||
m = DataTools.normalise(m); | ||
|
||
var tuple2 = Tuple.Create(m, tuple1.Item2); | ||
|
||
// return matrix and full bandwidth modal noise profile | ||
return tuple2; | ||
} | ||
} // end class SpectrogramMelScale | ||
} |
Oops, something went wrong.