Wavelet Transformation with Python Examples (Machine Learning Tutorial)

Wavelet transformation is a powerful mathematical tool used in signal processing and image compression.

It is a data transformation technique that allows us to decompose a signal into different frequency bands, each with its own amplitude and phase information.

In this article, we will explore what wavelet transformation is, how it works, and its applications in machine learning.

What is Wavelet Transformation?

Wavelet transformation is a mathematical technique used to decompose a signal into different frequency bands, each with its own amplitude and phase information.

Unlike Fourier transformation, which decomposes a signal into sinusoids of different frequencies, wavelet transformation decomposes a signal into wavelets of different scales and frequencies.

How does Wavelet Transformation work?

Wavelet transformation works by passing a signal through a series of filters that remove or retain specific frequency components.

The signal is first passed through a low-pass filter, which removes high-frequency components, and then through a high-pass filter, which removes low-frequency components.

This process is repeated iteratively, with each iteration producing a set of coefficients that represent the signal at a different scale and frequency.

Once the signal has been decomposed into different frequency bands, we can analyze each band separately and apply different processing techniques to each band. This allows us to extract useful information from the signal that would otherwise be lost in a single-frequency analysis.

Applications of Wavelet Transformation in Machine Learning

Wavelet transformation plays an important role in data transformation, feature transformation, and feature engineering by enabling the analysis and processing of non-stationary signals.

It can be used to decompose signals into wavelets, which can then be used as features in machine learning models.

Wavelet transformation can also be used for denoising, compression, and feature extraction in image and audio processing applications. Its ability to provide multiresolution analysis and good time-frequency localization makes it a valuable tool in signal processing and feature engineering.

Wavelet transformation has many applications in machine learning, including:

In image compression, wavelet transformation is used to decompose an image into different frequency bands, each with its own compression ratio. This allows us to compress an image more efficiently while preserving important features.

In signal processing, wavelet transformation is used to filter and analyze signals in real-time. It is often used in applications such as noise reduction, speech recognition, and biomedical signal analysis.

In feature extraction, wavelet transformation is used to extract useful information from signals and images. By decomposing a signal into different frequency bands, we can extract features that are relevant to a specific task, such as identifying abnormalities in medical images or detecting anomalies in time-series data.

Python Example of Wavelet Transformation

Here is a Python example of how to apply wavelet transform to an image using the pyWavelets library.

Install PyWavlets

pip install PyWavelets

import numpy as np
import matplotlib.pyplot as plt

import pywt
import pywt.data


# Load image
original = pywt.data.camera()

# Wavelet transform of image, and plot approximation and details
titles = ['Approximation', ' Horizontal detail',
            'Vertical detail', 'Diagonal detail']
coeffs2 = pywt.dwt2(original, 'bior1.3')
LL, (LH, HL, HH) = coeffs2
fig = plt.figure(figsize=(12, 3))
for i, a in enumerate([LL, LH, HL, HH]):
    ax = fig.add_subplot(1, 4, i + 1)
    ax.imshow(a, interpolation="nearest", cmap=plt.cm.gray)
    ax.set_title(titles[i], fontsize=10)
    ax.set_xticks([])
    ax.set_yticks([])

fig.tight_layout()
plt.show()

The example above comes from the pywavelets official documentation

Here is another example:

Discrete Wavelet Transform


import pywt
import numpy as np

# Define the input signal
x = np.array([3, 7, 1, 1, -2, 5, 4, 6])

# Perform a level 2 discrete wavelet transform using the db2 wavelet
coeffs = pywt.wavedec(x, 'db2', level=2)

# Reconstruct the signal using the wavelet coefficients
x_reconstructed = pywt.waverec(coeffs, 'db2')
x_reconstructed

Useful Python Libraries for Wavelet transformation

Here are some Python libraries and methods that can be useful for Wavelet transformation in machine learning:

PyWavelets: This is a Python library that provides support for various wavelet families, including Haar, Daubechies, Symlets, Coiflets, and others. PyWavelets can be used for both one-dimensional and two-dimensional wavelet transforms, and it includes functions for performing discrete wavelet transforms (DWT) and wavelet packet transforms (WPT).
SciPy: This is a popular scientific computing library in Python that includes modules for signal processing, optimization, and more. The signal processing module in SciPy includes functions for performing continuous wavelet transforms (CWT) and DWT using various wavelet families.
PyWT: This is another Python library that provides support for various wavelet families and can be used for both one-dimensional and two-dimensional wavelet transforms. PyWT includes functions for performing DWT, CWT, and WPT, and it also includes tools for denoising signals using wavelet thresholding.
scikit-image: This is a Python library that provides various image processing tools, including support for wavelet transforms. The transform module in scikit-image includes functions for performing 2D DWT and CWT using various wavelet families.
Wavelets.jl: This is a Julia package that provides support for various wavelet families and can be used for both one-dimensional and two-dimensional wavelet transforms. Wavelets.jl includes functions for performing DWT, CWT, and WPT, and it also includes tools for denoising signals using wavelet thresholding.

Dfference between fourier transform and wavelet transform

Fourier transform and wavelet transform are two commonly used mathematical techniques for analyzing signals in machine learning. Here are some of the key differences between the two:

Frequency resolution: Fourier transform has a fixed frequency resolution for the entire signal, whereas wavelet transform provides a variable frequency resolution that can be adjusted based on the scale of the wavelet used. This means that wavelet transform can provide better time-frequency localization than Fourier transform.
Time localization: Fourier transform provides excellent frequency information but poor time information, whereas wavelet transform provides good time and frequency information. This means that wavelet transform is more suitable for analyzing signals that change over time.
Multi-resolution analysis: Wavelet transform provides a multi-resolution analysis of the signal, which means that it can analyze the signal at different scales. Fourier transform, on the other hand, provides a single-scale analysis of the signal.


Here is some Python code that visualizes Fourier transform and wavelet transform for a simple signal:


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

# Create a simple signal with two sine waves
t = np.linspace(0, 1, 500)
f1 = 5
f2 = 20
signal = np.sin(2*np.pi*f1*t) + np.sin(2*np.pi*f2*t)

# Compute and visualize the Fourier transform
fft = np.fft.fft(signal)
freq = np.fft.fftfreq(len(signal), t[1]-t[0])
plt.plot(freq, np.abs(fft))
plt.title('Fourier Transform')
plt.xlabel('Frequency (Hz)')
plt.show()

In this example, we create a simple signal with two sine waves of frequencies 5 Hz and 20 Hz.

We then compute and visualize the Fourier transform using np.fft.fft, and the wavelet transform using pywt.cwt with the Haar wavelet.

The resulting plots show the frequency spectrum of the signal for Fourier transform, and the time-frequency representation of the signal for wavelet transform using the Haar wavelet.


coeffs = pywt.wavedec(signal, 'haar')
# Calculate the sampling frequency and number of samples
fs = len(signal) / (t[-1] - t[0]) # Sample rate in Hz
N = len(signal)

# Calculate the scales used in the wavelet transform
scales = pywt.scale2frequency('haar', np.arange(1, len(signal) + 1))

# Calculate the frequencies corresponding to the scales and sample rate
freqs = scales * fs

# Plot the scales and frequencies over time
fig, ax = plt.subplots(nrows=2, sharex=True)
ax[0].plot(t, signal)
ax[0].set_ylabel('Signal')
ax[1].plot(t, np.zeros_like(t))
for i in range(len(coeffs)):
    t_i = np.linspace(0, (N - 2**(i+1))/fs, len(coeffs[i]))
    ax[1].plot(t_i, coeffs[i], label=f'Scale {i+1}')
ax[1].set_xlabel('Time (s)')
ax[1].set_ylabel('Wavelet Coefficients')
ax[1].legend()
plt.show()

We can see that Fourier transform provides a fixed frequency resolution for the entire signal, whereas wavelet transform provides a variable frequency resolution that depends on the scale of the wavelet used. We can also see that wavelet transform provides a good time-frequency localization of the signal, whereas Fourier transform only provides frequency information.

Useful Datasets for Wavelet Transformation

ECG (Electrocardiogram) Signal Dataset

The ECG dataset contains electrocardiogram recordings of heart activity. The dataset is commonly used in machine learning applications for arrhythmia detection, heart rate variability analysis, and other cardiac-related tasks. The signal data in this dataset is typically preprocessed and formatted in a way that is suitable for wavelet analysis.

Here is an example of how to load the ECG dataset in Python using the wfdb library:

pip install wfdb

import wfdb
ecg_record = wfdb.rdheader('100', pn_dir='mitdb')
# Download and load ECG dataset
record = wfdb.rdrecord('100', pn_dir='mitdb', sampfrom=0, sampto=3000, channels=[0])
signal = record.p_signal.flatten()

# Perform wavelet transform using pywt
import pywt
import numpy as np
coeffs, freqs = pywt.cwt(signal, np.arange(1, 31), 'morl')

# Visualize the wavelet transform
import matplotlib.pyplot as plt
plt.imshow(coeffs, extent=[0, 3, freqs[-1], freqs[0]], cmap='coolwarm', aspect='auto')
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
plt.show()

Image Dataset

Image datasets are also commonly used in machine learning applications for wavelet analysis. These datasets typically contain images in various formats (e.g. JPEG, PNG), which can be converted to a 2D array for wavelet analysis. Examples of image datasets include the MNIST dataset for handwritten digits recognition and the CIFAR-10 dataset for object recognition.
Here is an example of how to load the CIFAR-10 dataset in Python using the tensorflow library:


import tensorflow as tf
from tensorflow.keras.datasets import cifar10

# Download and load CIFAR-10 dataset
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

# Convert image to 2D array and perform wavelet transform using pywt
import pywt
coeffs, freqs = pywt.cwt(x_train[0].flatten(), np.arange(1, 31), 'morl')

# Visualize the wavelet transform
import matplotlib.pyplot as plt
plt.imshow(coeffs, extent=[0, 1, freqs[-1], freqs[0]], cmap='coolwarm', aspect='auto')
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
plt.show()  

To Know Before You Learn Wavelet transformation?

  • Basic knowledge of linear algebra, calculus, and probability theory
  • Familiarity with Python programming language
  • Understanding of signal processing concepts, such as Fourier analysis and time-frequency analysis
  • Knowledge of common machine learning techniques, such as classification and regression
  • Familiarity with common machine learning libraries, such as scikit-learn and TensorFlow
  • Basic understanding of wavelet theory, including different types of wavelets and their properties
  • Understanding of the advantages and limitations of wavelet analysis compared to other time-frequency analysis techniques
  • Familiarity with common wavelet analysis tools and libraries in Python, such as PyWavelets and Wavelet Toolbox for Python
  • Knowledge of applications of wavelet analysis in different domains, such as image processing, audio signal analysis, and biomedical signal analysis.

Important Concepts in Wavelet Transformation

  • Fourier Transformation
  • Signal Processing
  • Image Compression
  • Frequency Domain Analysis
  • Feature Extraction
  • Scale and Translation Invariance
  • Discrete Wavelet Transform
  • Continuous Wavelet Transform
  • Wavelet Coefficients
  • Haar Wavelet
  • Daubechies Wavelet
  • Biorthogonal Wavelet
  • Mother Wavelet
  • Multiresolution Analysis

What’s Next?

After learning about Wavelet transformation in machine learning, some common topics that people often teach include:

Signal denoising

Wavelet transformation can be used to remove noise from signals, and many techniques have been developed for this purpose. Some popular denoising techniques include thresholding, filtering, and soft and hard thresholding.

Feature extraction

Wavelet coefficients can be used as features for machine learning tasks such as classification and regression.

Feature extraction techniques include selecting a subset of the coefficients based on statistical properties, combining multiple scales and orientations, and using deep learning architectures.

Time-frequency analysis

Wavelet transformation is a type of time-frequency analysis, which is used to analyze signals in both the time and frequency domains. Other techniques for time-frequency analysis include spectrograms, Gabor transforms, and Wigner-Ville distributions.

Wavelet neural networks

Wavelet neural networks are a type of neural network that incorporates wavelet transformation as a preprocessing step for feature extraction. They have been used in applications such as image recognition, speech recognition, and financial forecasting.

Multiscale analysis

Wavelet transformation is a multiscale analysis technique, which decomposes a signal into different scales or resolutions. Other multiscale analysis techniques include fractal analysis, scale-space analysis, and pyramid analysis.

Relevant Entities

EntityProperties
WaveletLocalized, finite energy, symmetric, orthogonal, biorthogonal
Signal ProcessingFiltering, noise reduction, feature extraction, compression
Fourier TransformationDecomposes a signal into sine and cosine waves of different frequencies
Multiresolution AnalysisDecomposition of a signal into different scales or resolutions
Discrete Wavelet TransformComputes wavelet coefficients using filter banks and downsampling
Continuous Wavelet TransformComputes wavelet coefficients for all possible scales and translations
Polynomial TransformationData preprocessing technique used to transform non-linear data into a linear form.

FAQs

What is wavelet transformation?

Time-frequency analysis.

What is the purpose of wavelet transformation?

Analysis of non-stationary signals.

How does wavelet transformation work?

Decomposing signals into wavelets

What are the advantages of wavelet transformation?

Multiresolution analysis, good time-frequency localization.

What are the applications of wavelet transformation?

Image processing, audio compression, denoising.

What are the limitations of wavelet transformation?

Interpretability, computational complexity.

Conclusion

Wavelet transformation is a powerful mathematical tool used in signal processing, image compression, and feature extraction. It allows us to decompose a signal into different frequency bands, each with its own amplitude and phase information, and analyze each band separately. Wavelet transformation has many applications in machine learning, and its ability to extract useful information from signals and images makes it an essential tool in the machine learning toolbox.

sources

Wavelet transform – Wikipedia: This page provides an overview of the theory and applications of wavelet transform, including continuous and discrete wavelet transform, wavelet packet transform, and wavelet-based denoising and compression.
PyWavelets documentation: PyWavelets is a popular Python library for wavelet analysis, which provides a wide range of functions for wavelet transform, wavelet packet transform, and discrete wavelet transform. The documentation includes tutorials, examples, and API reference.
Wavelet Toolbox for MATLAB documentation: MATLAB is a popular programming language for scientific computing, and the Wavelet Toolbox is a powerful tool for wavelet analysis. The documentation includes tutorials, examples, and API reference for a wide range of wavelet-based applications.
Wavelet Transform – ScienceDirect: This page provides a comprehensive overview of the theory and applications of wavelet transform, including different types of wavelets, wavelet-based signal processing, and wavelet-based machine learning.
Applications of Wavelet Analysis in Machine Learning: This book chapter provides an in-depth review of the applications of wavelet analysis in machine learning, including feature extraction, denoising, and classification. The chapter also includes case studies in different domains such as image processing, audio signal analysis, and biomedical signal analysis.