Skip to content

Commit

Permalink
implements HammingPeriodic and HanningPeriodic
Browse files Browse the repository at this point in the history
  • Loading branch information
gsgou committed Aug 18, 2023
1 parent 6f341bb commit a1f45fe
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 6 deletions.
20 changes: 19 additions & 1 deletion src/FftSharp.Tests/WindowValueTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using NUnit.Framework;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -70,6 +70,15 @@ public void Test_Hamming()
AssertEqual(Known_hamming_14, new Windows.Hamming().Create(14));
}

[Test]
public void Test_HammingPeriodic()
{
double[] Known_hamming_periodic_13 = { 0.08, 0.13269023, 0.27869022, 0.48455313, 0.70311825, 0.88431494,0.98663324, 0.98663324, 0.88431494, 0.70311825, 0.48455313, 0.27869022, 0.13269023 };
double[] Known_hamming_periodic_14 = { 0.08, 0.12555432, 0.25319469, 0.43764037, 0.64235963, 0.82680531, 0.95444568, 1.0, 0.95444568, 0.82680531, 0.64235963, 0.43764037, 0.25319469, 0.12555432 };
AssertEqual(Known_hamming_periodic_13, new Windows.HammingPeriodic().Create(13));
AssertEqual(Known_hamming_periodic_14, new Windows.HammingPeriodic().Create(14));
}

[Test]
public void Test_Hanning()
{
Expand All @@ -79,6 +88,15 @@ public void Test_Hanning()
AssertEqual(Known_hanning_14, new Windows.Hanning().Create(14));
}

[Test]
public void Test_HanningPeriodic()
{
double[] Known_hanning_periodic_13 = { 0.0, 0.05727199, 0.21596763, 0.43973166, 0.67730244, 0.87425537, 0.98547091, 0.98547091, 0.87425537, 0.67730244, 0.43973166, 0.21596763, 0.05727199 };
double[] Known_hanning_periodic_14 = { 0.0, 0.04951557, 0.1882551, 0.38873953, 0.61126047, 0.8117449, 0.95048443, 1.0, 0.95048443, 0.8117449, 0.61126047, 0.38873953, 0.1882551, 0.04951557 };
AssertEqual(Known_hanning_periodic_13, new Windows.HanningPeriodic().Create(13));
AssertEqual(Known_hanning_periodic_14, new Windows.HanningPeriodic().Create(14));
}

[Test]
public void Test_Rectangular()
{
Expand Down
9 changes: 6 additions & 3 deletions src/FftSharp/Windows/Hamming.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ public class Hamming : Window, IWindow
public override string Description =>
"The Hamming window has a sinusoidal shape does NOT touch zero at the edges (unlike the similar Hanning window). " +
"It is similar to the Hanning window but its abrupt edges are designed to cancel the largest side lobe. " +
"It may be a good choice for low-quality (8-bit) auto where side lobes lie beyond the quantization noise floor.";
"It may be a good choice for low-quality (8-bit) auto where side lobes lie beyond the quantization noise floor." +
"A symmetric window, for use in filter design.";

public override double[] Create(int size, bool normalize = false)
{
double[] window = new double[size];

double phaseStep = (2.0 * Math.PI) / (size - 1.0);

for (int i = 0; i < size; i++)
window[i] = 0.54 - 0.46 * Math.Cos(2 * Math.PI * i / (size - 1));
window[i] = 0.54 - 0.46 * Math.Cos(i * phaseStep);

if (normalize)
NormalizeInPlace(window);

return window;
}
}
}
}
29 changes: 29 additions & 0 deletions src/FftSharp/Windows/HammingPeriodic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

namespace FftSharp.Windows
{
public class HammingPeriodic : Window, IWindow
{
public override string Name => "HammingPeriodic";
public override string Description =>
"The Hamming window has a sinusoidal shape does NOT touch zero at the edges (unlike the similar Hanning window). " +
"It is similar to the Hanning window but its abrupt edges are designed to cancel the largest side lobe. " +
"It may be a good choice for low-quality (8-bit) auto where side lobes lie beyond the quantization noise floor." +
"A periodic window, for use in spectral analysis.";

public override double[] Create(int size, bool normalize = false)
{
double[] window = new double[size];

double phaseStep = (2.0 * Math.PI) / size;

for (int i = 0; i < size; i++)
window[i] = 0.54 - 0.46 * Math.Cos(i * phaseStep);

if (normalize)
NormalizeInPlace(window);

return window;
}
}
}
7 changes: 5 additions & 2 deletions src/FftSharp/Windows/Hanning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ public class Hanning : Window, IWindow
public override string Description =>
"The Hanning window has a sinusoidal shape which touches zero at the edges (unlike the similar Hamming window). " +
"It has good frequency resolution, low spectral leakage, and is satisfactory for 95 percent of use cases. " +
"If you do not know the nature of the signal but you want to apply a smoothing window, start with the Hann window.";
"If you do not know the nature of the signal but you want to apply a smoothing window, start with the Hann window." +
"A symmetric window, for use in filter design.";

public override double[] Create(int size, bool normalize = false)
{
double[] window = new double[size];

double phaseStep = (2.0 * Math.PI) / (size - 1.0);

for (int i = 0; i < size; i++)
window[i] = 0.5 - 0.5 * Math.Cos(2 * Math.PI * i / (size - 1));
window[i] = 0.5 - 0.5 * Math.Cos(i * phaseStep);

if (normalize)
NormalizeInPlace(window);
Expand Down
29 changes: 29 additions & 0 deletions src/FftSharp/Windows/HanningPeriodic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;

namespace FftSharp.Windows
{
public class HanningPeriodic : Window, IWindow
{
public override string Name => "HanningPeriodic";
public override string Description =>
"The Hanning window has a sinusoidal shape which touches zero at the edges (unlike the similar Hamming window). " +
"It has good frequency resolution, low spectral leakage, and is satisfactory for 95 percent of use cases. " +
"If you do not know the nature of the signal but you want to apply a smoothing window, start with the Hann window." +
"A periodic window, for use in spectral analysis.";

public override double[] Create(int size, bool normalize = false)
{
double[] window = new double[size];

double phaseStep = (2.0 * Math.PI) / size;

for (int i = 0; i < size; i++)
window[i] = 0.5 - 0.5 * Math.Cos(i * phaseStep);

if (normalize)
NormalizeInPlace(window);

return window;
}
}
}

0 comments on commit a1f45fe

Please sign in to comment.