Skip to content

Commit

Permalink
Fix production of Octave spectrogram
Browse files Browse the repository at this point in the history
Issue #332
  • Loading branch information
towsey committed Aug 11, 2020
1 parent a3778e3 commit 834fa95
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public static AudioToSonogramResult GenerateSpectrogramImages(
// EXTRACT ENVELOPE and SPECTROGRAM FROM RECORDING SEGMENT
var dspOutput1 = DSP_Frames.ExtractEnvelopeAndFfts(recordingSegment, frameSize, frameStep);

// This constructor initalises default values for Melscale and Mfcc spectrograms and other parameters.
// This constructor initializes default values for Melscale and Mfcc spectrograms and other parameters.
var sonoConfig = new SonogramConfig()
{
epsilon = recordingSegment.Epsilon,
Expand Down Expand Up @@ -211,9 +211,29 @@ public static AudioToSonogramResult GenerateSpectrogramImages(
// IMAGE 8) Octave-frequency scale Spectrogram
if (@do.Contains(OctaveScaleSpectrogram))
{
//Create new config because calling the octave spectrogram changes it.
var octaveConfig = new SonogramConfig()
{
epsilon = recordingSegment.Epsilon,
SampleRate = sampleRate,
WindowSize = frameSize,
WindowStep = frameStep,
WindowOverlap = frameOverlap,
WindowPower = dspOutput1.WindowPower,
Duration = recordingSegment.Duration,
NoiseReductionType = NoiseReductionType.Standard,
NoiseReductionParameter = bgNoiseThreshold,
};

// Here are some of the possible octave frequency scales.
//var freqScale = new FrequencyScale(FreqScaleType.LinearOctaveStandard);
//var freqScale = new FrequencyScale(FreqScaleType.OctaveDataReduction);
//var freqScale = new FrequencyScale(FreqScaleType.Linear62OctaveTones31Nyquist11025);
//var freqScale = new FrequencyScale(FreqScaleType.Linear125OctaveTones32Nyquist11025);

images.Add(
OctaveScaleSpectrogram,
GetOctaveScaleSpectrogram(sonoConfig, recordingSegment, sourceRecordingName));
GetOctaveScaleSpectrogram(octaveConfig, FreqScaleType.Linear62OctaveTones31Nyquist11025, recordingSegment, sourceRecordingName));
}

// IMAGE 9) AmplitudeSpectrogram_LocalContrastNormalization
Expand Down Expand Up @@ -403,24 +423,34 @@ public static Image<Rgb24> GetCepstralSpectrogram(
}

public static Image<Rgb24> GetOctaveScaleSpectrogram(
SonogramConfig sonoConfig,
SonogramConfig sgConfig,
FreqScaleType fst,
AudioRecording recording,
string sourceRecordingName)
{
var freqScale = new FrequencyScale(fst)
{
WindowStep = 512,
};

// ensure that the freq scale and the config are consistent.
sgConfig.WindowSize = freqScale.WindowSize;
freqScale.WindowStep = sgConfig.WindowStep;
sgConfig.WindowOverlap = SonogramConfig.CalculateFrameOverlap(freqScale.WindowSize, freqScale.WindowStep);

// TODO at present noise reduction type must be set = Standard.
sonoConfig.NoiseReductionType = NoiseReductionType.Standard;
sonoConfig.NoiseReductionParameter = 3.0;
sgConfig.NoiseReductionType = NoiseReductionType.Standard;
sgConfig.NoiseReductionParameter = 3.0;

/// ########################################################################TODO TODO need to init as AmplitudeSpectrogram and then use constructor at Line 25 of SpectrogramOctaveScale..
var octaveScaleGram = new SpectrogramOctaveScale(sonoConfig, recording.WavReader);
var octaveScaleGram = new SpectrogramOctaveScale(sgConfig, freqScale, recording.WavReader);
var image = octaveScaleGram.GetImage();
var titleBar = BaseSonogram.DrawTitleBarOfGrayScaleSpectrogram(
"OCTAVE-SCALE SPECTROGRAM " + sourceRecordingName,
image.Width,
ImageTags[CepstralSpectrogram]);
var startTime = TimeSpan.Zero;
var xAxisTicInterval = TimeSpan.FromSeconds(1);
TimeSpan xAxisPixelDuration = TimeSpan.FromSeconds(sonoConfig.WindowStep / (double)sonoConfig.SampleRate);
TimeSpan xAxisPixelDuration = TimeSpan.FromSeconds(sgConfig.WindowStep / (double)sgConfig.SampleRate);
var labelInterval = TimeSpan.FromSeconds(5);
image = BaseSonogram.FrameSonogram(image, titleBar, startTime, xAxisTicInterval, xAxisPixelDuration, labelInterval);
return image;
Expand Down
12 changes: 5 additions & 7 deletions src/AudioAnalysisTools/DSP/OctaveFreqScale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,17 @@ namespace AudioAnalysisTools.DSP

public static class OctaveFreqScale
{
/// IMPORTANT NOTE: If you are converting Herz scale from LINEAR to OCTAVE, this conversion MUST be done BEFORE noise reduction
/// <summary>
/// CONSTRUCTION OF Frequency Scales
/// IMPORTANT NOTE: If you are converting Herz scale from LINEAR to OCTAVE, this conversion MUST be done BEFORE noise reduction
/// WARNING!: Changing the constants for the octave scales will have undefined effects.
/// The options below have been debugged to give what is required.
/// However other values have not been debugged - so user should check the output to ensure it is what is required.
/// NOTE: octaveDivisions = the number of fractional Hz steps within one octave. Piano octave contains 12 steps per octave.
/// </summary>
public static void GetOctaveScale(FrequencyScale scale)
{
int sr, frameSize;

// NOTE: octaveDivisions = the number of fractional Hz steps within one octave. Piano octave contains 12 steps per octave.

var fst = scale.ScaleType;

switch (fst)
Expand All @@ -43,7 +41,7 @@ public static void GetOctaveScale(FrequencyScale scale)

case FreqScaleType.Linear62OctaveTones31Nyquist11025:
sr = 22050;
frameSize = 8192;
frameSize = 2048;
scale.LinearBound = 62;

// tone steps within one octave. Note: piano = 12 steps per octave.
Expand All @@ -53,13 +51,13 @@ public static void GetOctaveScale(FrequencyScale scale)
case FreqScaleType.Linear125OctaveTones32Nyquist11025:
// constants required for split linear-octave scale when sr = 22050
sr = 22050;
frameSize = 8192;
frameSize = 2048;
scale.LinearBound = 125;
scale.ToneCount = 32; // tone steps within one octave. Note: piano = 12 steps per octave.
break;

case FreqScaleType.Octaves24Nyquist32000:
//// constants required for full octave scale when sr = 64000
// constants required for full octave scale when sr = 64000
sr = 64000;
frameSize = 16384;
scale.LinearBound = 15;
Expand Down

0 comments on commit 834fa95

Please sign in to comment.