rfftn

Function rfftn 

Source
pub fn rfftn<T>(
    x: &ArrayView<'_, T, IxDyn>,
    shape: Option<Vec<usize>>,
    axes: Option<Vec<usize>>,
    norm: Option<&str>,
    overwrite_x: Option<bool>,
    workers: Option<usize>,
) -> FFTResult<Array<Complex64, IxDyn>>
where T: NumCast + Copy + Debug + 'static,
Expand description

Compute the N-dimensional discrete Fourier Transform for real input.

§Arguments

  • x - Input real-valued array
  • shape - Shape of the transformed array (optional)
  • axes - Axes over which to compute the RFFT (optional, defaults to all axes)

§Returns

  • The N-dimensional Fourier transform of the real input array

§Examples

// Example will be expanded when the function is implemented

Compute the N-dimensional discrete Fourier Transform for real input.

This function computes the N-D discrete Fourier Transform over any number of axes in an M-D real array by means of the Fast Fourier Transform (FFT). By default, all axes are transformed, with the real transform performed over the last axis, while the remaining transforms are complex.

§Arguments

  • x - Input array, taken to be real
  • shape - Shape (length of each transformed axis) of the output (optional). If given, the input is either padded or cropped to the specified shape.
  • axes - Axes over which to compute the FFT (optional, defaults to all axes). If not given, the last len(s) axes are used, or all axes if s is also not specified.
  • norm - Normalization mode (optional, default is “backward”):
    • “backward”: No normalization on forward transforms, 1/n on inverse
    • “forward”: 1/n on forward transforms, no normalization on inverse
    • “ortho”: 1/sqrt(n) on both forward and inverse transforms
  • overwrite_x - If true, the contents of x can be destroyed (default: false)
  • workers - Maximum number of workers to use for parallel computation (optional). If provided and > 1, the computation will try to use multiple cores.

§Returns

  • The N-dimensional Fourier transform of the real input array. The length of the transformed axis is s[-1]//2+1, while the remaining transformed axes have lengths according to s, or unchanged from the input.

§Examples

use scirs2_fft::rfftn;
use scirs2_core::ndarray::Array3;
use scirs2_core::ndarray::IxDyn;

// Create a 3D array with real values
let mut data = vec![0.0; 3*4*5];
for i in 0..data.len() {
    data[i] = i as f64;
}

// Calculate the sum before moving data into the array
let total_sum: f64 = data.iter().sum();

let arr = Array3::from_shape_vec((3, 4, 5), data).unwrap();

// Convert to dynamic view for N-dimensional functions
let dynamic_view = arr.view().into_dyn();

// Compute 3D RFFT with all parameters
// None for shape (default shape)
// None for axes (default axes)
// None for normalization mode (default "backward")
// None for overwrite_x (default false)
// None for workers (default 1 worker)
let spectrum = rfftn(&dynamic_view, None, None, None, None, None).unwrap();

// For real input with last dimension of length 5, the output shape will be (3, 4, 3)
// where 3 = 5//2 + 1
assert_eq!(spectrum.shape(), &[3, 4, 3]);

// Verify DC component (sum of all elements that we calculated earlier)
assert!((spectrum[IxDyn(&[0, 0, 0])].re - total_sum).abs() < 1e-10);

// Note: This example is marked as no_run to avoid complex number conversion issues
// that occur during doctest execution but not in normal usage.

§Notes

When the DFT is computed for purely real input, the output is Hermitian-symmetric, i.e., the negative frequency terms are just the complex conjugates of the corresponding positive-frequency terms, and the negative-frequency terms are therefore redundant. The real-to-complex transform exploits this symmetry by only computing the positive frequency components along the transformed axes, saving both computation time and memory.

For transforms along the last axis, the length of the transformed axis is n//2 + 1, where n is the original length of that axis. For the remaining axes, the output shape is unchanged.

§Performance

For large arrays or specific performance needs, setting the workers parameter to a value > 1 may provide better performance on multi-core systems.

§Errors

Returns an error if the FFT computation fails or if the input values cannot be properly processed.

§See Also

  • irfftn - The inverse of rfftn
  • rfft - The 1-D FFT of real input
  • fftn - The N-D FFT
  • rfft2 - The 2-D FFT of real input