Skip to content

Commit

Permalink
Setting default FFT window to Hanning
Browse files Browse the repository at this point in the history
Issue #291 Have set the default FFT window to Hanning for calculation of all summary and spectral indices.
However left the Test methods using HAMMING window so do not have to make changes to the output.
  • Loading branch information
towsey authored and atruskie committed Aug 17, 2020
1 parent 055571e commit 8a89100
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/AnalysisPrograms/Create4Sonograms.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public static void Main(Arguments arguments)

var recording = new AudioRecording(fiOutputSegment.FullName);

// EXTRACT ENVELOPE and SPECTROGRAM
// EXTRACT ENVELOPE and SPECTROGRAM// This call uses the default FFT window.
var dspOutput = DSP_Frames.ExtractEnvelopeAndFfts(recording, frameSize, windowOverlap);

// average absolute value over the minute recording
Expand Down
1 change: 1 addition & 0 deletions src/AnalysisPrograms/SnrAnalysis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public static void Execute(Arguments arguments)
int frameCount = signalLength / stepSize;

// (B) ################################## EXTRACT ENVELOPE and SPECTROGRAM ##################################
// Calling this method will set the default FFT window.
var dspOutput = DSP_Frames.ExtractEnvelopeAndFfts(
recording,
sonoConfig.WindowSize,
Expand Down
17 changes: 13 additions & 4 deletions src/AudioAnalysisTools/DSP/DSP_Frames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,31 @@ public static int FrameStep(int windowSize, double windowOverlap)
return frames;
}

/// <summary>
/// Calling this method will set default FFT window if windowName is null.
/// Otherwise sets the FFT window specified in the config file.
/// </summary>
public static EnvelopeAndFft ExtractEnvelopeAndFfts(AudioRecording recording, int frameSize, double overlap, string windowName = null)
{
int frameStep = (int)(frameSize * (1 - overlap));
return ExtractEnvelopeAndAmplSpectrogram(recording.WavReader.Samples, recording.SampleRate, recording.Epsilon, frameSize, frameStep, windowName);
}

/// <summary>
/// Calling this method sets the default FFT window, currently HANNING - see FFT.cs line 22.
/// </summary>
public static EnvelopeAndFft ExtractEnvelopeAndFfts(AudioRecording recording, int frameSize, int frameStep)
{
string windowName = FFT.KeyHammingWindow;
return ExtractEnvelopeAndAmplSpectrogram(recording.WavReader.Samples, recording.SampleRate, recording.Epsilon, frameSize, frameStep, windowName);
return ExtractEnvelopeAndAmplSpectrogram(recording.WavReader.Samples, recording.SampleRate, recording.Epsilon, frameSize, frameStep, FFT.DefaultFftWindow);
}

/// <summary>
/// Calling this method sets the default FFT window, currently HANNING - see FFT.cs line 22.
/// </summary>
public static EnvelopeAndFft ExtractEnvelopeAndAmplSpectrogram(double[] signal, int sampleRate, double epsilon, int frameSize, double overlap)
{
int frameStep = (int)(frameSize * (1 - overlap));
return ExtractEnvelopeAndAmplSpectrogram(signal, sampleRate, epsilon, frameSize, frameStep, FFT.KeyHammingWindow);
return ExtractEnvelopeAndAmplSpectrogram(signal, sampleRate, epsilon, frameSize, frameStep, FFT.DefaultFftWindow);
}

/// <summary>
Expand Down Expand Up @@ -196,7 +205,7 @@ public static EnvelopeAndFft ExtractEnvelopeAndAmplSpectrogram(
// set up the FFT parameters
if (windowName == null)
{
windowName = FFT.KeyHammingWindow;
windowName = FFT.DefaultFftWindow;
}

FFT.WindowFunc w = FFT.GetWindowFunction(windowName);
Expand Down
7 changes: 6 additions & 1 deletion src/TowseyLibrary/FFT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public sealed class FFT
public const string KeyNoWindow = "NONE";
public const string KeyHammingWindow = "HAMMING";
public const string KeyHanningWindow = "HANNING";
public const string DefaultFftWindow = KeyHanningWindow;

public delegate double WindowFunc(int n, int N);

Expand Down Expand Up @@ -338,9 +339,13 @@ private static double lrw(double a0, double a1, double a2, double a3, int n, int
return 1.0 - (1.93 * c1) + (1.29 * c2) - (0.388 * c3) + (0.032 * c4);
};

/// <summary>
/// Returns an FFT window function given the name of the window type.
/// </summary>
/// <param name="name">FFT window name.</param>
/// <returns>FFT.WindowFunc.</returns>
public static WindowFunc GetWindowFunction(string name)
{
//FFT.WindowFunc windowFnc;
if (name.StartsWith(KeyHammingWindow))
{
return Hamming;
Expand Down

0 comments on commit 8a89100

Please sign in to comment.