Wednesday, May 3, 2017

Why is Scipy implementation of Hilbert() function different from Matlab implementation of the function?


I am trying to fit Hilbert envelop to a high frequency ultrasonic signal of frequency 250 KHZ and sampling rate 12000000. Raw signal looks like below: Raw signal.


I used hilbert() function from scipy.signal package in python this is what it looks like Hilbert transform.


The python code looks like below


from scipy.signal import hilbert
import numpy as np
def Hilbert(self,i=0):

analytical_signal = hilbert(self.sensor["s"+str(i)])
amplitude_envelope = np.abs(analytical_signal)
return amplitude_envelope

The Matlab implementation looks like this Matlab implementation


Matlab code is as follows:


figure;
plot(abs(hilbert(signal)),'r');
hold on;
plot(signal,'b');


The line data is as follows Signal. I am wondering which one is correct?



Answer



It works fine for me:


from scipy.signal import hilbert
import numpy as np
from matplotlib.pyplot import plot

sensor = np.loadtxt('signal.txt')
plot(sensor)


analytical_signal = hilbert(sensor)
plot(analytical_signal.real)
plot(analytical_signal.imag)

amplitude_envelope = np.abs(analytical_signal)
plot(amplitude_envelope)

enter image description here


enter image description here



What are you doing differently? Maybe you're throwing away the imaginary part of analytical_signal somehow?


No comments:

Post a Comment

periodic trends - Comparing radii in lithium, beryllium, magnesium, aluminium and sodium ions

Apparently the of last four, $\ce{Mg^2+}$ is closest in radius to $\ce{Li+}$. Is this true, and if so, why would a whole larger shell ($\ce{...