From 8e2bd332e204f56587f334bffb301bbc063d14ca Mon Sep 17 00:00:00 2001 From: khoin Date: Fri, 22 Feb 2019 18:00:17 -0800 Subject: [PATCH 1/2] Corrected Signal.hammingWindow implementation. Old method moved to Signal.hammingWindow_old --- SCClassLibrary/Common/Math/Signal.sc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/SCClassLibrary/Common/Math/Signal.sc b/SCClassLibrary/Common/Math/Signal.sc index 1a7b6d7facd..a0ce0b11012 100644 --- a/SCClassLibrary/Common/Math/Signal.sc +++ b/SCClassLibrary/Common/Math/Signal.sc @@ -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); From 1680fbc8fdba29883da4b948ecb971ae1d37ef12 Mon Sep 17 00:00:00 2001 From: Nathan Ho Date: Sat, 23 Feb 2019 14:11:57 -0800 Subject: [PATCH 2/2] add unit test and help file (#1) * help: Document Signal.hammingWindow_old * sclang: Add unit tests for four Signal methods --- HelpSource/Classes/Signal.schelp | 9 +++++++++ testsuite/classlibrary/TestSignal.sc | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/HelpSource/Classes/Signal.schelp b/HelpSource/Classes/Signal.schelp index b5a1e5883f1..378546cc008 100644 --- a/HelpSource/Classes/Signal.schelp +++ b/HelpSource/Classes/Signal.schelp @@ -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; @@ -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 diff --git a/testsuite/classlibrary/TestSignal.sc b/testsuite/classlibrary/TestSignal.sc index add93cfb98f..6c109e14e85 100644 --- a/testsuite/classlibrary/TestSignal.sc +++ b/testsuite/classlibrary/TestSignal.sc @@ -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;