Skip to content

Commit

Permalink
Remove unnecssary code, change method names
Browse files Browse the repository at this point in the history
Issue #332
  • Loading branch information
towsey committed Aug 18, 2020
1 parent 0e429f7 commit 5031e9a
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 126 deletions.
121 changes: 0 additions & 121 deletions src/AudioAnalysisTools/DSP/DSP_Filters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,127 +20,6 @@ public static partial class DspFilters
{
public const double Pi = Math.PI;

public static void TestMethod_GenerateSignal1()
{
int sampleRate = 22050;
double duration = 20; // signal duration in seconds
int[] harmonics = { 500, 1000, 2000, 4000, 8000 };
int windowSize = 512;
var freqScale = new FrequencyScale(sampleRate / 2, windowSize, 1000);
string path = @"C:\SensorNetworks\Output\Sonograms\UnitTestSonograms\SineSignal1.png";

var recording = GenerateTestRecording(sampleRate, duration, harmonics, WaveType.Cosine);
var sonoConfig = new SonogramConfig
{
WindowSize = freqScale.WindowSize,
WindowOverlap = 0.0,
SourceFName = "Signal1",
NoiseReductionType = NoiseReductionType.Standard,
NoiseReductionParameter = 0.12,
};
var sonogram = new AmplitudeSonogram(sonoConfig, recording.WavReader);

// pick a row, any row
var oneSpectrum = MatrixTools.GetRow(sonogram.Data, 40);
oneSpectrum = DataTools.normalise(oneSpectrum);
var peaks = DataTools.GetPeaks(oneSpectrum, 0.5);
for (int i = 2; i < peaks.Length - 2; i++)
{
if (peaks[i])
{
LoggedConsole.WriteLine($"bin ={freqScale.BinBounds[i, 0]}, Herz={freqScale.BinBounds[i, 1]}-{freqScale.BinBounds[i + 1, 1]} ");
}
}

if (peaks[11] && peaks[22] && peaks[45] && peaks[92] && peaks[185])
{
LoggedConsole.WriteSuccessLine("Spectral Peaks found at correct places");
}
else
{
LoggedConsole.WriteErrorLine("Spectral Peaks found at INCORRECT places");
}

foreach (int h in harmonics)
{
LoggedConsole.WriteLine($"Harmonic {h}Herz should be in bin {freqScale.GetBinIdForHerzValue(h)}");
}

// spectrogram without framing, annotation etc
var image = sonogram.GetImage();
string title = $"Spectrogram of Harmonics: {DataTools.Array2String(harmonics)} SR={sampleRate} Window={windowSize}";
image = sonogram.GetImageFullyAnnotated(image, title, freqScale.GridLineLocations);
image.Save(path);
}

public static void TestMethod_GenerateSignal2()
{
int sampleRate = 64000;
double duration = 30; // signal duration in seconds
int[] harmonics = { 500, 1000, 2000, 4000, 8000 };

//var freqScale = new FrequencyScale(FreqScaleType.Linear125OctaveTones28Nyquist32000);
int nyquist = sampleRate / 2;
int frameSize = 16384;
int linearBound = 125;
int octaveToneCount = 28;
int gridInterval = 1000;
var freqScale = new FrequencyScale(FreqScaleType.OctaveCustom, nyquist, frameSize, linearBound, octaveToneCount, gridInterval);
string path = @"C:\SensorNetworks\Output\Sonograms\UnitTestSonograms\SineSignal2.png";
var recording = GenerateTestRecording(sampleRate, duration, harmonics, WaveType.Cosine);
var epsilon = recording.Epsilon;

// init the default sonogram config
var sonoConfig = new SonogramConfig
{
WindowSize = freqScale.WindowSize,
WindowOverlap = 0.2,
SourceFName = "Signal2",
NoiseReductionType = NoiseReductionType.None,
NoiseReductionParameter = 0.0,
};

var sonogram = new AmplitudeSonogram(sonoConfig, recording.WavReader);
sonogram.Data = OctaveFreqScale.ConvertAmplitudeSpectrogramToDecibelOctaveScale(sonogram.Data, freqScale, epsilon);

// pick a row, any row
var oneSpectrum = MatrixTools.GetRow(sonogram.Data, 40);
oneSpectrum = DataTools.normalise(oneSpectrum);
var peaks = DataTools.GetPeaks(oneSpectrum, 0.5);

var peakIds = new List<int>();
for (int i = 5; i < peaks.Length - 5; i++)
{
if (peaks[i])
{
int peakId = freqScale.BinBounds[i, 0];
peakIds.Add(peakId);
LoggedConsole.WriteLine($"Spectral peak located in bin {peakId}, Herz={freqScale.BinBounds[i, 1]}");
}
}

//if (peaks[129] && peaks[257] && peaks[513] && peaks[1025] && peaks[2049])
if (peakIds[0] == 129 && peakIds[1] == 257 && peakIds[2] == 513 && peakIds[3] == 1025 && peakIds[4] == 2049)
{
LoggedConsole.WriteSuccessLine("Spectral Peaks found at correct places");
}
else
{
LoggedConsole.WriteErrorLine("Spectral Peaks found at INCORRECT places");
}

foreach (int h in harmonics)
{
LoggedConsole.WriteLine($"Harmonic {h}Hertz should be in bin {freqScale.GetBinIdForHerzValue(h)}");
}

// spectrogram without framing, annotation etc
var image = sonogram.GetImage();
string title = $"Spectrogram of Harmonics: {DataTools.Array2String(harmonics)} SR={sampleRate} Window={freqScale.WindowSize}";
image = sonogram.GetImageFullyAnnotated(image, title, freqScale.GridLineLocations);
image.Save(path);
}

public static AudioRecording GenerateTestRecording(int sampleRate, double duration, int[] harmonics, WaveType waveType)
{
var signal = GenerateTestSignal(sampleRate, duration, harmonics, waveType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static EventStatistics AnalyzeAudioEvent(
stats.SnrDecibels = max;

// Now need to convert event matrix back to energy values before calculating other statistics
eventMatrix = MatrixTools.Decibels2Power(eventMatrix);
eventMatrix = MatrixTools.SpectrogramDecibels2Power(eventMatrix);

var columnAverages = MatrixTools.GetColumnAverages(eventMatrix);
var rowAverages = MatrixTools.GetRowAverages(eventMatrix);
Expand Down
4 changes: 1 addition & 3 deletions src/AudioAnalysisTools/SpectralPeakTracks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,8 @@ public static SpectralPeakTracks CalculateSpectralPeakTracks(AudioRecording reco
double[,] decibelSpectrogram;
if (octaveScale)
{
//TODO: Implement octave scale in this situation.
throw new System.Exception("Octave scale not currently implemented in this context.");
var freqScale = new FrequencyScale(FreqScaleType.OctaveStandard);
decibelSpectrogram = OctaveFreqScale.DecibelSpectra(dspOutput.AmplitudeSpectrogram, dspOutput.WindowPower, sampleRate, epsilon, freqScale);
decibelSpectrogram = OctaveFreqScale.ConvertAmplitudeSpectrogramToFreqScaledDecibels(dspOutput.AmplitudeSpectrogram, dspOutput.WindowPower, sampleRate, epsilon, freqScale);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ public SpectrogramOctaveScale(SonogramConfig config, FrequencyScale scale, WavRe

/// <summary>
/// Converts amplitude matrix to octave frequency scale spectrogram.
/// IMPORTANT: DOES NOISE REDUCTION after conversion.
/// </summary>
/// <param name="amplitudeM">Matrix of amplitude values.</param>
public override void Make(double[,] amplitudeM)
{
double[,] m = OctaveFreqScale.ConvertAmplitudeSpectrogramToDecibelOctaveScale(amplitudeM, this.FreqScale, this.Configuration.epsilon);
double windowPower = this.Configuration.WindowPower;
int sampleRate = this.SampleRate;
double epsilon = this.Configuration.epsilon;
double[,] m = OctaveFreqScale.ConvertAmplitudeSpectrogramToFreqScaledDecibels(amplitudeM, windowPower, sampleRate, epsilon, this.FreqScale);

// Do noise reduction
var tuple = SNR.NoiseReduce(m, this.Configuration.NoiseReductionType, this.Configuration.NoiseReductionParameter);
Expand Down

0 comments on commit 5031e9a

Please sign in to comment.