rai_toolbox.augmentations.fourier.generate_fourier_bases#
- rai_toolbox.augmentations.fourier.generate_fourier_bases(nrows, ncols, dtype=<class 'numpy.float32'>, *, factor_2pi_phase_shift=0, row_col_coords=None)[source]#
Yields each unique, real-valued 2D array with unit norm, such that its Fourier transform is supported at each (i, j) and its symmetric position on a shape-(nrows, ncols) grid in Fourier space, where the lowest frequency component resides at the center of the grid.
- Parameters:
- nrowsint
- ncolsint
- dtypeDTypeLike
- factor_2pi_phase_shiftfloat (optional, default=0)
E.g., specifying
0.5
will induce a phase-shift of \(\pi\) on all bases.- row_col_coordsOptional[Iterable[Tuple[int, int]]]
If provided, specifies the specific Fourier-bases to generate in terms of their Fourier-space locations. Otherwise, all non-redundant entries on the nrows x ncols grip will be generated.
- Yields:
- fourier_basis: FourierBasis
Yields the array’s position in Fourier space, (k_row, k_col), its symmetric position, and the shape-(nrows, ncols) real-valued basis array itself.
Notes
The iteration is performed in row-major order in 2D Fourier space, starting with the position (0, 0).
Examples
Here we will visualize the collection of bases in Fourier space that support the
>>> import numpy as np >>> from rai_toolbox.augmentations.fourier import generate_fourier_bases >>> [np.round(out.basis, 2) for out in generate_fourier_bases(3, 3)] [array([[ 0.33, 0.12, -0.46], [ 0.12, -0.46, 0.33], [-0.46, 0.33, 0.12]]), array([[ 0.33, 0.33, 0.33], [ 0.12, 0.12, 0.12], [-0.46, -0.46, -0.46]]), array([[ 0.33, -0.46, 0.12], [ 0.12, 0.33, -0.46], [-0.46, 0.12, 0.33]]), array([[ 0.33, 0.12, -0.46], [ 0.33, 0.12, -0.46], [ 0.33, 0.12, -0.46]]), array([[0.33, 0.33, 0.33], [0.33, 0.33, 0.33], [0.33, 0.33, 0.33]])]
We’ll now inspect the corresponding support arrays in Fourier space. Note that only five basis arrays, not nine, are returned in association with the specified 3x3 grid. This is because the remaining four bases are redundant due to the symmetries in the support space.
>>> f = lambda x: np.round(np.abs(np.fft.fftshift(np.fft.fft2(x)))) >>> [f(out.basis) for out in generate_fourier_bases(3, 3)] [array([[2., 0., 0.], [0., 0., 0.], [0., 0., 2.]]), array([[0., 2., 0.], [0., 0., 0.], [0., 2., 0.]]), array([[0., 0., 2.], [0., 0., 0.], [2., 0., 0.]]), array([[0., 0., 0.], [2., 0., 2.], [0., 0., 0.]]), array([[0., 0., 0.], [0., 3., 0.], [0., 0., 0.]])]