API reference¶
The package exports the full functional and module APIs at the top level
(import torchwavelets as tw). The compat submodule mirrors the
pytorch_wavelets API.
Precision contract
Every transform preserves the input dtype (fp16/bf16/fp32/fp64). The DTCWT family is the documented exception: half and bfloat16 inputs are computed in float32 (no complex16 kernels) and returned as float32/complex64; it also runs in an autocast-disabled scope.
Discrete wavelet transform¶
1-D¶
transform1d ¶
1-D discrete wavelet transform (functional and module API).
The module API resolves its filters into registered buffers at construction
time (eager) and only performs torch ops (.to(dtype, device) + convs) in
forward, so it is fully traceable by torch.compile.
Classes¶
DWT1D ¶
Bases: Module
1-D DWT module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
J
|
int
|
number of decomposition levels. |
1
|
wave
|
str or Wavelet or tuple
|
as in :func: |
'db1'
|
mode
|
str
|
as in :func: |
'symmetric'
|
The precision follows the input tensor: feed a float16 tensor and the
output is float16, regardless of the dtype used at construction time.
The module is torch.compile-compatible (filters are resolved eagerly
at construction; only torch ops run in forward).
IDWT1D ¶
Bases: Module
1-D inverse DWT module (see :func:idwt1d).
Functions:¶
dwt1d ¶
Forward 1-D DWT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
|
required |
wave
|
str or Wavelet or tuple
|
wavelet name, |
'db1'
|
mode
|
str
|
boundary mode. |
'symmetric'
|
level
|
int
|
number of decomposition levels ( |
1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
tuple[torch.Tensor, list[torch.Tensor]]: |
list[Tensor]
|
the final lowpass of shape |
tuple[Tensor, list[Tensor]]
|
|
tuple[Tensor, list[Tensor]]
|
input dtype. |
idwt1d ¶
Inverse 1-D DWT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coeffs
|
tuple
|
|
required |
wave
|
str or Wavelet or tuple
|
as in :func: |
'db1'
|
mode
|
str
|
as in :func: |
'symmetric'
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: reconstructed |
Tensor
|
coefficients). |
This is a pywt.waverec-style inverse: the coefficient pyramid from
:func:dwt1d is inverted level by level, and (like pywt.waverec) an
odd-length previous level's extra sample is dropped inside the
reconstruction. Unlike pywt.idwt (single-level), the lowpass/detail
pair of a level does not need to match in length.
2-D¶
transform2d ¶
2-D discrete wavelet transform (functional and module API).
The module API resolves its filters into registered buffers at construction
time (eager) and only performs torch ops (.to(dtype, device) + convs) in
forward, so it is fully traceable by torch.compile.
Classes¶
DWT2D ¶
Bases: Module
2-D DWT module.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
J
|
int
|
number of decomposition levels. |
1
|
wave
|
str or Wavelet or tuple
|
as in :func: |
'db1'
|
mode
|
str
|
as in :func: |
'symmetric'
|
The precision follows the input tensor: feed a float16 tensor and the
output is float16, regardless of the dtype used at construction time.
The module is torch.compile-compatible (filters are resolved eagerly
at construction; only torch ops run in forward).
IDWT2D ¶
Bases: Module
2-D inverse DWT module (see :func:idwt2d).
Functions:¶
dwt2d ¶
Forward 2-D DWT (separable, row then column filtering).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
|
required |
wave
|
str or Wavelet or tuple
|
wavelet name, |
'db1'
|
mode
|
str
|
boundary mode. |
'symmetric'
|
level
|
int
|
number of decomposition levels ( |
1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
|
list[Tensor]
|
|
tuple[Tensor, list[Tensor]]
|
|
tuple[Tensor, list[Tensor]]
|
(finest first). |
tuple[Tensor, list[Tensor]]
|
|
idwt2d ¶
Inverse 2-D DWT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coeffs
|
tuple
|
|
required |
wave
|
str or Wavelet or tuple
|
as in :func: |
'db1'
|
mode
|
str
|
as in :func: |
'symmetric'
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: reconstructed |
Tensor
|
the coefficients). |
Dual-tree complex wavelet transform¶
1-D¶
transform1d ¶
1-D dual-tree complex wavelet transform (functional and module API).
The 1-D DTCWT produces complex bandpass coefficients from the two decimation phases of the highpass filtering (real = even phase, imaginary = odd phase), so a single 1-D DTCWT level yields a complex band that is approximately shift-invariant and analytic. Levels 2+ use the quarter-sample-shift filters, exactly as in the 2-D transform.
Precision follows the input (float32 -> complex64, float64 -> complex128); half inputs are computed in float32 (no usable complex16 kernels). Boundary handling is Kingsbury's symmetric extension.
Classes¶
DTCWT1D ¶
Bases: Module
1-D DTCWT module (see :func:dtcwt1d).
IDTCWT1D ¶
Bases: Module
1-D inverse DTCWT module (see :func:idtcwt1d).
Functions:¶
dtcwt1d ¶
Forward 1-D dual-tree complex wavelet transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
|
required |
biort
|
str
|
filter bank name. |
'near_sym_a'
|
qshift
|
str
|
filter bank name. |
'qshift_a'
|
J
|
int
|
number of levels ( |
3
|
include_scale
|
bool
|
also return the intermediate lowpasses. |
False
|
Odd-length inputs are padded by one sample (the last sample is repeated)
and the inverse returns the padded length L + 1, matching the 2-D
transform's convention.
Returns:
| Type | Description |
|---|---|
Tensor | list[Tensor]
|
tuple[torch.Tensor, list[torch.Tensor]]: |
list[Tensor] | None
|
|
tuple[Tensor | list[Tensor], list[Tensor] | None]
|
(finest first). |
idtcwt1d ¶
Inverse 1-D DTCWT (exact inverse of :func:dtcwt1d).
2-D¶
transform2d ¶
2-D dual-tree complex wavelet transform (functional and module API).
The bandpass coefficients are native complex tensors (torch.complex64 /
torch.complex128): yh[j] has shape (N, C, 6, H, W) with the 6
orientations (15, 45, 75, 105, 135, 165 degrees) along dim 2.
Precision follows the input: float32 -> complex64, float64 -> complex128. Half inputs (float16/bfloat16) are computed in float32 and return complex64 (torch has no usable complex16 kernels). Boundary handling follows Kingsbury's symmetric extension (the DTCWT is designed for it); other modes are not supported.
Classes¶
DTCWT2D ¶
Bases: Module
2-D DTCWT module (see :func:dtcwt2d).
Filters are resolved eagerly at construction; the forward pass is pure
torch ops, so the module is torch.compile-compatible.
IDTCWT2D ¶
Bases: Module
2-D inverse DTCWT module (see :func:idtcwt2d).
Functions:¶
dtcwt2d ¶
dtcwt2d(x, biort='near_sym_a', qshift='qshift_a', J=3, skip_hps=False, include_scale=False, o_dim=2)
Forward 2-D dual-tree complex wavelet transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
|
required |
biort
|
str
|
filter bank name (see
:mod: |
'near_sym_a'
|
qshift
|
str
|
filter bank name. |
'qshift_a'
|
J
|
int
|
number of levels ( |
3
|
skip_hps
|
bool or list[bool]
|
one per level, to skip bandpasses. |
False
|
include_scale
|
bool or list[bool]
|
one per level, to also return the intermediate lowpasses. |
False
|
o_dim
|
int
|
dimension for the 6 orientations (default 2). |
2
|
Returns:
| Type | Description |
|---|---|
Tensor
|
tuple[torch.Tensor, list[torch.Tensor]]: |
list[Tensor]
|
|
tuple[Tensor, list[Tensor]]
|
|
idtcwt2d ¶
Inverse 2-D DTCWT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coeffs
|
tuple
|
|
required |
biort
|
str
|
as in :func: |
'near_sym_a'
|
qshift
|
str
|
as in :func: |
'qshift_a'
|
o_dim
|
int
|
as in :func: |
2
|
Returns:
| Type | Description |
|---|---|
Tensor
|
torch.Tensor: reconstructed |
3-D¶
transform3d ¶
3-D dual-tree complex wavelet transform (functional and module API).
The 3-D DTCWT (Kingsbury's design, as in the dtcwt numpy package) runs
four separable trees with the level-1 biorthogonal and level-2+
quarter-sample-shift filters on a doubled work grid and combines the octant
outputs into 28 complex orientation subbands per level (7 (H/L) face
combinations x 4 subbands each).
Precision follows the input (float32 -> complex64, float64 -> complex128);
half inputs are computed in float32. Boundary handling is Kingsbury's
symmetric extension (ext_mode=4: each input dimension must be even;
higher levels are edge-padded to a multiple of 4).
Classes¶
DTCWT3D ¶
Bases: Module
3-D DTCWT module (see :func:dtcwt3d).
IDTCWT3D ¶
Bases: Module
3-D inverse DTCWT module (see :func:idtcwt3d).
Functions:¶
dtcwt3d ¶
Forward 3-D DTCWT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
|
required |
biort
|
str
|
filter bank name. |
'near_sym_a'
|
qshift
|
str
|
filter bank name. |
'qshift_a'
|
J
|
int
|
number of levels. |
3
|
Returns:
| Type | Description |
|---|---|
Tensor
|
tuple[torch.Tensor, list[torch.Tensor]]: |
list[Tensor] | None
|
|
tuple[Tensor, list[Tensor] | None]
|
|
idtcwt3d ¶
Inverse 3-D DTCWT (exact inverse of :func:dtcwt3d).
DTCWT scattering¶
lowlevel ¶
Dual-tree complex wavelet scattering layers.
A scattering layer applies one (or two) DTCWT scale(s) with the smooth
modulus sqrt(|Wx|^2 + magbias^2) - magbias and stacks the results along
the channel dimension. With the DTCWT's near shift-invariance this gives
locally shift-invariant feature maps for a single, cheap layer — the classic
DTCWT scattering front-end.
The implementation is built directly on the complex-native DTCWT, so no
custom autograd.Function is needed: gradients flow through the complex
ops, autocast works, and the module is torch.compile-compatible.
Classes¶
ScatLayer ¶
Bases: Module
One order of scattering at a single scale (level-1 DTCWT).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
biort
|
str
|
level-1 biorthogonal filter bank name. |
'near_sym_a'
|
mode
|
str
|
boundary handling (only |
'symmetric'
|
magbias
|
float
|
smoothing bias of the modulus. |
0.01
|
combine_colour
|
bool
|
for 3-channel input, combine colour channels into a single set of 6 orientation magnitudes (output has 3+6 channels instead of 1+6*C). |
False
|
Returns a feature map (N, 1 + 6*C, H/2, W/2) (or (N, 9, H/2, W/2)
with combine_colour), the lowpass followed by the invariant terms.
ScatLayerj2 ¶
Bases: Module
Two scales of scattering with second-order terms.
Applies the level-1 biorthogonal and level-2 qshift DTCWT filters (the
correct two-scale filter set, unlike stacking two :class:ScatLayer).
Returns (N, 49*C, H/4, W/4): C lowpass + 12*C first-order
invariants (scales 1 and 2) + 36*C second-order invariants (or
(N, 3 + 12 + 36, ...) with combine_colour).
network ¶
General (arbitrary-order) DTCWT scattering network.
A scattering network computes locally shift-invariant feature maps by
iterating wavelet bandpasses with the smooth modulus sqrt(|Wx|^2 + b^2) - b
and smoothing the intermediate invariants with the wavelet lowpass. With the
DTCWT's near shift-invariance this gives a small, cheap, highly invariant
front-end for classification and segmentation.
Paths are sequences of increasing scales (j1 < j2 < ... < jm) with
m <= max_order. For each path the network returns one invariant:
avg_pool2d(lowpass, 2) when the path can still be extended (the smoothed
modulus feeding the next order) and the raw |bandpass| at the terminal
paths. With (J=2, max_order=2) the output matches
:class:~torchwavelets.scatter.lowlevel.ScatLayerj2 exactly.
Shift invariance: the raw level-1 modulus |W_1 x| is only weakly shift
invariant (the single-tree level-1 design), so individual raw bandpass
invariants drift with sub-scale shifts; the smoothed invariants (pooled
lowpasses) and the order-0 lowpass are exactly invariant at dyadic integer
shifts, and the invariance improves with scale.
Classes¶
DTCWTScattering ¶
Bases: Module
DTCWT scattering network module (see :func:dtcwt_scattering).
Functions:¶
dtcwt_scattering ¶
Forward DTCWT scattering network.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
|
required |
J
|
int
|
number of wavelet scales. |
3
|
max_order
|
int
|
maximum scattering order. |
2
|
biort
|
str
|
filter bank name. |
'near_sym_a'
|
qshift
|
str
|
filter bank name. |
'qshift_a'
|
magbias
|
float
|
smoothing bias of the modulus. |
0.01
|
Returns:
| Type | Description |
|---|---|
Tensor
|
tuple[torch.Tensor, list[tuple[tuple[int, ...], torch.Tensor]]]: |
list[tuple[tuple[int, ...], Tensor]]
|
|
tuple[Tensor, list[tuple[tuple[int, ...], Tensor]]]
|
|
tuple[Tensor, list[tuple[tuple[int, ...], Tensor]]]
|
per path (orders 1..max_order, ordered by order then scale). The |
tuple[Tensor, list[tuple[tuple[int, ...], Tensor]]]
|
invariant size depends only on the path's final scale |
tuple[Tensor, list[tuple[tuple[int, ...], Tensor]]]
|
|
Stationary (undecimated) wavelet transform¶
transform ¶
Stationary (undecimated) wavelet transform: functional and module API.
The SWT (algorithm a trous) keeps every subband at the input resolution by
dilating the analysis filters by 2**(level-1) and skipping decimation.
Coefficients follow the PyWavelets conventions exactly (coarsest first), and
iswt / iswt2 are exact inverses (verified bit-for-bit against pywt
by the parity tests).
Inputs must be divisible by 2**level along every transformed axis, as
required by the stationary transform (PyWavelets raises the same error).
Classes¶
SWT1D ¶
Bases: Module
1-D stationary wavelet transform module (see :func:swt1d).
ISWT1D ¶
Bases: Module
1-D inverse stationary wavelet transform module (see :func:iswt1d).
SWT2D ¶
Bases: Module
2-D stationary wavelet transform module (see :func:swt2d).
ISWT2D ¶
Bases: Module
2-D inverse stationary wavelet transform module (see :func:iswt2d).
Functions:¶
swt1d ¶
1-D stationary wavelet transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
|
required |
wave
|
str or Wavelet or tuple
|
wavelet name / bank (see
:func: |
required |
level
|
int
|
number of scales. |
required |
norm
|
bool
|
normalize so the coefficient energy equals the input energy (orthogonal wavelets only, like PyWavelets). |
False
|
Returns:
| Type | Description |
|---|---|
list[tuple[Tensor, Tensor]]
|
list[tuple[torch.Tensor, torch.Tensor]]: ``[(cA_n, cD_n), ..., |
list[tuple[Tensor, Tensor]]
|
(cA_1, cD_1)] |
iswt1d ¶
Inverse 1-D stationary wavelet transform (exact inverse of :func:swt1d).
swt2d ¶
2-D stationary wavelet transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
|
required |
wave
|
str or Wavelet or tuple
|
as in :func: |
required |
level
|
int
|
as in :func: |
required |
norm
|
bool
|
as in :func: |
False
|
Returns:
| Type | Description |
|---|---|
list[tuple[Tensor, tuple[Tensor, Tensor, Tensor]]]
|
list[tuple[torch.Tensor, tuple[torch.Tensor, torch.Tensor, torch.Tensor]]]: |
list[tuple[Tensor, tuple[Tensor, Tensor, Tensor]]]
|
|
list[tuple[Tensor, tuple[Tensor, Tensor, Tensor]]]
|
of shape |
iswt2d ¶
Inverse 2-D stationary wavelet transform (exact inverse of :func:swt2d).
Wavelet packets¶
packets ¶
Wavelet packets (full binary decomposition tree) for 1D and 2D.
A wavelet packet decomposes both the approximation AND the detail bands at
every level, producing a full binary (1D) or quaternary (2D) tree. The API
mirrors pywt.WaveletPacket / pywt.WaveletPacket2D: nodes are accessed
by path ("aad", "av", ...), the tree is built with decompose(),
reconstructed with reconstruct(), and coefficients match PyWavelets
bit-for-bit (verified by the parity tests).
All node data are torch tensors of shape (N, C, L) (1D) or
(N, C, H, W) (2D); the precision follows the input dtype.
Classes¶
WaveletPacket ¶
Bases: BaseNode
1-D wavelet packet decomposition tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Tensor
|
|
required |
wavelet
|
str or Wavelet or tuple
|
as in :func: |
required |
mode
|
str
|
as in :func: |
'symmetric'
|
maxlevel
|
int
|
maximum decomposition depth (default: pywt's rule). |
None
|
axis
|
int
|
the signal axis (default |
-1
|
Children are named 'a' (approximation) and 'd' (detail).
WaveletPacket2D ¶
Bases: BaseNode
2-D wavelet packet decomposition tree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Tensor
|
|
required |
wavelet
|
str or Wavelet or tuple
|
as in :func: |
required |
mode
|
str
|
as in :func: |
'smooth'
|
maxlevel
|
int
|
maximum decomposition depth (default: pywt's rule). |
None
|
axes
|
tuple[int, int]
|
the transformed axes ( |
(-2, -1)
|
Children are named 'a' (LL), 'h' (LH), 'v' (HL), 'd' (HH),
following PyWavelets' convention ('h'/'v' match cH/cV).
pytorch_wavelets compatibility¶
compat ¶
Drop-in compatibility with the pytorch_wavelets API.
The upstream class names are plain aliases: the constructor signatures and
call semantics are identical, only the semantics are fixed (precision follows
the input dtype, autocast works, and boundary handling matches PyWavelets
exactly). For the DTCWT, the bandpass coefficients are converted from the
native complex format to the upstream (..., 2) real/imag layout.
Boundary modes and filter banks¶
modes ¶
Boundary modes: PyWavelets-exact padding for all supported modes.
filters ¶
Filter-bank handling: PyWavelets-compatible wavelets and custom banks.
Custom banks are validated for perfect reconstruction at creation time, so a biorthogonal bank passed as a 2-tuple (which assumes orthogonality) raises immediately instead of silently producing a wrong inverse.
Classes¶
FilterBank
dataclass
¶
A 1-D two-channel filter bank in PyWavelets' natural (non-reversed) order.
Attributes:
| Name | Type | Description |
|---|---|---|
h0 |
ndarray
|
analysis lowpass ( |
h1 |
ndarray
|
analysis highpass ( |
g0 |
ndarray
|
synthesis lowpass ( |
g1 |
ndarray
|
synthesis highpass ( |
Functions:¶
filter_bank ¶
Resolve wave to a :class:FilterBank.
wave may be a PyWavelets wavelet name ("db4"), a
:class:pywt.Wavelet, a 2-tuple (h0, h1) (orthogonal custom bank), or
a 4-tuple (h0, h1, g0, g1) (general bank). Custom banks are validated
for perfect reconstruction and raise ValueError otherwise.