SciPy - Wavelet Packet Transform (WPT)



Wavelet Packet Transform in SciPy

The Wavelet Packet Transform (WPT) is an advanced signal processing technique that extends the Discrete Wavelet Transform (DWT) by allowing the decomposition of both approximation and detail coefficients at each level. This provides a more detailed analysis of signals by making WPT useful in applications requiring finer frequency resolution such as feature extraction, compression and denoising.

Mathematically the Wavelet Packet Transform of a signal x(t) can be represented as follows −

$\mathrm{W(j, k, n) = \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.
  • n is the node index in the wavelet packet tree.
  • W(j, k, n) represents the wavelet packet coefficients at scale j, position k and node n.

Key Properties of WPT

Following are the key properties of the Wavelet Packet Transform −

  • Full Decomposition: Unlike DWT, WPT decomposes both approximation and detail coefficients at each level by providing richer signal representation.
  • Better Frequency Resolution: WPT offers improved frequency localization compared to DWT by making it suitable for fine-grained spectral analysis.
  • Flexible Signal Representation: WPT enables adaptive signal representation based on application-specific requirements.
  • Computational Complexity: Due to the full decomposition, WPT requires more computational resources than DWT.
Note:

As with DWT, WPT can be implemented using the PyWavelets (pywt) library which offers powerful functions for wavelet packet analysis.

Using PyWavelets (pywt) for WPT

If we want to perform Wavelet Packet Transform in Python, PyWavelets provides easy-to-use functions to handle WPT efficiently.

Basic Example of WPT using PyWavelets

The Wavelet Packet Transform (WPT) provides a more detailed signal decomposition compared to the standard Discrete Wavelet Transform (DWT). In this example we will analyze a composite signal using the Daubechies wavelet ('db1') and decompose it into multiple subbands. WPT allows for a richer frequency-time analysis by making it suitable for applications such as audio processing, biomedical signal analysis and fault detection.

Let's perform a Wavelet Packet Transform on a sample signal and visualize the decomposition −

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

# Generate a composite signal with two sine waves of different frequencies
t = np.linspace(0, 1, 512, endpoint=False)
signal = np.sin(2 * np.pi * 50 * t) + np.sin(2 * np.pi * 120 * t)

# Perform Wavelet Packet Transform using Daubechies wavelet ('db1')
wp = pywt.WaveletPacket(data=signal, wavelet='db1', mode='symmetric', maxlevel=3)

# Extract coefficients from level 3 nodes
coeffs = [node.data for node in wp.get_level(3, order='freq')]

# Plot original signal and wavelet packet coefficients
plt.figure(figsize=(10, 8))

# Plot original signal
plt.subplot(len(coeffs) + 1, 1, 1)
plt.plot(t, signal, label="Original Signal", color='blue')
plt.title("Original Signal")
plt.legend()

# Plot wavelet packet coefficients at different nodes
for i, coef in enumerate(coeffs):
    plt.subplot(len(coeffs) + 1, 1, i + 2)
    plt.plot(coef, label=f"Node {i}")
    plt.title(f"Wavelet Packet Coefficients - Node {i}")
    plt.legend()

plt.tight_layout()
plt.show()

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

PyWavelets Basic Wavelet Packet

Optimal Subband Selection in WPT

Wavelet Packet Transform (WPT) provides a more flexible decomposition of signals compared to standard Discrete Wavelet Transform (DWT) by allowing decomposition at all frequency bands. Selecting the optimal subband in WPT is crucial for applications like signal compression, denoising and feature extraction.

The selection is typically based on criteria such as entropy, energy or statistical measures that quantify the amount of useful information in each subband.

Common Criteria for Subband Selection

  • Energy-based Selection: Measures the total energy within each subband to select the most significant one.
  • Entropy-based Selection: Chooses the subband with the highest entropy which contains the most information.
  • Reconstruction Error: Selecting subbands that minimize reconstruction error to retain the most relevant data.
  • Variance or Statistical Measures: Uses variance to quantify the spread of coefficients which helps in feature extraction tasks.

In this example we will perform a Wavelet Packet Transform on a synthetic signal and select the subband with the highest energy −

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

# Generate a synthetic signal (sum of sine waves)
t = np.linspace(0, 1, 512, endpoint=False)
signal = np.sin(2 * np.pi * 50 * t) + np.sin(2 * np.pi * 120 * t)

# Perform 3-level Wavelet Packet Transform using 'db1' wavelet
wp = pywt.WaveletPacket(data=signal, wavelet='db1', mode='symmetric', maxlevel=3)

# Retrieve all subbands at the maximum decomposition level
nodes = wp.get_level(3, order='freq')

# Calculate energy for each subband
subband_energies = [np.sum(node.data**2) for node in nodes]

# Identify the optimal subband (maximum energy)
optimal_subband_index = np.argmax(subband_energies)
optimal_subband = nodes[optimal_subband_index].data

# Plot original signal and the optimal subband
plt.figure(figsize=(10, 6))

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

# Plot optimal subband
plt.subplot(2, 1, 2)
plt.plot(optimal_subband, label=f"Optimal Subband (Node {optimal_subband_index})", color='red')
plt.title(f"Optimal Subband with Highest Energy - Node {optimal_subband_index}")
plt.legend()

plt.tight_layout()
plt.show()

The following output shows the original signal along with the selected optimal subband containing the highest energy by making it useful for further analysis −

Optimal Subband Selection in WPT

Applications of WPT

The Wavelet Packet Transform (WPT) is widely used in various fields, especially where fine frequency analysis is required. Some key applications are as follows −

  • Speech and Audio Processing: WPT provides enhanced resolution for audio compression and speech recognition.
  • Biomedical Signal Analysis: Used in ECG and EEG analysis for feature extraction and noise reduction.
  • Image Compression: WPT helps in efficient compression by offering adaptive subband decomposition.
  • Fault Diagnosis: WPT is employed in mechanical and electrical systems for detecting abnormalities in vibration signals.

Choosing the Right Wavelet for WPT

Choosing an appropriate wavelet function for WPT is crucial for optimal signal representation. Some commonly used wavelets include −

Wavelet Best For Example Use Cases
Daubechies (db) Smooth signals Speech and audio processing
Coiflet Biomedical signals EEG/ECG analysis
Symlet Symmetric signals Compression