CWT Edge Effects in PyWavelets: Scales, Frequencies, and Padding

Convert CWT scales to Hz in PyWavelets and measure how far zero, reflection, and periodic padding affect wavelet coefficients at different frequencies.

A low-frequency band looks weaker at the start of a CWT scalogram. The underlying signal may have changed, or the transform may need samples from before the recording began. We can reproduce the second case with PyWavelets and measure how far each padding assumption affects the coefficients at each scale. For the transform and scalogram basics, see the wavelet transform introduction .

Convert scales to Hz

The CWT slides a wavelet along a signal and compares the two at each position. Increasing the scale stretches the wavelet in time and lowers its corresponding frequency. With a sampling period of \(\Delta t\) seconds, the frequency for scale \(s\) is

\[ f(s)=\frac{\operatorname{scale2frequency}(\mathrm{wavelet},s)}{\Delta t}\quad\mathrm{Hz}. \]

For example, with the complex Morlet wavelet cmor1.5-1.0 and a 256 Hz sampling rate, scale 8 corresponds to 32 Hz and scale 64 corresponds to 4 Hz. These are nominal frequencies. Each wavelet also responds to nearby frequencies.

The second return value of pywt.cwt(x, scales, wavelet, sampling_period=1 / fs) gives the same conversion. With the input samples and scales fixed, this argument changes the reported frequencies while leaving the coefficients unchanged. To target the same frequency at a different sampling rate, compute the scale again with pywt.frequency2scale(wavelet, target_hz / fs). At very small scales, the sampled filters can alias, so check their responses when selecting the upper end of the frequency range. PyWavelets CWT documentation

Decide what to use outside the recording

Consider a coefficient centered 20 samples into a recording. If its wavelet extends more than 20 samples to the left, the integral calls for signal values outside the recording. At the same time position, a larger scale makes this more likely.

ExtensionValues outside the recordingWhen the assumption fits
ZeroFill both sides with zerosThe signal is known to be near zero outside the record, as with an isolated pulse
ReflectMirror samples at each end without repeating the endpointThe signal is expected to be locally symmetric about the endpoints
PeriodicRepeat the segment, joining the end to the startThe segment contains a whole number of periods and a seamless join is plausible
More observationsUse real samples on either sideThe interval of interest lies within a longer recording

A coefficient near an edge depends on the chosen extension. The same observed segment can have different reference coefficients depending on whether the signal continued as before or dropped to zero outside the recording.

Measure how far a 1% difference extends

For a synthetic signal we can generate a cosine that extends beyond the observed interval. Use a 256 Hz sampling rate and keep only its central 501 samples. None of the tested periods of 8, 16, 32, and 64 samples divides 501, so repeating the observed segment with wrap supplies a different continuation.

For each row, match the cosine frequency to the wavelet’s nominal frequency. Use the CWT of the long cosine as the reference and compare it with the CWT of the same 501 samples under each extension. Divide the absolute difference between the complex coefficients by the median reference magnitude. Among points that exceed 1%, find the one farthest from its nearest edge. Count the first sample at either edge as one. The reported width is the larger of the two sides.

Install NumPy and PyWavelets to run the example.

python -m pip install numpy PyWavelets
import numpy as np
import pywt

fs = 256
length = 501
pad = 1024
scales = np.array([8, 16, 32, 64])
wavelet = "cmor1.5-1.0"
frequencies = pywt.scale2frequency(wavelet, scales) * fs

n = np.arange(-pad, length + pad)
observed = slice(pad, pad + length)
sample = np.arange(length)
edge_distance = np.minimum(sample, length - 1 - sample)

def coefficients(signal, scale):
    values, _ = pywt.cwt(
        signal, [scale], wavelet, sampling_period=1 / fs, method="conv"
    )
    return values[0, observed]

def affected_width(estimate, reference):
    relative_error = np.abs(estimate - reference) / np.median(np.abs(reference))
    affected = edge_distance[relative_error > 0.01]
    return int(np.max(affected + 1, initial=0))

print("scale    Hz   zero  reflect  wrap")
for scale, hz in zip(scales, frequencies):
    full_signal = np.cos(2 * np.pi * hz * n / fs + 0.2)
    segment = full_signal[observed]
    reference = coefficients(full_signal, scale)
    widths = [
        affected_width(coefficients(np.pad(segment, pad, mode=mode), scale), reference)
        for mode in ("constant", "reflect", "wrap")
    ]
    print(f"{scale:5d} {hz:5.1f} {widths[0]:6d} {widths[1]:8d} {widths[2]:5d}")
ScaleFrequency (Hz)Zero (samples)Reflect (samples)Periodic (samples)
832171419
1616333535
328666973
644133139133

With zeros, the difference extends 17 samples (about 66 ms) at 32 Hz and 133 samples (about 520 ms) at 4 Hz. Reflection reduces that width at 32 Hz and increases it at the other three frequencies. Periodic and zero padding have the same width at 4 Hz, while their actual coefficient errors can differ. The table locates the last sample above the threshold for this particular experiment. It gives no ranking of the overall coefficient error.

Interpret features near the edges of a scalogram

The region that depends on missing samples varies with frequency. Wavelet plots often mark a cone of influence (COI) to help readers account for edge effects. In the definition by Torrence and Compo, the boundary is based on the time for wavelet power from an edge discontinuity to decay to \(e^{-2}\) of its edge value. Here, the 1% threshold is applied to the difference between complex coefficients and a known reference. When drawing a COI, specify the wavelet and the definition used for its boundary. Torrence and Compo, A Practical Guide to Wavelet Analysis

  1. With a longer recording, compute the CWT with the surrounding samples in place and then extract coefficients for the interval of interest.
  2. If surrounding samples are unavailable, try plausible extensions and compare coefficients or peak locations at the frequencies and times you care about.
  3. When marking an affected region on the plot, vary its width by frequency and state the extension method and threshold. This helps readers judge where an event near the edge depends on the continuation assumption.

Frequently asked questions

How do I convert CWT scales to Hz?

Multiply pywt.scale2frequency(wavelet, scales) by the sampling frequency. If you pass the sampling period to pywt.cwt, its second return value gives the corresponding frequencies.

Why do edge effects extend farther at low frequencies?

With the same wavelet, lower frequencies correspond to larger scales. A wider wavelet reaches outside the observed interval even when it is centered farther from an edge.

Does reflection padding make coefficients near the edges reliable?

Reflection helps when the unobserved signal resembles a mirror image of the observed samples. For real data, extend the recording if possible or compare plausible extensions to see how much the coefficients and conclusions depend on the boundary condition.