Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrected Signal.hammingWindow implementation. #4324

Merged
merged 2 commits into from
Jun 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions HelpSource/Classes/Signal.schelp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ the number of samples of the size that is zero padding.

method::hammingWindow
Fill a Signal of the given size with a Hamming window.

warning::
In versions of SuperCollider before 3.11, the implementation of code:: Signal.hammingWindow :: had incorrect coefficients. To get the old behavior back, use code:: Signal.hammingWindow_old ::.
::

code::
Signal.hammingWindow(1024).plot;
Signal.hammingWindow(1024, 512).plot;
Expand Down Expand Up @@ -129,6 +134,10 @@ code::
Signal.fftCosTable(512).plot;
::

method::hammingWindow_old

This used to be code:: Signal.hammingWindow ::, but the coefficients were incorrect (making it a different window in the generalized Hamming window family). It is provided to assist in porting code to 3.11 and later.

INSTANCEMETHODS::

private::performBinaryOpOnSignal, performBinaryOpOnComplex, performBinaryOpOnSimpleNumber
Expand Down
9 changes: 8 additions & 1 deletion SCClassLibrary/Common/Math/Signal.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ Signal[float] : FloatArray {
*chebyFill { arg size, amplitudes, normalize=true, zeroOffset=false;
^Signal.newClear(size).chebyFill(amplitudes, normalize, zeroOffset);
}
*hammingWindow { arg size, pad=0;
*hammingWindow_old { arg size, pad=0;
if (pad == 0, {
^this.newClear(size).fill(0.5).addSine(1, 0.39, -0.5pi);
},{
^this.newClear(size-pad).fill(0.5).addSine(1, 0.39, -0.5pi) ++ this.newClear(pad);
});
}
*hammingWindow { arg size, pad=0;
if (pad == 0, {
^this.newClear(size).fill(0.53836).addSine(1, 0.46164, -0.5pi);
},{
^this.newClear(size).fill(0.53836).addSine(1, 0.46164, -0.5pi) ++ this.newClear(pad);
});
}
*hanningWindow { arg size, pad=0;
if (pad == 0, {
^this.newClear(size).fill(0.5).addSine(1, 0.5, -0.5pi);
Expand Down
20 changes: 20 additions & 0 deletions testsuite/classlibrary/TestSignal.sc
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,26 @@ TestSignal : UnitTest {

}

test_hanningWindow_max1 {
var window = Signal.hanningWindow(1024);
this.assertFloatEquals(window.maxItem, 1.0, "Signal.hanningWindow has maximum of 1.0 for even window size");
}

test_hammingWindow_max1 {
var window = Signal.hammingWindow(1024);
this.assertFloatEquals(window.maxItem, 1.0, "Signal.hammingWindow has maximum of 1.0 for even window size");
}

test_rectWindow_max1 {
var window = Signal.rectWindow(1024);
this.assertFloatEquals(window.maxItem, 1.0, "Signal.rectWindow has maximum of 1.0");
}

test_welchWindow_max1 {
var window = Signal.welchWindow(1024);
this.assertFloatEquals(window.maxItem, 1.0, "Signal.welchWindow has maximum of 1.0 for even window size");
}

// This is just a helper method
getSignalMidValue { arg sig;
var midIndex = sig.size >> 1;
Expand Down