9951 explained code solutions for 126 technologies


python-scipyHow can I use Python Scipy to perform a wavelet transform?


Scipy is an open source library for scientific computing in Python. It includes a module for wavelet transforms, which can be used to decompose a signal into components of different frequency. To perform a wavelet transform using Scipy, the following steps should be taken:

  1. Import the necessary packages:
import numpy as np
import scipy.signal as sig
  1. Create a signal to be transformed. This example creates a noisy sine wave:
t = np.arange(0, 10, 0.1)
x = np.sin(t) + np.random.randn(len(t))*0.2
  1. Compute the wavelet transform of the signal:
coeff, freq = sig.cwt(x, sig.ricker, np.arange(1,128))
  1. Plot the resulting transform:
import matplotlib.pyplot as plt
plt.imshow(coeff, extent=[-1, 1, 1, 128], cmap='PRGn', aspect='auto',
           vmax=abs(coeff).max(), vmin=-abs(coeff).max())
plt.show()

The output of this code is a plot of the wavelet transform of the signal.

Helpful links

Edit this code on GitHub