CWT
The Continuous Wavelet Transform (CWT) is a time-frequency analysis tool that decomposes a signal into wavelets. Unlike the Fourier transform, wavelets are localized in both time and frequency.
This implementation uses the specified wavelet function and widths to produce a 2D matrix of coefficients.
Excel Usage
=CWT(data, widths, wavelet_type)
data(list[list], required): The input signal data.widths(list[list], required): Sequence of widths to use for the transform.wavelet_type(str, optional, default: “ricker”): The wavelet function to use.
Returns (list[list]): A 2D matrix of transform coefficients (widths as rows, data as columns).
Example 1: Ricker CWT
Inputs:
| data | widths | wavelet_type | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 3 | 2 | 1 | 1 | 2 | 3 | ricker |
Excel formula:
=CWT({1,2,3,4,3,2,1}, {1,2,3}, "ricker")
Expected output:
| Result | ||||||
|---|---|---|---|---|---|---|
| -0.497416 | 0.0948513 | 1.03926 | 1.90658 | 1.03926 | 0.0948513 | -0.497416 |
| 0.4296 | 2.10362 | 3.77763 | 4.39092 | 3.77763 | 2.10362 | 0.4296 |
| 2.01115 | 3.57677 | 4.91964 | 5.42039 | 4.91964 | 3.57677 | 2.01115 |
Python Code
Show Code
import numpy as np
from scipy.signal import cwt as scipy_cwt
from scipy.signal import ricker, morlet2
def cwt(data, widths, wavelet_type='ricker'):
"""
Perform a Continuous Wavelet Transform (CWT).
See: https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.cwt.html
This example function is provided as-is without any representation of accuracy.
Args:
data (list[list]): The input signal data.
widths (list[list]): Sequence of widths to use for the transform.
wavelet_type (str, optional): The wavelet function to use. Valid options: Ricker, Morlet 2. Default is 'ricker'.
Returns:
list[list]: A 2D matrix of transform coefficients (widths as rows, data as columns).
"""
try:
def to_1d(v):
if isinstance(v, list):
return np.array([float(x) for row in v for x in row])
return np.array([float(v)])
data_arr = to_1d(data)
widths_arr = to_1d(widths)
# Map wavelet_type string to function
wavelet_map = {
'ricker': ricker,
'morlet2': morlet2
}
wav_func = wavelet_map.get(wavelet_type, ricker)
# Determine if output should be complex
out_dtype = np.float64
if wavelet_type == 'morlet2':
out_dtype = np.complex128
result = scipy_cwt(data_arr, wav_func, widths_arr, dtype=out_dtype)
if wavelet_type == 'morlet2':
return np.abs(result).tolist()
return result.tolist()
except Exception as e:
return f"Error: {str(e)}"Online Calculator
The input signal data.
Sequence of widths to use for the transform.
The wavelet function to use.