Skip to content

Commit

Permalink
Produce Mel frequency scale spectorgrams
Browse files Browse the repository at this point in the history
Issue #332 Get Mel frequency scale spectrograms working.
Start work on Octave scale spectrograms.
  • Loading branch information
towsey committed Jul 30, 2020
1 parent 68a0ba2 commit 57cdec6
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public static SonogramConfig Load(string configFile)
/// <summary>
/// Initializes a new instance of the <see cref="SonogramConfig"/> class.
/// Default Constructor - initialises a configuration with the default values.
/// This sets the default values for most spectrogram types, including Mel-scale and MFCC spectrograms.
/// </summary>
public SonogramConfig()
{
Expand Down
14 changes: 4 additions & 10 deletions src/AudioAnalysisTools/StandardSpectrograms/SpectrogramMelScale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public SpectrogramMelScale(AmplitudeSonogram sg, int minHz, int maxHz)
/// <param name="amplitudeM">Matrix of amplitude values.</param>
public override void Make(double[,] amplitudeM)
{
var tuple = MakeMelScaleSpectrogram(this.Configuration, amplitudeM, this.DecibelsNormalised, this.SampleRate);
var tuple = MakeMelScaleSpectrogram(this.Configuration, amplitudeM, this.SampleRate);
this.Data = tuple.Item1;
this.ModalNoiseProfile = tuple.Item2; //store the full bandwidth modal noise profile
}
Expand All @@ -76,34 +76,28 @@ public override void Make(double[,] amplitudeM)
/// <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)
protected static Tuple<double[,], double[]> MakeMelScaleSpectrogram(SonogramConfig config, double[,] matrix, 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. (" +
"## FATAL ERROR in MakeMelScaleSpectrogram(): 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);
m = MFCCStuff.MelFilterBank(m, bandCount, nyquist, 0, nyquist);

Log.WriteIfVerbose("\tDim after filter bank=" + m.GetLength(1) + " (Max filter bank=" + bandCount + ")");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public SpectrogramOctaveScale(AmplitudeSonogram sg, int minHz, int maxHz)
/// <param name="amplitudeM">Matrix of amplitude values.</param>
public override void Make(double[,] amplitudeM)
{
var tuple = MakeOctaveScaleSpectrogram(this.Configuration, amplitudeM, this.DecibelsNormalised, this.SampleRate);
var tuple = MakeOctaveScaleSpectrogram(this.Configuration, amplitudeM, this.SampleRate);
this.Data = tuple.Item1;
this.ModalNoiseProfile = tuple.Item2; //store the full bandwidth modal noise profile
}
Expand All @@ -82,35 +82,29 @@ public override void Make(double[,] amplitudeM)
/// <summary>
/// NOTE!!!! The decibel array has been normalised in 0 - 1.
/// </summary>
protected static Tuple<double[,], double[]> MakeOctaveScaleSpectrogram(SonogramConfig config, double[,] matrix, double[] decibels, int sampleRate)
protected static Tuple<double[,], double[]> MakeOctaveScaleSpectrogram(SonogramConfig config, double[,] matrix, 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. (" +
"## FATAL ERROR in MakeOctaveScaleSpectrogram(): Filterbank Count > number of FFT bins. (" +
bandCount + " > " + fftBinCount + ")\n\n");
}

//this is the filter count for full bandwidth 0-Nyquist
// TODO ADJUST THIS TO OCTAVE SCALE
m = doMelScale ? MFCCStuff.MelFilterBank(m, bandCount, nyquist, minHz, maxHz) : MFCCStuff.LinearFilterBank(m, bandCount, nyquist, minHz, maxHz);
m = MFCCStuff.MelFilterBank(m, bandCount, nyquist, 0, nyquist);

Log.WriteIfVerbose("\tDim after filter bank=" + m.GetLength(1) + " (Max filter bank=" + bandCount + ")");

Expand Down

0 comments on commit 57cdec6

Please sign in to comment.