org-arl / arlpy

ARL Python Tools
BSD 3-Clause "New" or "Revised" License
119 stars 37 forks source link

asig.envelope artefacts #47

Closed notthetup closed 4 years ago

notthetup commented 4 years ago

asig.envelope has a strange window like artefact in the first few samples.

Screenshot 2020-06-28 08 01 56

d = np.concatenate([np.random.normal(-0.001,0.001,200), np.random.normal(-0.1,0.1,200)])
e = asig.envelope(d)
plt.plot(d, hold=True)
plt.plot(e)
mchitre commented 4 years ago

Not a bug. The signal you gave it as an input has a DC offset in the second half of the signal. The way scipy implements the Hilbert transform, it effectively assumes a periodic signal, so it means your signal had a DC offset before it started. So the Hilbert transform has a non-zero imaginary part in the first 200 samples to try and match to the DC offsets on both sides, and that results in the envelope you see.

I recognize that it is non-intuitive that the envelope doesn't hug the signal here, but the Hilbert envelope (definition of the envelope function in arlpy) is indeed what you see. If you zero pad your signal at the end, to allow it to go back to zero, you'll get the result you desire.