Here's a direct implementation of the formula
$$\mathcal H(u)(t) = \frac1{\pi} {\int_{-\infty}^{\infty} \!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!\!-}\quad \frac{u(\tau)}{t-\tau}\, \mathrm d\tau$$$$\mathcal H(u)(t) = \frac1{\pi} -\hspace{-1.1em}\int_{-\infty}^\infty \frac{u(\tau)}{t-\tau}\, \mathrm d\tau$$
hilbertTransform[f_, u_, t_] :=
FullSimplify[Convolve[f, 1/u, u, t, PrincipalValue -> True]/Pi]π]
Try it out:
hilbertTransform[#, v, w] & /@ {Sin[v], Cos[v], 1/(1 + v^2), Sinc[v], DiracDelta[v]}
{-Cos[w], Sin[w], w/(1 + w^2), (1 - Cos[w])/w, 1/(Pi*wπ w)}
For the discrete Hilbert transform, here is a Mathematica routine:
hilbert[data_?VectorQ] := Block[Module[{nfopts = FourierParameters -> {1, -1}, e, n},
e = Boole[EvenQ[n = Length[data]]];
Im[InverseFourier[Im[InverseFourier[Fourier[data, fopts] *
PadRight[
ArrayPad[ConstantArray[2 PadRight[ArrayPad[ConstantArray[2, Quotient[n, 2] - e], {1, e}, 1], n],
n] Fourier[data, FourierParameters -> {1, -1}],
FourierParameters -> {1, -1}]]]fopts]]] /; And @@ Thread[Im[data] == 0]
(making everything completely analogous to FourierTransform[]
and Fourier[]
). The algorithm is based on the routine in Marple's paper, and is essentially the same algorithm used by the function hilbert()
in MATLAB's Signal Processing Toolbox.
Examples:
hilbert[{1, -2, 1}]
{1.73205, 0., -1.73205}
hilbert[{1, -2, 1, 2}]
{2., 0., -2., 0.}