9951 explained code solutions for 126 technologies


python-scipyHow can I use Python SciPy to calculate the Hilbert transform?


The SciPy library provides a number of functions for performing the Hilbert transform. The most basic of these is scipy.signal.hilbert(), which takes a signal x as an argument and returns the analytical signal, y, which is the result of the Hilbert transform.

For example:

import scipy.signal as sig
import numpy as np

x = np.arange(-2*np.pi, 2*np.pi, 0.1)
y = np.sin(x)

y_hilbert = sig.hilbert(y)

The output of this code is an array of complex numbers, y_hilbert, which is the result of the Hilbert transform.

The following list describes the parts of the code in more detail:

  1. import scipy.signal as sig: imports the SciPy signal module, which contains the hilbert() function.
  2. import numpy as np: imports the NumPy library, which is used to generate the signal x.
  3. x = np.arange(-2*np.pi, 2*np.pi, 0.1): creates an array of values from -2π to 2π in increments of 0.1.
  4. y = np.sin(x): creates an array of values which are the sine of the values in x.
  5. y_hilbert = sig.hilbert(y): performs the Hilbert transform on the signal y.

More information about the SciPy Hilbert transform functions can be found in the SciPy documentation.

Edit this code on GitHub