FFT Utilities¶
spectrans.utils.fft ¶
FFT utilities with MKL compatibility fallbacks.
This module provides wrapper functions for FFT operations that handle MKL compatibility issues on certain platforms. When MKL FFT operations fail due to configuration inconsistencies, these functions automatically fall back to DFT matrix multiplication or sequential 1D transforms.
The fallback implementations maintain mathematical correctness while avoiding MKL-specific issues. All transforms preserve the standard FFT properties including linearity, unitarity (with appropriate normalization), and the convolution theorem.
Environment Variables
SPECTRANS_DISABLE_MKL_FFT : str Set to "1" to force use of fallback implementations.
Notes
The fallback algorithms trade computational efficiency for compatibility. DFT matrix multiplication has \(O(n^2)\) complexity compared to FFT's \(O(n \log n)\), but ensures consistent behavior across platforms.
For 2D transforms, the module decomposes operations into sequential 1D transforms following the separability property:
where \(\mathcal{F}_x\) and \(\mathcal{F}_y\) denote 1D transforms along the respective dimensions.
References
James W. Cooley and John W. Tukey. 1965. An algorithm for the machine calculation of complex Fourier series. Mathematics of Computation, 19(90):297-301.
Functions:
| Name | Description |
|---|---|
safe_rfft2 |
2D real FFT with MKL fallback support. |
safe_irfft2 |
2D inverse real FFT with MKL fallback support. |
safe_rfft |
1D real FFT with MKL fallback support. |
safe_irfft |
1D inverse real FFT with MKL fallback support. |
safe_fft |
1D complex FFT with MKL fallback support. |
safe_ifft |
1D inverse complex FFT with MKL fallback support. |
safe_fft2 |
2D complex FFT with MKL fallback support. |
safe_ifft2 |
2D inverse complex FFT with MKL fallback support. |
safe_rfftn |
N-dimensional real FFT with MKL fallback support. |
safe_irfftn |
N-dimensional inverse real FFT with MKL fallback support. |
Functions¶
safe_rfft2 ¶
safe_rfft2(input: Tensor, s: tuple[int, int] | None = None, dim: tuple[int, int] = (-2, -1), norm: str | None = None) -> Tensor
2D real FFT with MKL fallback support.
Computes the 2D discrete Fourier transform of real-valued input, exploiting Hermitian symmetry to store only non-redundant frequencies. Falls back to sequential 1D transforms when MKL errors occur.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Real-valued input tensor with at least 2 dimensions. |
required |
s
|
tuple[int, int] | None
|
Output sizes along transformed dimensions. If None, uses input sizes. |
None
|
dim
|
tuple[int, int]
|
Dimensions over which to compute the FFT. |
(-2, -1)
|
norm
|
str | None
|
Normalization mode. Options are: - "forward": normalize by 1/n on forward transform - "backward": normalize by 1/n on inverse transform - "ortho": normalize by 1/sqrt(n) for unitarity |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex tensor containing FFT coefficients. Due to Hermitian symmetry, the last dimension contains only positive frequencies with size (n//2 + 1) for input size n along that dimension. |
Notes
The 2D real FFT computes:
For real input, the output satisfies the Hermitian symmetry property: \(X[k_1, k_2] = X^*[N_1-k_1, N_2-k_2]\), allowing storage of only positive frequencies along the last dimension.
The fallback implementation decomposes the 2D transform into sequential 1D operations: first an RFFT along dim[1], then an FFT along dim[0]. This order ensures RFFT operates on real input as required.
Source code in spectrans/utils/fft.py
safe_irfft2 ¶
safe_irfft2(input: Tensor, s: tuple[int, int] | None = None, dim: tuple[int, int] = (-2, -1), norm: str | None = None) -> Tensor
2D inverse real FFT with MKL fallback support.
Computes the 2D inverse discrete Fourier transform for real-valued output from Hermitian-symmetric frequency domain input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Complex tensor containing Hermitian-symmetric FFT coefficients. The last dimension should contain only positive frequencies. |
required |
s
|
tuple[int, int] | None
|
Output spatial sizes. If None, inferred from input: s[1] = 2*(input.shape[dim[1]]-1) for the last dimension. |
None
|
dim
|
tuple[int, int]
|
Dimensions over which to compute the inverse FFT. |
(-2, -1)
|
norm
|
str | None
|
Normalization mode matching forward transform. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Real-valued spatial domain tensor. |
Notes
The inverse transform reconstructs the spatial signal:
where the normalization factor depends on the norm parameter.
The fallback implementation reverses the forward transform decomposition: first IFFT along dim[0], then IRFFT along dim[1]. This ensures the final IRFFT produces real-valued output as expected.
Source code in spectrans/utils/fft.py
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
safe_rfft ¶
1D real FFT with MKL fallback support.
Computes the 1D discrete Fourier transform of real-valued input along a single dimension, storing only positive frequency components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Real-valued input tensor. |
required |
n
|
int | None
|
Signal length along transform dimension. Input is truncated or zero-padded to match. If None, uses input size. |
None
|
dim
|
int
|
Dimension along which to compute the FFT. |
-1
|
norm
|
str | None
|
Normalization mode for scaling FFT outputs. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex tensor of shape (..., n//2+1, ...) containing positive frequency coefficients along the specified dimension. |
Notes
For real input of length \(n\), the FFT satisfies Hermitian symmetry: \(X[k] = X^*[n-k]\). This function returns only frequencies \(k \in [0, n/2]\), reducing memory usage by nearly half.
The fallback uses full complex FFT and extracts positive frequencies, maintaining mathematical equivalence while avoiding MKL issues.
Source code in spectrans/utils/fft.py
safe_irfft ¶
1D inverse real FFT with MKL fallback support.
Reconstructs real-valued signal from Hermitian-symmetric frequency domain representation containing only positive frequencies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Complex tensor containing positive frequency coefficients. Shape along dim should be n//2+1 for output length n. |
required |
n
|
int | None
|
Output signal length. If None, inferred as 2*(input_size-1). |
None
|
dim
|
int
|
Dimension along which to compute inverse FFT. |
-1
|
norm
|
str | None
|
Normalization mode matching the forward transform. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Real-valued reconstructed signal of length n along specified dimension. |
Notes
This function reconstructs the full Hermitian-symmetric spectrum from positive frequencies before applying inverse FFT. For input containing \(m = n//2 + 1\) frequencies, the reconstruction satisfies:
where negative frequencies are constructed via conjugate symmetry: \(X[n-k] = X^*[k]\) for \(k > 0\).
The fallback explicitly constructs the full spectrum and uses complex IFFT, ensuring correctness when MKL operations fail.
Source code in spectrans/utils/fft.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 | |
safe_fft ¶
1D complex FFT with MKL fallback support.
Computes the 1D discrete Fourier transform along a single dimension, supporting both real and complex inputs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Input tensor (real or complex). |
required |
n
|
int | None
|
Signal length along transform dimension. Input is truncated or zero-padded to match. If None, uses input size. |
None
|
dim
|
int
|
Dimension along which to compute the FFT. |
-1
|
norm
|
str | None
|
Normalization mode affecting output scaling. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex tensor containing frequency domain representation. |
Notes
The discrete Fourier transform is defined as:
The fallback implementation uses explicit DFT matrix multiplication with \(O(n^2)\) complexity when MKL FFT fails. While computationally less efficient than FFT's \(O(n \log n)\), it guarantees correctness across all platforms.
Source code in spectrans/utils/fft.py
safe_ifft ¶
1D inverse complex FFT with MKL fallback support.
Computes the 1D inverse discrete Fourier transform, reconstructing signals from frequency domain representation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Complex tensor containing frequency coefficients. |
required |
n
|
int | None
|
Output signal length. Input is truncated or zero-padded. If None, uses input size. |
None
|
dim
|
int
|
Dimension along which to compute inverse FFT. |
-1
|
norm
|
str | None
|
Normalization mode matching the forward transform. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex tensor containing reconstructed signal. For originally real signals, imaginary components represent numerical error. |
Notes
The inverse transform is:
where the normalization factor depends on the norm parameter. The fallback uses DFT matrix multiplication with conjugate transpose for the inverse operation.
Source code in spectrans/utils/fft.py
safe_fft2 ¶
safe_fft2(input: Tensor, s: tuple[int, int] | None = None, dim: tuple[int, int] = (-2, -1), norm: str | None = None) -> Tensor
2D complex FFT with MKL fallback support.
Computes the 2D discrete Fourier transform for complex or real input. Falls back to sequential 1D transforms when MKL errors occur.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Input tensor (real or complex). |
required |
s
|
tuple[int, int] | None
|
Output sizes along transformed dimensions. If None, uses input sizes. |
None
|
dim
|
tuple[int, int]
|
Dimensions over which to compute the FFT. |
(-2, -1)
|
norm
|
str | None
|
Normalization mode. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex tensor containing 2D FFT coefficients. |
Source code in spectrans/utils/fft.py
safe_ifft2 ¶
safe_ifft2(input: Tensor, s: tuple[int, int] | None = None, dim: tuple[int, int] = (-2, -1), norm: str | None = None) -> Tensor
2D inverse complex FFT with MKL fallback support.
Computes the 2D inverse discrete Fourier transform. Falls back to sequential 1D transforms when MKL errors occur.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Complex tensor containing FFT coefficients. |
required |
s
|
tuple[int, int] | None
|
Output spatial sizes. If None, uses input sizes. |
None
|
dim
|
tuple[int, int]
|
Dimensions over which to compute the inverse FFT. |
(-2, -1)
|
norm
|
str | None
|
Normalization mode matching forward transform. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex tensor containing spatial domain result. |
Source code in spectrans/utils/fft.py
safe_rfftn ¶
safe_rfftn(input: Tensor, s: tuple[int, ...] | None = None, dim: tuple[int, ...] | None = None, norm: str | None = None) -> Tensor
N-dimensional real FFT with MKL fallback support.
Computes the N-dimensional discrete Fourier transform of real input. Falls back to sequential transforms when MKL errors occur.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Real-valued input tensor. |
required |
s
|
tuple[int, ...] | None
|
Output sizes along transformed dimensions. |
None
|
dim
|
tuple[int, ...] | None
|
Dimensions to transform. If None, transforms all dimensions. |
None
|
norm
|
str | None
|
Normalization mode. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Complex tensor with positive frequencies only in the last dimension. |
Source code in spectrans/utils/fft.py
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 | |
safe_irfftn ¶
safe_irfftn(input: Tensor, s: tuple[int, ...] | None = None, dim: tuple[int, ...] | None = None, norm: str | None = None) -> Tensor
N-dimensional inverse real FFT with MKL fallback support.
Computes the N-dimensional inverse discrete Fourier transform for real output. Falls back to sequential transforms when MKL errors occur.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Complex tensor with Hermitian symmetry. |
required |
s
|
tuple[int, ...] | None
|
Output spatial sizes. |
None
|
dim
|
tuple[int, ...] | None
|
Dimensions to transform. If None, transforms all dimensions. |
None
|
norm
|
str | None
|
Normalization mode. |
None
|
Returns:
| Type | Description |
|---|---|
Tensor
|
Real-valued spatial domain tensor. |