Skip to content

Commit

Permalink
Revert "Revert "Refactor: Integrate coefficients from Signal *hilbert…
Browse files Browse the repository at this point in the history
… for Hartley Phasing""

This reverts commit e3abe4b.
  • Loading branch information
joslloand committed May 9, 2019
1 parent 1b42fa6 commit 264e8cd
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 337 deletions.
83 changes: 30 additions & 53 deletions Classes/Hilbert.sc
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,16 @@ HilbertH {
})
}

// calculate real coefficients as delayed impulse
/* To be deprecated */
*calcRealCoeffs { |size|

^HilbertHRe.calcCoeffs(size)
"HilbertH *calcRealCoeffs to be deprecated. Use SignalBox Quark: Signal *hilbert.real".warn;
^Signal.hilbert(size).real.as(Array)
}

// calculate imag coefficients via (1-cos(t)) / t
/* To be deprecated */
*calcImagCoeffs { |size|

^HilbertHIm.calcCoeffs(size)
"HilbertH *calcImagCoeffs to be deprecated. Use SignalBox Quark: Signal *hilbert.imag".warn;
^(Signal.kaiserWindow(size, a: 1) * Signal.hilbert(size).imag).as(Array)
}

*arMag { |in, size = 2048, mul = 1, add = 0|
Expand Down Expand Up @@ -310,65 +310,43 @@ HilbertHRe {
// between real and imaginary parts, at the expense of an
// extra convolution
*arConv { |in, size=2048, mul=1.0, add=0.0|
var r, kernel_r;

r = HilbertHRe.calcCoeffs(size);
kernel_r = LocalBuf.new(size, 1).set(r);

^Convolution2.ar(in, kernel_r, framesize: size, mul: mul, add: add);
^Convolution2.ar(
in,
LocalBuf.new(size, 1).set(
Signal.hilbert(size).real
),
framesize: size,
mul: mul,
add: add
)
}

// calculate real coefficients as delayed impulse
/* To be deprecated */
*calcCoeffs { |size|
var half_win, xReal;

half_win = size/2;

// real response
xReal = Array.fill(size, { 0.0 });
xReal.put(half_win, 1.0);

^xReal
"HilbertHRe *calcCoeffs to be deprecated. Use SignalBox Quark: Signal *hilbert.real".warn;
^Signal.hilbert(size).real.as(Array)
}

// // calculate real coefficients via sinc
// *calcCoeffs { |size|
// var half_win, window, xReal;
//
// half_win = size/2;
// window = Signal.hanningWindow(size+1);
//
// // imaginary response
// xReal = Array.series(size+1, half_win.neg, 1);
// xReal = xReal.collect({|i| (i == 0).if({ 1 }, { sin(pi * i) / (pi * i) }) }) * window;
//
// ^xReal.keep(size)
// }
}


HilbertHIm {
*ar {
| in, size=2048, mul=1.0, add=0.0 |
var i, kernel_i, image;

i = HilbertHIm.calcCoeffs(size);
kernel_i = LocalBuf.new(size, 1).set(i);

^Convolution2.ar(in, kernel_i, framesize: size, mul: mul, add: add);
^Convolution2.ar(
in,
LocalBuf.new(size, 1).set(
Signal.kaiserWindow(size, a: 1) * Signal.hilbert(size).imag
),
framesize: size,
mul: mul,
add: add
)
}

// calculate imag coefficients via (1-cos(t)) / t
/* To be deprecated */
*calcCoeffs { |size|
var half_win, window, xImag;

half_win = size/2;
window = Signal.hanningWindow(size+1);

// imaginary response
xImag = Array.series(size+1, half_win.neg, 1);
xImag = xImag.collect({|i| (i == 0).if({ 0 }, { 1 - cos(pi * i) / (pi * i) }) }) * window;
^xImag.keep(size);
"HilbertHIm *calcCoeffs to be deprecated. Use SignalBox Quark: Signal *hilbert.imag".warn;
^(Signal.kaiserWindow(size, a: 1) * Signal.hilbert(size).imag).as(Array)
}
}

Expand Down Expand Up @@ -540,4 +518,3 @@ HilbertPDNIm {
^((hilbertSin * mul) + add)
}
}

169 changes: 25 additions & 144 deletions HelpSource/Classes/HilbertH.schelp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ This value will be added to the output.
returns:: An array of channels: code::[real, imag]::


NOTE::The kernel used for emphasis::Hartley Phasing:: filtering is windowed with the link::Classes/Signal#*kaiserWindow::, where emphasis::alpha:: is set to 1.::



METHOD:: arRotate

Rotate footnote::decrement:: the phase of the input.
Expand Down Expand Up @@ -139,6 +143,7 @@ returns::

emphasis::Instantaneous phase::, in radians.


METHOD:: calcRealCoeffs

Generate real coefficients.
Expand All @@ -147,6 +152,15 @@ ARGUMENT:: size

The size of the kernel used for emphasis::Hartley Phasing:: filtering.

DISCUSSION::

WARNING::To be deprecated! Use link::Classes/Signal#*hilbert#Signal *hilbert.real:: instead.::

code::
Signal.hilbert(1000).real.plot("Hilbert Real Coefficients")
::


METHOD:: calcImagCoeffs

Generate imaginary coefficients.
Expand All @@ -155,6 +169,17 @@ ARGUMENT:: size

The size of the kernel used for emphasis::Hartley Phasing:: filtering.

DISCUSSION::

WARNING::To be deprecated! Use link::Classes/Signal#*hilbert#Signal *hilbert.imag:: instead.::

code::
(
var size = 1000;
(Signal.kaiserWindow(size, a: 1) * Signal.hilbert(size).imag).plot("Hilbert Imaginary Coefficients")
)
::



SECTION:: Frequency response
Expand Down Expand Up @@ -585,147 +610,3 @@ freq = dur.reciprocal;
)
)
::

SUBSECTION:: calcRealCoeffs

code::
/*

FFT analysis functions

NOTE: FFT returns linear frequency

*/
(
~calcMag = { arg kernel;

var fftResponse, fftMag;

// FFT analysis here!
fftResponse = fft(
kernel.as(Signal),
Signal.newClear(kernel.size),
Signal.fftCosTable(kernel.size)
);

// find (& trim magnitude)
fftMag = fftResponse.magnitude;
fftMag = fftMag.copyFromStart((kernel.size/2).asInteger);

fftMag;
};
)


/*
TEST Hartley : *calcRealCoeffs

coefficients
*/

(
var size;
var kernel;
var plotDbMin, plotDbMax, plotDegMin, plotDegMax;

// kernel size
size = 2048;

// plot params
plotDbMin = -60.0;
plotDbMax = 6.0;
plotDegMin = 0.0;
plotDegMax = 180.0;


// calculate coefficients
kernel = HilbertH.calcRealCoeffs(size);

// plot coefficients
kernel.plot(
name: "Real Coefficients",
minval: -1,
maxval: 1
);

// plot magnitude
~calcMag.value(kernel).ampdb.plot(
name: "Real Magnitude Response",
minval: plotDbMin,
maxval: plotDbMax
);

)
::

SUBSECTION:: calcImagCoeffs

code::
/*

FFT analysis functions

NOTE: FFT returns linear frequency

*/
(
~calcMag = { arg kernel;

var fftResponse, fftMag;

// FFT analysis here!
fftResponse = fft(
kernel.as(Signal),
Signal.newClear(kernel.size),
Signal.fftCosTable(kernel.size)
);

// find (& trim magnitude)
fftMag = fftResponse.magnitude;
fftMag = fftMag.copyFromStart((kernel.size/2).asInteger);

fftMag;
};
)


/*
TEST Hartley : *calcImagCoeffs

coefficients
*/

(
var size;
var kernel;
var plotDbMin, plotDbMax, plotDegMin, plotDegMax;

// kernel size
size = 2048;

// plot params
plotDbMin = -60.0;
plotDbMax = 6.0;
plotDegMin = 0.0;
plotDegMax = 180.0;


// calculate coefficients
kernel = HilbertH.calcImagCoeffs(size);

// plot coefficients
kernel.plot(
name: "Imaginary Coefficients",
minval: -1,
maxval: 1
);

// plot magnitude
~calcMag.value(kernel).ampdb.plot(
name: "Imaginary Magnitude Response",
minval: plotDbMin,
maxval: plotDbMax
);

)
::
Loading

0 comments on commit 264e8cd

Please sign in to comment.