CWT_PYWT
This function computes the continuous wavelet transform (CWT) of a one-dimensional signal for a set of scales using a selected mother wavelet.
The transform measures similarity between the signal and scaled/shifted wavelets, producing coefficients indexed by scale and position. It also returns pseudo-frequencies corresponding to each scale.
For signal x(t) and wavelet \psi, the CWT at scale a and shift b is:
W_x(a,b)=\frac{1}{\sqrt{|a|}}\int_{-\infty}^{\infty}x(t)\,\psi^*\left(\frac{t-b}{a}\right)dt
Coefficients are returned as rows with frequencies included as the first row.
Excel Usage
=CWT_PYWT(data, scales, wavelet, sampling_period, method, axis)
data(list[list], required): Input signal values as an Excel range.scales(list[list], required): Positive scales used for analysis.wavelet(str, optional, default: “mexh”): Wavelet name.sampling_period(float, optional, default: 1): Sampling period of the input signal (seconds).method(str, optional, default: “conv”): Algorithm used for CWT computation.axis(int, optional, default: -1): Axis index of the signal.
Returns (list[list]): A rectangular 2D range where the first row contains pseudo-frequencies and subsequent rows contain transform coefficients by scale.
Example 1: Mexican hat CWT with three scales
Inputs:
| data | scales | wavelet | sampling_period | method | axis | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 2 | 1 | 1 | 2 | 3 | mexh | 1 | conv | -1 |
Excel formula:
=CWT_PYWT({1,2,3,2,1}, {1,2,3}, "mexh", 1, "conv", -1)
Expected output:
| Result | ||||
|---|---|---|---|---|
| 0.25 | 0.125 | 0.0833333 | ||
| -0.730298 | 0.263894 | 1.54896 | 1.54896 | 0.263894 |
| -0.223961 | 1.67159 | 3.16782 | 3.16782 | 1.67159 |
| 1.08292 | 2.54236 | 3.45789 | 3.45789 | 2.54236 |
Example 2: Morlet CWT using FFT method
Inputs:
| data | scales | wavelet | sampling_period | method | axis | ||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 1 | 0 | -1 | 0 | 1 | 1 | 2 | morl | 0.5 | fft | -1 |
Excel formula:
=CWT_PYWT({0,1,0,-1,0,1}, {1,2}, "morl", 0.5, "fft", -1)
Expected output:
| Result | |||||
|---|---|---|---|---|---|
| 1.625 | 0.8125 | ||||
| 0.120407 | -0.129497 | -0.243535 | 0.248083 | 0.248083 | -0.243535 |
| -0.26446 | -0.0684351 | 0.449466 | -0.281727 | -0.281727 | 0.449466 |
Example 3: Scalar signal is normalized to one-point input
Inputs:
| data | scales | wavelet | sampling_period | method | axis | |
|---|---|---|---|---|---|---|
| 5 | 1 | 2 | gaus1 | 1 | conv | -1 |
Excel formula:
=CWT_PYWT(5, {1,2}, "gaus1", 1, "conv", -1)
Expected output:
| Result | |
|---|---|
| 0.2 | 0.1 |
| -2.81359 | |
| -1.38996 |
Example 4: Matrix data is flattened before transform
Inputs:
| data | scales | wavelet | sampling_period | method | axis | |||
|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 1 | 2 | 4 | gaus2 | 1 | conv | -1 |
| 3 | 4 |
Excel formula:
=CWT_PYWT({1,2;3,4}, {1,2,4}, "gaus2", 1, "conv", -1)
Expected output:
| Result | |||
|---|---|---|---|
| 0.3 | 0.15 | 0.075 | |
| -0.418199 | -0.0357481 | 0.186334 | 1.94042 |
| -1.12753 | 0.393749 | 2.69092 | 3.60706 |
| 0.869678 | 2.72246 | 4.01056 | 4.14076 |
Python Code
import numpy as np
import pywt
def cwt_pywt(data, scales, wavelet='mexh', sampling_period=1, method='conv', axis=-1):
"""
Compute the continuous wavelet transform and associated pseudo-frequencies.
See: https://pywavelets.readthedocs.io/en/latest/ref/cwt.html
This example function is provided as-is without any representation of accuracy.
Args:
data (list[list]): Input signal values as an Excel range.
scales (list[list]): Positive scales used for analysis.
wavelet (str, optional): Wavelet name. Valid options: Mexican Hat, Morlet, Gaussian1, Gaussian2. Default is 'mexh'.
sampling_period (float, optional): Sampling period of the input signal (seconds). Default is 1.
method (str, optional): Algorithm used for CWT computation. Valid options: Convolution, FFT. Default is 'conv'.
axis (int, optional): Axis index of the signal. Default is -1.
Returns:
list[list]: A rectangular 2D range where the first row contains pseudo-frequencies and subsequent rows contain transform coefficients by scale.
"""
try:
def to2d(x):
return [[x]] if not isinstance(x, list) else x
def flatten_numeric(matrix):
flat = []
for row in matrix:
if not isinstance(row, list):
return None
for item in row:
try:
flat.append(float(item))
except (TypeError, ValueError):
continue
return flat
data_vals = flatten_numeric(to2d(data))
scale_vals = flatten_numeric(to2d(scales))
if data_vals is None or scale_vals is None:
return "Error: Invalid input - data and scales must be 2D lists"
if len(data_vals) == 0:
return "Error: Input must contain at least one numeric data value"
if len(scale_vals) == 0:
return "Error: Input must contain at least one numeric scale"
coeffs, freqs = pywt.cwt(
np.asarray(data_vals, dtype=float),
np.asarray(scale_vals, dtype=float),
wavelet=wavelet,
sampling_period=float(sampling_period),
method=method,
axis=int(axis),
)
coeff_matrix = np.asarray(coeffs)
if np.iscomplexobj(coeff_matrix):
coeff_rows = np.abs(coeff_matrix).tolist()
else:
coeff_rows = coeff_matrix.tolist()
rows = [np.asarray(freqs, dtype=float).tolist()] + [list(row) for row in coeff_rows]
max_len = max(len(row) for row in rows)
return [row + [""] * (max_len - len(row)) for row in rows]
except Exception as e:
return f"Error: {str(e)}"