SciPy - Discrete Wavelet Transform (DWT)



Discrete Wavelet Transform in SciPy

The Discrete Wavelet Transform (DWT) is a powerful tool for analyzing signals by decomposing them into different frequency components with a discrete scale. Unlike the Continuous Wavelet Transform (CWT), DWT uses a fixed set of wavelet functions which makes it computationally more efficient and appropriate for real-time signal processing applications.

DWT is commonly used for signal compression, denoising and feature extraction in various fields such as image processing, audio processing and bio-signal analysis.

The Discrete Wavelet Transform of a signal x(t) can be represented as follows −

$\mathrm{W(j, k) = \int_{-\infty}^{\infty} x(t) \psi^* \left( \frac{t - k}{2^j} \right) dt}$

Where −

  • x(t) is the input signal.
  • (t) is the mother wavelet.
  • = 2j is the scale factor.
  • = k is the translation parameter (discrete shifts of the signal).
  • W(j, k) represents the wavelet coefficients at scale j and shift k.

Key Properties of DWT

Following are the key properties of the Discrete Wavelet Transform −

  • Multi-Resolution Analysis: DWT allows the signal to be analyzed at different scales (resolutions) which helps capture both the low-frequency and high-frequency components.
  • Efficient Computation: DWT is computationally efficient because it operates with a discrete set of wavelet functions by making it suitable for real-time applications.
  • Downsampling: DWT performs downsampling at each level, reducing the size of the coefficients and thus the data storage requirements.
  • Time-Frequency Localization: DWT provides both time and frequency localization of the signal by making it useful for analyzing non-stationary signals.

In SciPy versions 1.15.1 and later the cwt function has been removed from the scipy.signal module. Therefore, if you're using SciPy 1.15.1 or higher, we should use alternative methods to perform the Continuous Wavelet Transform (CWT) such as the PyWavelets (pywt) library.

Using PyWavelets (pywt)

If We're facing issues with SciPy's dwt function then we have an alternative approach which is to use PyWavelets (pywt), a powerful library for wavelet analysis in Python. It provides extensive functionality for Continuous Wavelet Transform (CWT), Discrete Wavelet Transform (DWT) and more.

Basic Example of DWT using PyWavelets

In this example we will perform a 1-level DWT on a simple signal using the Haar wavelet which one of the simplest wavelets −

import numpy as np
import pywt
import matplotlib.pyplot as plt

# Create a simple signal (a step function for demonstration)
signal = np.array([1, 1, 1, 0, 0, 0, 1, 1, 1, 0])

# Perform 1-level Discrete Wavelet Transform (DWT) using the Haar wavelet
coeffs = pywt.dwt(signal, 'haar')

# Extract the approximation (cA) and detail (cD) coefficients
cA, cD = coeffs

# Plot the original signal, approximation coefficients, and detail coefficients
plt.figure(figsize=(10, 6))

# Plot the original signal
plt.subplot(3, 1, 1)
plt.plot(signal, label="Original Signal", color='blue')
plt.title("Original Signal")
plt.legend()

# Plot the approximation coefficients (cA)
plt.subplot(3, 1, 2)
plt.plot(cA, label="Approximation Coefficients (cA)", color='green')
plt.title("Approximation Coefficients (cA)")
plt.legend()

# Plot the detail coefficients (cD)
plt.subplot(3, 1, 3)
plt.plot(cD, label="Detail Coefficients (cD)", color='red')
plt.title("Detail Coefficients (cD)")
plt.legend()

plt.tight_layout()
plt.show()

Following is the output of the Basic Discrete Wavelet Transform using PyWavelets −

PyWavelets Basic DWT

Multi-Level DWT Decomposition using PyWavelets

This example shows how to perform multi-level DWT decomposition on a signal using PyWavelets. We will decompose the signal into multiple levels −

import numpy as np
import matplotlib.pyplot as plt
import pywt

# Generate a simple signal (a combination of two sine waves)
t = np.linspace(0, 1, 500, endpoint=False)  # Time vector
signal = np.sin(2 * np.pi * 10 * t) + np.sin(2 * np.pi * 50 * t)  # Signal

# Perform Multi-Level DWT decomposition using 'db1' (Daubechies wavelet)
coeffs = pywt.wavedec(signal, 'db1', level=3)  # Decompose to 3 levels

# coeffs contains the approximation and detail coefficients at each level
cA3, cD3, cD2, cD1 = coeffs  # cA3: Approximation at level 3, cD3: Detail at level 3, etc.

# Plot the original signal and the decomposition results
plt.figure(figsize=(10, 10))

# Plot the original signal
plt.subplot(5, 1, 1)
plt.plot(t, signal)
plt.title("Original Signal")
plt.xlabel("Time (s)")
plt.ylabel("Amplitude")

# Plot the approximation coefficients at level 3
plt.subplot(5, 1, 2)
plt.plot(cA3)
plt.title("Approximation Coefficients at Level 3")

# Plot the detail coefficients at level 3
plt.subplot(5, 1, 3)
plt.plot(cD3)
plt.title("Detail Coefficients at Level 3")

# Plot the detail coefficients at level 2
plt.subplot(5, 1, 4)
plt.plot(cD2)
plt.title("Detail Coefficients at Level 2")

# Plot the detail coefficients at level 1
plt.subplot(5, 1, 5)
plt.plot(cD1)
plt.title("Detail Coefficients at Level 1")

plt.tight_layout()
plt.show()

Below is the output of the Multi-Level Discrete Wavelet Transform using PyWavelets −

PyWavelets Multi-Level DWT

Applications of DWT

The Discrete Wavelet Transform (DWT) is widely used in various fields due to its computational efficiency and ability to provide both time and frequency information. Some key applications are as follows −

  • Signal Compression: DWT is used in applications like JPEG 2000 for image compression and in audio and video compression formats.
  • Denoising: DWT helps remove noise from signals while preserving the important features by making it useful in fields like speech processing and biomedical signal analysis.
  • Feature Extraction: DWT is used to extract features from signals for classification or qattern recognition in machine learning tasks.
  • Data Fusion: DWT can fuse information from multiple data sources useful in sensor networks and medical diagnostics.

Choosing the Right Wavelet for DWT

Just like in CWT choosing the right wavelet for DWT is important for capturing the features of the signal being analyzed. Some common wavelets for DWT are as follows −

Wavelet Best For Example Use Cases
Daubechies (db) Smooth signals, multiresolution analysis Signal denoising, image compression
Symlet Symmetric signals, efficient approximation Audio processing, compression
Coiflet Signals with smooth features, multiresolution Feature extraction, denoising
Haar Discontinuous signals, edge detection Compression, real-time signal analysis
Advertisements