Spectral Transforms¶
spectrans.transforms ¶
Spectral transform implementations for neural networks.
This module provides implementations of spectral transforms used in spectral transformer architectures. All transforms implement consistent interfaces through the base classes, enabling easy substitution and experimentation with different spectral methods. The transforms support both real and complex inputs, batch processing, and multi-dimensional operations where applicable.
Modules:
| Name | Description |
|---|---|
base |
Base classes and interfaces for spectral transforms. |
cosine |
Discrete Cosine and Sine Transform implementations. |
fourier |
Fast Fourier Transform implementations. |
hadamard |
Hadamard and related orthogonal transforms. |
wavelet |
Discrete Wavelet Transform implementations. |
Classes:
| Name | Description |
|---|---|
AdaptiveTransform |
Transform with learnable parameters for adaptation. |
DCT |
Discrete Cosine Transform implementation. |
DCT2D |
2D Discrete Cosine Transform for image-like data. |
DST |
Discrete Sine Transform implementation. |
DWT1D |
1D Discrete Wavelet Transform. |
DWT2D |
2D Discrete Wavelet Transform. |
FFT1D |
1D Fast Fourier Transform with real/complex support. |
FFT2D |
2D Fast Fourier Transform for AFNO-style operations. |
HadamardTransform |
Fast Hadamard Transform implementation. |
HadamardTransform2D |
2D Hadamard Transform implementation. |
MDCT |
Modified Discrete Cosine Transform for audio processing. |
MultiResolutionTransform |
Base class for multi-resolution decompositions. |
NeuralSpectralTransform |
Transform with neural network components. |
OrthogonalTransform |
Base class for orthogonal transforms (DCT, DST, Hadamard). |
RFFT |
Real-input Fast Fourier Transform. |
RFFT2D |
2D Real-input Fast Fourier Transform. |
SequencyHadamardTransform |
Sequency-ordered Hadamard transform. |
SlantTransform |
Slant transform implementation. |
SpectralPooling |
Spectral pooling operation in frequency domain. |
SpectralTransform |
Base class for simple 1D spectral transforms. |
UnitaryTransform |
Base class for unitary transforms (FFT). |
Examples:
Using Fourier transforms:
>>> from spectrans.transforms import FFT1D, RFFT
>>> # Complex-input FFT
>>> fft = FFT1D()
>>> complex_output = fft.transform(complex_input)
>>> reconstructed = fft.inverse_transform(complex_output)
>>>
>>> # Real-input FFT
>>> rfft = RFFT()
>>> freq_domain = rfft.transform(real_input)
Using orthogonal transforms:
>>> from spectrans.transforms import DCT, HadamardTransform
>>> # Discrete Cosine Transform
>>> dct = DCT(normalized=True)
>>> dct_coeffs = dct.transform(signal)
>>>
>>> # Fast Hadamard Transform
>>> hadamard = HadamardTransform()
>>> hadamard_coeffs = hadamard.transform(signal, dim=-1)
Using wavelet transforms:
>>> from spectrans.transforms import DWT1D
>>> dwt = DWT1D(wavelet='db4', levels=3)
>>> approx_coeffs, detail_coeffs = dwt.decompose(signal)
>>> reconstructed = dwt.reconstruct((approx_coeffs, detail_coeffs))
Notes
Mathematical Properties:
The transforms maintain important mathematical properties:
- Orthogonal Transforms (DCT, DST, Hadamard):
- Preserve inner products: \(\langle \mathbf{x}, \mathbf{y} \rangle = \langle \mathcal{T}(\mathbf{x}), \mathcal{T}(\mathbf{y}) \rangle\)
- Perfect reconstruction: \(\mathcal{T}^{-1}(\mathcal{T}(\mathbf{x})) = \mathbf{x}\)
-
Energy conservation (Parseval's theorem)
-
Unitary Transforms (FFT):
- Complex inner product preservation
- Norm conservation: \(||\mathcal{T}(\mathbf{x})||_2 = ||\mathbf{x}||_2\)
-
Hermitian symmetry for real inputs
-
Multi-Resolution Transforms (DWT):
- Perfect reconstruction from coefficients
- Localization in both time and frequency
- Compact support for finite-length wavelets
Implementation Details:
- All transforms support batch processing with proper broadcasting
- Complex number operations use the spectrans.utils.complex module
- Numerical stability is ensured through proper scaling and normalization
- GPU acceleration through PyTorch's native FFT operations
- In-place operations used where possible
Performance Characteristics:
- FFT: \(O(n \log n)\) time complexity
- DCT/DST: \(O(n \log n)\) via FFT-based algorithms
- Hadamard: \(O(n \log n)\) fast transform algorithms
- DWT: \(O(n)\) time complexity with compact support wavelets
See Also
spectrans.transforms.base : Base classes and interfaces.
spectrans.utils.complex : Complex tensor operations.
spectrans.core.registry : Component registration for transforms.
Classes¶
AdaptiveTransform ¶
Bases: NeuralSpectralTransform
Base class for adaptive transforms with learnable parameters.
Adaptive transforms can learn their basis functions or transformation parameters from data. This is useful for applications where the optimal spectral representation depends on the specific data distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_dim
|
int
|
Input dimension size. |
required |
learnable
|
bool
|
Whether transform parameters are learnable. |
True
|
Source code in spectrans/transforms/base.py
MultiResolutionTransform ¶
Bases: Transform
Base class for multi-resolution transforms.
For transforms that decompose signals into multiple components at different resolution levels, such as Discrete Wavelet Transform (DWT).
These transforms are mathematically different from simple spectral transforms as they return multiple components: - Approximation coefficients at the coarsest level - Detail coefficients at each level
This matches the mathematical formulation: DWT(x) = {c_{A_J}, {c_{D_j}}_{j=1}^J}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
levels
|
int
|
Number of decomposition levels. |
1
|
Methods:
| Name | Description |
|---|---|
decompose |
Decompose signal into multiple resolution levels. |
reconstruct |
Reconstruct signal from multi-resolution coefficients. |
Source code in spectrans/transforms/base.py
Functions¶
decompose
abstractmethod
¶
Decompose signal into multiple resolution levels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor to decompose. |
required |
levels
|
int | None
|
Number of levels. If None, use self.levels. |
None
|
dim
|
int
|
Dimension along which to apply decomposition. |
-1
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, list[Tensor]]
|
Tuple of (approximation_coefficients, detail_coefficients_list) where detail_coefficients_list contains coefficients from coarsest to finest level. |
Source code in spectrans/transforms/base.py
reconstruct
abstractmethod
¶
Reconstruct signal from multi-resolution coefficients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coeffs
|
tuple[Tensor, list[Tensor]]
|
Tuple of (approximation_coefficients, detail_coefficients_list). |
required |
dim
|
int
|
Dimension along which to apply reconstruction. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Reconstructed tensor. |
Source code in spectrans/transforms/base.py
NeuralSpectralTransform ¶
Bases: SpectralTransform
Base class for learnable spectral transforms.
This class is for transforms that can learn their parameters during training, such as learnable filters in the frequency domain.
Methods:
| Name | Description |
|---|---|
forward |
Forward pass through the neural spectral transform. |
Functions¶
forward ¶
Forward pass through the neural spectral transform.
By default, applies the transform operation. Subclasses can override this for more complex learned behaviors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Output tensor. |
Source code in spectrans/transforms/base.py
OrthogonalTransform ¶
Bases: SpectralTransform
Base class for orthogonal transforms.
Orthogonal transforms preserve inner products and have the property that their inverse is their transpose. This includes DCT, DST, and Hadamard transforms.
Attributes:
| Name | Type | Description |
|---|---|---|
is_orthogonal |
bool
|
Orthogonal transforms preserve inner products. |
SpectralTransform ¶
Bases: Transform
Base class for simple spectral transforms.
For transforms that map Tensor → Tensor along a specified dimension, such as FFT, DCT, DST, and Hadamard transforms. These transforms operate on a single dimension and return tensors of the same shape.
Mathematical operations supported: - Fourier transforms (FFT, RFFT) - Discrete Cosine Transform (DCT) - Discrete Sine Transform (DST) - Hadamard transform
Methods:
| Name | Description |
|---|---|
transform |
Apply forward transform along specified dimension. |
inverse_transform |
Apply inverse transform along specified dimension. |
Attributes:
| Name | Type | Description |
|---|---|---|
is_orthogonal |
bool
|
Whether the transform is orthogonal. |
is_unitary |
bool
|
Whether the transform is unitary. |
Attributes¶
is_orthogonal
property
¶
Whether the transform is orthogonal.
Returns:
| Type | Description |
|---|---|
bool
|
True if the transform preserves inner products. |
is_unitary
property
¶
Whether the transform is unitary.
Returns:
| Type | Description |
|---|---|
bool
|
True if the transform preserves complex inner products. |
Functions¶
transform
abstractmethod
¶
Apply forward transform along specified dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor to transform. |
required |
dim
|
int
|
Dimension along which to apply the transform. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Transformed tensor with same shape as input. |
Source code in spectrans/transforms/base.py
inverse_transform
abstractmethod
¶
Apply inverse transform along specified dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Transformed tensor to invert. |
required |
dim
|
int
|
Dimension along which to apply the inverse transform. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Inverse transformed tensor with same shape as input. |
Source code in spectrans/transforms/base.py
UnitaryTransform ¶
Bases: SpectralTransform
Base class for unitary transforms.
Unitary transforms preserve complex inner products and have the property that their inverse is their conjugate transpose. This includes the Discrete Fourier Transform (DFT/FFT).
Attributes:
| Name | Type | Description |
|---|---|---|
is_unitary |
bool
|
Unitary transforms preserve complex inner products. |
DCT ¶
Bases: OrthogonalTransform
Discrete Cosine Transform (Type-II).
The DCT-II is the most commonly used DCT variant, often referred to as simply "the DCT". It's widely used in signal compression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normalized
|
bool
|
Whether to use orthonormal normalization. |
True
|
Methods:
| Name | Description |
|---|---|
transform |
Apply DCT-II transform. |
inverse_transform |
Apply inverse DCT (DCT-III). |
Source code in spectrans/transforms/cosine.py
Functions¶
transform ¶
Apply DCT-II transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
dim
|
int
|
Dimension along which to apply DCT. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
DCT coefficients. |
Source code in spectrans/transforms/cosine.py
inverse_transform ¶
Apply inverse DCT (DCT-III).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
DCT coefficients. |
required |
dim
|
int
|
Dimension along which to apply inverse DCT. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Reconstructed signal. |
Source code in spectrans/transforms/cosine.py
DCT2D ¶
Bases: SpectralTransform2D
2D Discrete Cosine Transform.
Applies DCT-II along both spatial dimensions, commonly used in image compression (e.g., JPEG).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normalized
|
bool
|
Whether to use orthonormal normalization. |
True
|
Methods:
| Name | Description |
|---|---|
transform |
Apply 2D DCT. |
inverse_transform |
Apply inverse 2D DCT. |
Source code in spectrans/transforms/cosine.py
Functions¶
transform ¶
Apply 2D DCT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
dim
|
tuple[int, int]
|
Dimensions along which to apply 2D DCT. |
(-2, -1)
|
Returns:
| Type | Description |
|---|---|
Tensor
|
2D DCT coefficients. |
Source code in spectrans/transforms/cosine.py
inverse_transform ¶
Apply inverse 2D DCT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
2D DCT coefficients. |
required |
dim
|
tuple[int, int]
|
Dimensions along which to apply inverse 2D DCT. |
(-2, -1)
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Reconstructed signal. |
Source code in spectrans/transforms/cosine.py
DST ¶
Bases: OrthogonalTransform
Discrete Sine Transform (Type-II).
The DST-II is analogous to the DCT-II but uses sine functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normalized
|
bool
|
Whether to use orthonormal normalization. |
True
|
Methods:
| Name | Description |
|---|---|
transform |
Apply DST-II transform. |
inverse_transform |
Apply inverse DST (DST-III). |
Source code in spectrans/transforms/cosine.py
Functions¶
transform ¶
Apply DST-II transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. |
required |
dim
|
int
|
Dimension along which to apply DST. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
DST coefficients. |
Source code in spectrans/transforms/cosine.py
inverse_transform ¶
Apply inverse DST (DST-III).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
DST coefficients. |
required |
dim
|
int
|
Dimension along which to apply inverse DST. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Reconstructed signal. |
Source code in spectrans/transforms/cosine.py
MDCT ¶
Bases: OrthogonalTransform
Modified Discrete Cosine Transform.
The MDCT is a lapped transform based on DCT-IV with 50% overlap, commonly used in audio compression (MP3, AAC).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
block_size
|
int
|
Size of the transform block (must be even). |
required |
window
|
str
|
Window function to use: "sine" or "vorbis". |
"sine"
|
Methods:
| Name | Description |
|---|---|
transform |
Apply MDCT. |
inverse_transform |
Apply inverse MDCT. |
Source code in spectrans/transforms/cosine.py
Functions¶
transform ¶
Apply MDCT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. Length along dim must be multiple of half_block. |
required |
dim
|
int
|
Dimension along which to apply MDCT. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
MDCT coefficients. |
Source code in spectrans/transforms/cosine.py
inverse_transform ¶
Apply inverse MDCT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
MDCT coefficients. |
required |
dim
|
int
|
Dimension along which to apply inverse MDCT. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Reconstructed signal with overlap-add. |
Source code in spectrans/transforms/cosine.py
FFT1D ¶
Bases: UnitaryTransform
1D Fast Fourier Transform.
Applies 1D FFT along a specified dimension of the input tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
norm
|
FFTNorm
|
Normalization mode: "forward", "backward", or "ortho". |
"ortho"
|
Methods:
| Name | Description |
|---|---|
transform |
Apply 1D FFT. |
inverse_transform |
Apply inverse 1D FFT. |
Source code in spectrans/transforms/fourier.py
Functions¶
transform ¶
Apply 1D FFT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of real or complex values. |
required |
dim
|
int
|
Dimension along which to apply FFT. |
-1
|
Returns:
| Type | Description |
|---|---|
ComplexTensor
|
Complex-valued FFT result. |
Source code in spectrans/transforms/fourier.py
inverse_transform ¶
Apply inverse 1D FFT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ComplexTensor
|
Complex-valued FFT coefficients. |
required |
dim
|
int
|
Dimension along which to apply inverse FFT. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Inverse FFT result (may be complex if input was complex). |
Source code in spectrans/transforms/fourier.py
FFT2D ¶
Bases: SpectralTransform2D
2D Fast Fourier Transform.
Applies 2D FFT along the last two dimensions of the input tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
norm
|
FFTNorm
|
Normalization mode: "forward", "backward", or "ortho". |
"ortho"
|
Methods:
| Name | Description |
|---|---|
transform |
Apply 2D FFT. |
inverse_transform |
Apply inverse 2D FFT. |
Source code in spectrans/transforms/fourier.py
Functions¶
transform ¶
Apply 2D FFT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor of real or complex values. |
required |
dim
|
tuple[int, int]
|
Dimensions along which to apply 2D FFT. |
(-2, -1)
|
Returns:
| Type | Description |
|---|---|
ComplexTensor
|
Complex-valued 2D FFT result. |
Source code in spectrans/transforms/fourier.py
inverse_transform ¶
Apply inverse 2D FFT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ComplexTensor
|
Complex-valued FFT coefficients. |
required |
dim
|
tuple[int, int]
|
Dimensions along which to apply inverse FFT. |
(-2, -1)
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Inverse FFT result. |
Source code in spectrans/transforms/fourier.py
RFFT ¶
Bases: UnitaryTransform
Real Fast Fourier Transform.
Applies FFT to real-valued inputs, returning only the positive frequency components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
norm
|
FFTNorm
|
Normalization mode: "forward", "backward", or "ortho". |
"ortho"
|
Methods:
| Name | Description |
|---|---|
transform |
Apply real FFT. |
inverse_transform |
Apply inverse real FFT. |
Source code in spectrans/transforms/fourier.py
Functions¶
transform ¶
Apply real FFT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Real-valued input tensor. |
required |
dim
|
int
|
Dimension along which to apply RFFT. |
-1
|
Returns:
| Type | Description |
|---|---|
ComplexTensor
|
Complex-valued RFFT result (positive frequencies only). |
Source code in spectrans/transforms/fourier.py
inverse_transform ¶
Apply inverse real FFT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ComplexTensor
|
Complex-valued RFFT coefficients. |
required |
dim
|
int
|
Dimension along which to apply inverse RFFT. |
-1
|
n
|
int | None
|
Length of the output signal. If None, inferred from input. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Real-valued inverse RFFT result. |
Source code in spectrans/transforms/fourier.py
RFFT2D ¶
Bases: SpectralTransform2D
2D Real Fast Fourier Transform.
Applies 2D FFT to real-valued inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
norm
|
FFTNorm
|
Normalization mode: "forward", "backward", or "ortho". |
"ortho"
|
Methods:
| Name | Description |
|---|---|
transform |
Apply 2D real FFT. |
inverse_transform |
Apply inverse 2D real FFT. |
Source code in spectrans/transforms/fourier.py
Functions¶
transform ¶
Apply 2D real FFT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Real-valued input tensor. |
required |
dim
|
tuple[int, int]
|
Dimensions along which to apply 2D RFFT. |
(-2, -1)
|
Returns:
| Type | Description |
|---|---|
ComplexTensor
|
Complex-valued 2D RFFT result. |
Source code in spectrans/transforms/fourier.py
inverse_transform ¶
inverse_transform(x: ComplexTensor, dim: tuple[int, int] = (-2, -1), s: tuple[int, int] | None = None) -> Tensor
Apply inverse 2D real FFT.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
ComplexTensor
|
Complex-valued RFFT coefficients. |
required |
dim
|
tuple[int, int]
|
Dimensions along which to apply inverse RFFT. |
(-2, -1)
|
s
|
tuple[int, int] | None
|
Output signal size. If None, inferred from input. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Real-valued inverse RFFT result. |
Source code in spectrans/transforms/fourier.py
SpectralPooling ¶
Bases: UnitaryTransform
Spectral pooling via frequency domain truncation.
Reduces spatial dimensions by truncating high-frequency components in the Fourier domain.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
output_size
|
int | tuple[int, ...]
|
Target output size after pooling. |
required |
norm
|
FFTNorm
|
Normalization mode for FFT operations. |
"ortho"
|
Methods:
| Name | Description |
|---|---|
transform |
Apply spectral pooling. |
inverse_transform |
Inverse is not well-defined for pooling operations. |
Source code in spectrans/transforms/fourier.py
Functions¶
transform ¶
Apply spectral pooling.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor to pool. |
required |
dim
|
int | tuple[int, ...]
|
Dimensions to pool along. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Spectrally pooled tensor. |
Source code in spectrans/transforms/fourier.py
inverse_transform ¶
Inverse is not well-defined for pooling operations.
HadamardTransform ¶
Bases: OrthogonalTransform
Fast Walsh-Hadamard Transform.
The Hadamard transform is an orthogonal transform using only +1 and -1 values. The transform size must be a power of 2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normalized
|
bool
|
Whether to normalize by 1/sqrt(n) for orthogonality. |
True
|
Methods:
| Name | Description |
|---|---|
transform |
Apply Fast Walsh-Hadamard Transform. |
inverse_transform |
Apply inverse Hadamard transform. |
Source code in spectrans/transforms/hadamard.py
Functions¶
transform ¶
Apply Fast Walsh-Hadamard Transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. Size along dim must be power of 2. |
required |
dim
|
int
|
Dimension along which to apply transform. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Hadamard transformed tensor. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If size along dim is not a power of 2. |
Source code in spectrans/transforms/hadamard.py
inverse_transform ¶
Apply inverse Hadamard transform.
The Hadamard transform is self-inverse (up to normalization).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Hadamard coefficients. |
required |
dim
|
int
|
Dimension along which to apply inverse transform. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Inverse transformed tensor. |
Source code in spectrans/transforms/hadamard.py
HadamardTransform2D ¶
Bases: SpectralTransform2D
2D Fast Walsh-Hadamard Transform.
Applies Hadamard transform along two dimensions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normalized
|
bool
|
Whether to normalize for orthogonality. |
True
|
Methods:
| Name | Description |
|---|---|
transform |
Apply 2D Hadamard transform. |
inverse_transform |
Apply inverse 2D Hadamard transform. |
Source code in spectrans/transforms/hadamard.py
Functions¶
transform ¶
Apply 2D Hadamard transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. Sizes along both dims must be powers of 2. |
required |
dim
|
tuple[int, int]
|
Dimensions along which to apply transform. |
(-2, -1)
|
Returns:
| Type | Description |
|---|---|
Tensor
|
2D Hadamard transformed tensor. |
Source code in spectrans/transforms/hadamard.py
inverse_transform ¶
Apply inverse 2D Hadamard transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Hadamard coefficients. |
required |
dim
|
tuple[int, int]
|
Dimensions along which to apply inverse transform. |
(-2, -1)
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Inverse transformed tensor. |
Source code in spectrans/transforms/hadamard.py
SequencyHadamardTransform ¶
Bases: OrthogonalTransform
Sequency-ordered Hadamard Transform.
The sequency ordering arranges basis functions by number of zero-crossings, similar to frequency ordering in Fourier transforms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normalized
|
bool
|
Whether to normalize for orthogonality. |
True
|
Methods:
| Name | Description |
|---|---|
transform |
Apply sequency-ordered Hadamard transform. |
inverse_transform |
Apply inverse sequency-ordered Hadamard transform. |
Source code in spectrans/transforms/hadamard.py
Functions¶
transform ¶
Apply sequency-ordered Hadamard transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. Size along dim must be power of 2. |
required |
dim
|
int
|
Dimension along which to apply transform. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Sequency-ordered Hadamard coefficients. |
Source code in spectrans/transforms/hadamard.py
inverse_transform ¶
Apply inverse sequency-ordered Hadamard transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Sequency-ordered Hadamard coefficients. |
required |
dim
|
int
|
Dimension along which to apply inverse transform. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Inverse transformed tensor. |
Source code in spectrans/transforms/hadamard.py
SlantTransform ¶
Bases: OrthogonalTransform
Slant Transform.
The Slant transform is similar to Hadamard but with varying basis function slopes, providing better energy compaction for certain signals.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
normalized
|
bool
|
Whether to normalize for orthogonality. |
True
|
Methods:
| Name | Description |
|---|---|
transform |
Apply Slant transform. |
inverse_transform |
Apply inverse Slant transform. |
Source code in spectrans/transforms/hadamard.py
Functions¶
transform ¶
Apply Slant transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input tensor. Size along dim should be power of 2. |
required |
dim
|
int
|
Dimension along which to apply transform. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Slant transformed tensor. |
Source code in spectrans/transforms/hadamard.py
inverse_transform ¶
Apply inverse Slant transform.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Slant coefficients. |
required |
dim
|
int
|
Dimension along which to apply inverse transform. |
-1
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Inverse transformed tensor. |
Source code in spectrans/transforms/hadamard.py
DWT1D ¶
Bases: MultiResolutionTransform
PyWavelets-compatible 1D Discrete Wavelet Transform.
This implementation exactly matches PyWavelets behavior based on comprehensive C code analysis. It supports multi-level decomposition and achieves perfect reconstruction (< 1e-6 error) for all wavelets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wavelet
|
WaveletType
|
Wavelet type (e.g., 'db1', 'db2', 'db4', 'db8', 'sym2', 'coif1'). |
'db4'
|
levels
|
int
|
Number of decomposition levels. |
1
|
mode
|
str
|
Boundary handling mode (currently only 'symmetric' supported). |
'symmetric'
|
Attributes:
| Name | Type | Description |
|---|---|---|
wavelet |
str
|
The wavelet type being used. |
levels |
int
|
Number of decomposition levels. |
mode |
str
|
Boundary handling mode. |
dec_lo |
Tensor
|
Low-pass decomposition filter. |
dec_hi |
Tensor
|
High-pass decomposition filter. |
rec_lo |
Tensor
|
Low-pass reconstruction filter. |
rec_hi |
Tensor
|
High-pass reconstruction filter. |
filter_length |
int
|
Length of the wavelet filters. |
Examples:
>>> dwt = DWT1D(wavelet='db4', levels=3)
>>> x = torch.randn(16, 256) # batch_size=16, length=256
>>> cA, cD_list = dwt.decompose(x)
>>> print(f"Approximation shape: {cA.shape}")
>>> print(f"Number of detail levels: {len(cD_list)}")
>>> x_rec = dwt.reconstruct((cA, cD_list))
>>> error = torch.max(torch.abs(x - x_rec))
>>> print(f"Reconstruction error: {error:.2e}")
Methods:
| Name | Description |
|---|---|
decompose |
Multi-level DWT decomposition. |
reconstruct |
Multi-level DWT reconstruction. |
Source code in spectrans/transforms/wavelet.py
Functions¶
decompose ¶
Multi-level DWT decomposition.
Recursively applies DWT to approximation coefficients.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input signal. |
required |
levels
|
int | None
|
Number of levels. If None, uses self.levels. |
None
|
dim
|
int
|
Dimension to decompose along. |
-1
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, list[Tensor]]
|
Tuple of (approximation, [detail_1, ..., detail_N]) where details are ordered from finest to coarsest. |
Source code in spectrans/transforms/wavelet.py
reconstruct ¶
reconstruct(coeffs: tuple[Tensor, list[Tensor]], dim: int = -1, output_len: int | None = None) -> Tensor
Multi-level DWT reconstruction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coeffs
|
tuple[Tensor, list[Tensor]]
|
Tuple of (approximation, [detail_1, ..., detail_N]). |
required |
dim
|
int
|
Dimension to reconstruct along. |
-1
|
output_len
|
int | None
|
Desired output length. If provided, the reconstructed signal will be trimmed or padded to this length. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Reconstructed signal. |
Source code in spectrans/transforms/wavelet.py
DWT2D ¶
Bases: MultiResolutionTransform2D
PyWavelets-compatible 2D Discrete Wavelet Transform.
Implements 2D DWT using separable 1D transforms, applying DWT along each dimension sequentially. Returns coefficients in the standard format: (LL, [(LH, HL, HH) per level]).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
wavelet
|
WaveletType
|
Wavelet type to use. |
'db4'
|
levels
|
int
|
Number of decomposition levels. |
1
|
mode
|
str
|
Boundary handling mode. |
'symmetric'
|
Attributes:
| Name | Type | Description |
|---|---|---|
wavelet |
str
|
The wavelet type. |
levels |
int
|
Number of decomposition levels. |
mode |
str
|
Boundary handling mode. |
dwt1d |
DWT1D
|
1D DWT instance used for separable transforms. |
Examples:
>>> dwt2d = DWT2D(wavelet='db2', levels=2)
>>> image = torch.randn(4, 64, 64) # batch of 4 images
>>> ll, detail_bands = dwt2d.decompose(image)
>>> print(f"LL shape: {ll.shape}")
>>> for i, (lh, hl, hh) in enumerate(detail_bands):
... print(f"Level {i+1} - LH: {lh.shape}, HL: {hl.shape}, HH: {hh.shape}")
>>> reconstructed = dwt2d.reconstruct((ll, detail_bands))
Methods:
| Name | Description |
|---|---|
decompose |
Multi-level 2D DWT decomposition. |
reconstruct |
Multi-level 2D DWT reconstruction. |
Source code in spectrans/transforms/wavelet.py
Functions¶
decompose ¶
decompose(x: Tensor, levels: int | None = None, dim: tuple[int, int] = (-2, -1)) -> tuple[Tensor, list[tuple[Tensor, Tensor, Tensor]]]
Multi-level 2D DWT decomposition.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
Tensor
|
Input 2D tensor. |
required |
levels
|
int | None
|
Number of levels. If None, uses self.levels. |
None
|
dim
|
tuple[int, int]
|
Dimensions to decompose along. |
(-2, -1)
|
Returns:
| Type | Description |
|---|---|
tuple[Tensor, list[tuple[Tensor, Tensor, Tensor]]]
|
Tuple of (LL, [(HL, LH, HH) per level]) following PyWavelets convention where HL is horizontal detail, LH is vertical detail, HH is diagonal detail. |
Source code in spectrans/transforms/wavelet.py
reconstruct ¶
reconstruct(coeffs: tuple[Tensor, list[tuple[Tensor, Tensor, Tensor]]], dim: tuple[int, int] = (-2, -1)) -> Tensor
Multi-level 2D DWT reconstruction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coeffs
|
tuple[Tensor, list[tuple[Tensor, Tensor, Tensor]]]
|
Tuple of (LL, [(HL, LH, HH) per level]) following PyWavelets convention. |
required |
dim
|
tuple[int, int]
|
Dimensions to reconstruct along. |
(-2, -1)
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Reconstructed 2D tensor. |