Skip to content

Commit

Permalink
More work on Octave Frequency scales
Browse files Browse the repository at this point in the history
Issue #332
  • Loading branch information
towsey committed Aug 13, 2020
1 parent 0a643f1 commit 5908e27
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ public static Image<Rgb24> GetOctaveScaleSpectrogram(
AudioRecording recording,
string sourceRecordingName)
{
// ensure that the freq scale and the config are consistent.
// ensure that the freq scale and the spectrogram config are consistent.
sgConfig.WindowSize = freqScale.WindowSize;
freqScale.WindowStep = sgConfig.WindowStep;
sgConfig.WindowOverlap = SonogramConfig.CalculateFrameOverlap(freqScale.WindowSize, freqScale.WindowStep);
Expand All @@ -441,15 +441,15 @@ public static Image<Rgb24> GetOctaveScaleSpectrogram(

var octaveScaleGram = new SpectrogramOctaveScale(sgConfig, freqScale, recording.WavReader);
var image = octaveScaleGram.GetImage();
var titleBar = BaseSonogram.DrawTitleBarOfGrayScaleSpectrogram(
"OCTAVE-SCALE SPECTROGRAM " + sourceRecordingName,
image.Width,
ImageTags[OctaveScaleSpectrogram]);
var startTime = TimeSpan.Zero;
var xAxisTicInterval = TimeSpan.FromSeconds(1);
TimeSpan xAxisPixelDuration = TimeSpan.FromSeconds(sgConfig.WindowStep / (double)sgConfig.SampleRate);
var labelInterval = TimeSpan.FromSeconds(5);
image = BaseSonogram.FrameSonogram(image, titleBar, startTime, xAxisTicInterval, xAxisPixelDuration, labelInterval);
var title = "OCTAVE-SCALE SPECTROGRAM " + sourceRecordingName;

//var titleBar = BaseSonogram.DrawTitleBarOfGrayScaleSpectrogram(title, image.Width, ImageTags[OctaveScaleSpectrogram]);
//var startTime = TimeSpan.Zero;
//var xAxisTicInterval = TimeSpan.FromSeconds(1);
//TimeSpan xAxisPixelDuration = TimeSpan.FromSeconds(sgConfig.WindowStep / (double)sgConfig.SampleRate);
//var labelInterval = TimeSpan.FromSeconds(5);
//image = BaseSonogram.FrameSonogram(image, titleBar, startTime, xAxisTicInterval, xAxisPixelDuration, labelInterval);
image = octaveScaleGram.GetImageFullyAnnotated(image, title, freqScale.GridLineLocations);
return image;
}

Expand Down
20 changes: 6 additions & 14 deletions src/AudioAnalysisTools/DSP/FrequencyScale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ namespace AudioAnalysisTools.DSP
using System.IO;
using Acoustics.Shared.ImageSharp;
using AudioAnalysisTools.StandardSpectrograms;
using AudioAnalysisTools.WavTools;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
Expand All @@ -27,7 +26,7 @@ public enum FreqScaleType
Mel = 1,
OctaveCustom = 2,
OctaveStandard = 3,
OctaveDataReduction = 3,
OctaveDataReduction = 4,
}

public class FrequencyScale
Expand Down Expand Up @@ -220,24 +219,17 @@ public FrequencyScale(FreqScaleType fst)
public int[,] GridLineLocations { get; set; }

/// <summary>
/// returns the binId for the grid line closest to the passed frequency.
/// returns the binId for freq bin closest to the passed Hertz value.
/// </summary>
public int GetBinIdForHerzValue(int herzValue)
public int GetBinIdForHerzValue(int hertzValue)
{
int binId = 0;
int binCount = this.BinBounds.GetLength(0);

for (int i = 1; i < binCount; i++)
while (this.BinBounds[binId, 1] < hertzValue)
{
if (this.BinBounds[i, 1] > herzValue)
{
binId = this.BinBounds[i, 0];
break;
}
binId++;
}

// subtract 1 because have actually extracted the upper bin bound
return binId - 1;
return binId;
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/AudioAnalysisTools/DSP/OctaveFreqScale.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ public static FrequencyScale GetStandardOctaveScale(FrequencyScale scale)

public static int[,] GetGridLineLocations(int nyquist, int linearBound, int[,] octaveBinBounds)
{
int[] gridLocationsHertz = { 62, 125, 250, 500, 1000, 2000, 4000, 8000, 16000, 32000 };
int[] gridLocationsHertz = { 62, 125, 250, 500, 1000, 2000, 4000, 8000, 16000, 32000, 64000, 128000 };
int binCount = octaveBinBounds.GetLength(0);
var gridLineLocations = new List<int[]>();
var upperBound = nyquist * 0.75;
Expand Down

0 comments on commit 5908e27

Please sign in to comment.