Fast Fourier Transform (FFT)#
The sarcomp.fft
module includes functions to perform 1D and 2D Fast Fourier Transforms (FFTs). It supports both split-complex and ndarray inputs for complex-to-complex (c2c) and real-to-complex (r2c) FFTs. Currently, the module accommodates data sizes that are a power of 2, utilizing mixed radix (radix 2 and radix 4) FFTs. Future iterations of this module will extend this capability to support radix 3 and radix 5, allowing FFTs for data sizes with any prime factorization.
Note
Currently, speed optimization is limited to range FFTs. Azimuth FFTs will be enhanced for speed in future updates.
FFT Class#
The detailed documentation for the FFT functions is automatically included below.
- class sarcomp.fft.FFT(data=None, dtype=None, shape=None, direction=0, axis=1, type='c2c')#
Bases:
object
FFT class for performing Fast Fourier Transforms (FFTs) on both standard and split-complex arrays.
This class provides functionality for executing real-to-complex (r2c) and complex-to-complex (c2c) FFTs. It uses NumPy arrays for standard formats and the Array class for split-complex formats.
- supported_sizes#
List of supported FFT sizes.
- Type:
list
- FORWARD#
Constant representing the forward FFT direction.
- Type:
int
- BACKWARD#
Constant representing the backward FFT direction.
- Type:
int
- r2c#
String identifier for real-to-complex FFTs.
- Type:
str
- c2c#
String identifier for complex-to-complex FFTs.
- Type:
str
- fft_plan#
FFT plan object created during initialization.
- Type:
object
- fft_exec#
Function for executing the FFT.
- Type:
function
- plan_isset#
Flag indicating if the FFT plan has been set.
- Type:
bool
- __init__(data=None, dtype=None, shape=None, direction=0, axis=1, type='c2c')#
Initialize the FFT instance.
- __call__(data=None)#
Execute the FFT.
- free()#
Free the resources used for the FFT.
Methods
free
()Free the resources used for the FFT.
Get the execution function for the FFT.
- free()#
Free the resources used for the FFT.
This method releases any allocated buffers and lookup tables used for the FFT calculation.
- get_exec_function()#
Get the execution function for the FFT.
- Returns:
The FFT execution function.
- Return type:
function
- supported_sizes = [32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768]#
Functions#
- sarcomp.fft.fft(data: ndarray | Array | None = None, dtype: str | dtype | None = None, shape: tuple | None = None, axis: int = 1) FFT #
Perform a complex-to-complex FFT.
This function creates an instance of the FFT class configured to perform a complex-to-complex FFT on the provided data.
- Parameters:
data (Union[np.ndarray, Array], optional) – The input data array for the FFT operation.
dtype (str, optional) – Data type of the FFT (complex or split_complex). Default is ‘complex64’.
shape (tuple, optional) – Shape of the FFT. Required if data is not provided.
axis (int, optional) – Axis along which the FFT is applied. Default is 1.
- Returns:
An instance of the FFT class.
- Return type:
Examples
>>> import numpy as np >>> from sarcomp.fft import fft # Adjust import path as needed
>>> data = np.random.random((64, 64)) + 1j * np.random.random((64, 64)) >>> myfft = fft(data) >>> myfft() # Execute the FFT >>> myfft(other_data) # Execute the FFT with other data of the same shape and dtype >>> # Or create a plan without data for reuse >>> myfft = fft(dtype='complex64', shape=(64, 64)) >>> myfft(data) # Make sure that data matches the dtype and shape provided earlier >>> myfft(other_data) # Execute the FFT with other data of the same shape and dtype
- sarcomp.fft.ifft(data: ndarray | Array | None = None, dtype: str | dtype | None = None, shape: tuple | None = None, axis: int = 1) FFT #
Perform an inverse complex-to-complex FFT.
This function creates an instance of the FFT class configured to perform an inverse complex-to-complex FFT on the provided data.
- Parameters:
data (Union[np.ndarray, Array], optional) – The input data array for the FFT operation.
dtype (str, optional) – Data type of the FFT (complex or split_complex). Default is ‘complex64’.
shape (tuple, optional) – Shape of the FFT. Required if data is not provided.
axis (int, optional) – Axis along which the FFT is applied. Default is 1.
- Returns:
An instance of the FFT class.
- Return type:
Examples
>>> import numpy as np >>> from sarcomp.fft import ifft # Adjust import path as needed
>>> data = np.random.random((64, 64)) + 1j * np.random.random((64, 64)) >>> myifft = ifft(data) >>> myifft() # Execute the inverse FFT >>> myifft(other_data) # Execute the inverse FFT with other data of the same shape and dtype >>> # Or create a plan without data for reuse >>> myifft = ifft(dtype='complex64', shape=(64, 64)) >>> myifft(data) # Make sure that data matches the dtype and shape provided earlier >>> myifft(other_data) # Execute the inverse FFT with other data of the same shape and dtype
- sarcomp.fft.fft2(data: ndarray | Array | None = None, dtype: str | dtype | None = None, shape: tuple | None = None) FFT #
Perform a 2D complex-to-complex FFT.
This function creates an instance of the FFT class configured to perform a 2D complex-to-complex FFT on the provided data.
- Parameters:
data (Union[np.ndarray, Array], optional) – The input data array for the FFT operation.
dtype (str, optional) – Data type of the FFT (complex or split_complex). Default is ‘complex64’.
shape (tuple, optional) – Shape of the FFT. Required if data is not provided.
- Returns:
An instance of the FFT class.
- Return type:
Examples
>>> import numpy as np >>> from sarcomp.fft import fft2 # Adjust import path as needed
>>> data = np.random.random((4, 4)) + 1j * np.random.random((4, 4)) >>> myfft = fft2(data) >>> myfft() # Execute the FFT >>> myfft(other_data) # Execute the FFT with other data of the same shape and dtype
- sarcomp.fft.ifft2(data: ndarray | Array | None = None, dtype: str | dtype | None = None, shape: tuple | None = None) FFT #
Perform a 2D inverse complex-to-complex FFT.
This function creates an instance of the FFT class configured to perform a 2D inverse complex-to-complex FFT on the provided data.
- Parameters:
data (Union[np.ndarray, Array], optional) – The input data array for the FFT operation.
dtype (str, optional) – Data type of the FFT (complex or split_complex). Default is ‘complex64’.
shape (tuple, optional) – Shape of the FFT. Required if data is not provided.
- Returns:
An instance of the FFT class.
- Return type:
Examples
>>> import numpy as np >>> from sarcomp.fft import ifft2 # Adjust import path as needed
>>> data = np.random.random((4, 4)) + 1j * np.random.random((4, 4)) >>> myifft = ifft2(data) >>> myifft() # Execute the inverse FFT >>> myifft(other_data) # Execute the inverse FFT with other data of the same shape and dtype
Usage#
The FFT functions in the sarcomp.fft
module are designed for high-performance computations. Below are examples of how to use these functions for 1D and 2D FFTs.
1D FFT Example#
import sarcomp as sc
# Example data (power of 2)
data = sc.memory.empty(dtype="complex64", shape=(1024,)) # just an example
# Perform 1D forward FFT
result = sc.fft.fft(data)
# Perform 1D backward FFT
result = sc.fft.ifft(data)
print("1D FFT Result:", result.data)
2D FFT Example#
import sarcomp as sc
# Example 2D data
data = sc.memory.empty(dtype="complex64", shape(256, 1024))
# Perform 2D forward range FFT
result = sc.fft.fft(data, axis=1)
# Perform 2D forward azimuth FFT
result = sc.fft.fft(data, axis=0)
# Perform 2D forward FFT
result = sc.fft.fft2(data)
print("2D FFT Result:\n", result.data)