0
$\begingroup$

I am following the example from: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.hilbert.html

And although I can replicate their example, it does not work with my data.

This is the code:

analytic_signal = hilbert(signal)
amplitude_envelope = np.abs(analytic_signal)

fig = plt.figure(figsize=(15,5))
fig.add_subplot(121)
plot(signal, label='signal')
fig.add_subplot(122)
plot(amplitude_envelope, label='envelope')

This is the output. Left is the original signal, right is the envelope as calculated above.

enter image description here

And this is the signal in case you want to test it:

[[ 0.5704682 ] [ 0.06571468] [-0.28429375] [ 0.255279 ] [ 0.06216578] [-0.13484125] [ 0.38119613] [-0.40148008] [ 0.08082003] [-0.17566549] [ 0.24713693] [ 0.00198688] [ 0.18063265] [ 0.11902314] [ 0.04984834] [ 0.20022316] [-0.04666157] [ 0.30517989] [-0.09863981] [ 0.14672664] [ 0.10639614] [-0.10206009] [ 0.36802793] [-0.11765433] [ 0.25391577] [ 0.00158574] [ 0.01649894] [-0.279135 ] [-0.27651419] [-0.22468916] [-0.25466327] [-0.02636799] [-0.20293286] [-0.02929768] [-0.36659976] [-0.09297736] [-0.31926376] [-0.3779127 ] [-0.04120377] [-0.42557633] [-0.00207802] [-0.34818142] [-0.04297083] [-0.09915719] [-0.2356183 ] [-0.12256754] [-0.39409093] [-0.18308043] [-0.33650381] [-0.20342049] [-0.26423265] [ 0.05856 ] [-0.08310593] [ 0.22382836] [ 0.12261066] [ 0.2724963 ] [ 0.34208882] [ 0.20179415] [ 0.39766479] [ 0.12914944] [ 0.34921983] [ 0.10552505] [ 0.16650139] [ 0.13513111] [ 0.0009718 ] [ 0.26501238] [ 0.20015644] [ 0.44631929] [ 0.35737483] [ 0.42040605] [ 0.30267179] [ 0.50492252] [ 0.41893588] [ 0.25135659] [ 0.34242197] [ 0.06739386] [ 0.17077554] [ 0.0656095 ] [ 0.04012584] [ 0.15730393] [-0.08188899] [ 0.05170137] [-0.17682187] [-0.20028294] [-0.1136277 ] [-0.19337712] [ 0.06004237] [-0.24706524] [ 0.00228392] [-0.03011352] [-0.19129926] [ 0.10081927] [-0.29406951] [ 0.15344296] [-0.19615539] [ 0.16516892] [-0.16082269] [ 0.0421557 ] [-0.02897998] [-0.15913717]]

Another example, first the images, as before, left signal, right envelope:

enter image description here

And the signal

[[ 3.63968429e-02] [-3.29983277e-02] [ 2.06170725e-02] [-2.37741482e-02] [ 8.15294448e-03] [-1.22408903e-02] [-4.13352253e-03] [-3.13553630e-04] [-7.50674682e-03] [-5.50724797e-03] [-3.18441877e-04] [-1.29022198e-02] [ 5.05903995e-03] [-1.61897903e-02] [ 1.10073479e-02] [-2.09121034e-02] [ 4.33312414e-03] [-8.54053188e-03] [-6.74401988e-03] [-2.34535194e-04] [-1.35199584e-02] [ 1.09679964e-03] [-6.15911062e-03] [-9.66997874e-03] [ 1.14586036e-03] [-1.31098663e-02] [ 3.98470224e-04] [-1.19023724e-02] [ 1.68468182e-03] [-1.55489232e-02] [ 4.90542067e-03] [-1.27899675e-02] [ 9.03119999e-07] [-9.20601121e-03] [-1.66646380e-03] [-6.45997328e-03] [ 1.22983973e-03] [-1.52880477e-02] [ 7.60554167e-03] [-1.91834496e-02] [ 9.04069102e-03] [-1.92939289e-02] [ 5.37510894e-03] [-1.83001449e-02] [ 8.20770182e-03] [-1.76959583e-02] [ 5.43452896e-03] [-1.29964867e-02] [ 1.06079450e-03] [-9.42743254e-03] [-3.28567744e-04] [-9.02460988e-03] [-1.57831017e-03] [-5.89834797e-03] [-2.85756319e-03] [-7.19735934e-03] [-7.51708596e-03] [-4.40813455e-03] [-6.68162096e-03] [-2.03114234e-03] [-1.20218079e-02] [ 4.73647854e-03] [-1.68435980e-02] [ 5.23919428e-03] [-1.25609816e-02] [ 5.41013112e-03] [-1.93121716e-02] [ 6.63237594e-03] [-8.76211500e-03] [-2.31810298e-03] [-8.03766296e-03] [-1.01486730e-02] [ 1.10900471e-02] [-2.65212753e-02] [ 1.38229846e-02] [-1.94234453e-02] [ 5.53125433e-03] [-2.07770939e-02] [ 1.01584295e-02] [-1.35340646e-02] [-8.79709301e-04] [-4.32789216e-03] [-6.60632101e-03] [-6.40936667e-04] [-1.12091052e-02] [-9.39950396e-04] [-6.94627266e-04] [-1.05248070e-02] [-2.45723898e-04] [-7.93141249e-03] [ 3.82839374e-03] [-1.83226272e-02] [ 9.14453716e-03] [-1.75662825e-02] [-2.52022260e-03] [-7.61242736e-03] [ 3.99888234e-03] [-2.07124341e-02] [ 1.25127588e-02] [-2.08206049e-02]]

$\endgroup$
  • $\begingroup$ See also this other question and answers. Not a duplicate, but maybe informative. $\endgroup$ – JRE Jul 27 at 16:24
  • $\begingroup$ I went and had a look at the source code of the scipy hilbert function - it seems to implement the transformation properly rather than using an approximation as I've seen in other libraries (looking at you PureData.) $\endgroup$ – JRE Jul 27 at 16:31
3
$\begingroup$

Hm well, technically it is some kind of envelope: it oscillates between hilbert(x) and -hilbert(x). Your examples (dashed lines are $\pm$hilbert(x)):

First example Second example

I'm assuming you're looking for something smoother. Matlab has a function called envelope where you have various ways of controlling how the envelope is extracted. Not sure if there is a Python equivalent. Here is an example for your first dataset:

Envelope example

$\endgroup$
1
$\begingroup$

Your code does what you ask it do and the result look fine to me. It would be helpful to state why you think this is wrong and what you did expect instead.

Envelope detection using Hilbert Transform works well for narrow band signals, (Amplitude modulation for example), but it typically does not do well for broad band signals as you do have here.

You should start perhaps with describing what exactly you mean by "envelope", what do you want to the output to look like and what you want to do with it. There are a great variety of envelop detection algorithms out there, but the best choice depends on signal type and specific application

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.