Skip to main content

fft

Function fft 

Source
pub fn fft(
    samples: &NonEmptySlice<f64>,
    n_fft: NonZeroUsize,
) -> SpectrogramResult<Array1<Complex<f64>>>
Expand description

Compute the real-to-complex FFT of a real-valued signal.

This function performs a forward FFT on real-valued input, returning the complex frequency domain representation. Only the positive frequencies are returned (length = n_fft/2 + 1) due to conjugate symmetry.

§Arguments

  • samples - Input signal (length ≤ n_fft, will be zero-padded if shorter)
  • n_fft - FFT size

§Returns

A vector of complex frequency bins with length n_fft/2 + 1.

§Automatic Zero-Padding

If the input signal is shorter than n_fft, it will be automatically zero-padded to the required length. This is standard DSP practice and preserves frequency resolution (bin spacing = sample_rate / n_fft).

use spectrograms::{fft, nzu};
use non_empty_slice::non_empty_vec;

let signal = non_empty_vec![1.0, 2.0, 3.0]; // Only 3 samples
let spectrum = fft(&signal, nzu!(8))?;   // Automatically padded to 8
assert_eq!(spectrum.len(), 5);     // Output: 8/2 + 1 = 5 bins

§Errors

Returns InvalidInput error if the input length exceeds n_fft.

§Examples

use spectrograms::*;
use non_empty_slice::non_empty_vec;

let signal = non_empty_vec![0.0; nzu!(512)];
let spectrum = fft(&signal, nzu!(512))?;

assert_eq!(spectrum.len(), 257); // 512/2 + 1