julenum/dsp/fourier
Package for Discrete Fourier Transform (DFT).
Index
fn CoefficientsRadix2(mut seq: []cmplx128, invert: bool): []cmplx128
fn SequenceRadix2(mut coeff: []cmplx128): []cmplx128
fn PadRadix2(mut x: []cmplx128): []cmplx128
fn TrimRadix2(mut x: []cmplx128): (even: []cmplx128, remains: []cmplx128)
struct FFT
fn New(n: int): &FFT
fn Len(*self): int
fn Reset(mut *self, n: int)
fn Coefficients(*self, mut dst: []cmplx128, seq: []cmplx128): []cmplx128
fn Sequence(*self, mut dst: []cmplx128, coeff: []cmplx128): []cmplx128
fn Freq(*self, i: int): f64
fn ShiftIdx(*self, i: int): int
fn UnshiftIdx(*self, i: int): int
struct RFFT
fn New(n: int): &RFFT
fn Len(*self): int
fn Reset(mut *self, n: int)
fn Coefficients(*self, mut dst: []cmplx128, seq: []f64): []cmplx128
fn Sequence(*self, mut dst: []f64, coeff: []cmplx128): []f64
fn Freq(*self, i: int): f64
CoefficientsRadix2
fn CoefficientsRadix2(mut seq: []cmplx128, invert: bool): []cmplx128Computes the Fourier coefficients of the input sequence, converting the time series in seq into the frequency spectrum, in place and returning it. This transform is unnormalized; a call to CoefficientsRadix2 followed by a call of SequenceRadix2 will multiply the input sequence by the length of the sequence.
The invert parameter controls the direction of the transform: passing false performs a forward Fast Fourier Transform (FFT) to convert time-series data into the frequency spectrum, while passing true performs an Inverse FFT (IFFT) to restore the frequency spectrum back to the original time domain. When invert is true, the function also normalizes the output by scaling all elements by 1/N (where N = len(x)) to guarantee that the original amplitudes are accurately recovered.
It does not allocate, requiring that FFT twiddle factors be calculated lazily. For performance reasons, this is done by successive multiplication, so numerical accuracies can accumulate for large inputs. If accuracy is needed, the RFFT or FFT types should be used.
If the length of seq is not an integer power of 2, it will panic.
SequenceRadix2
#disable boundary
fn SequenceRadix2(mut coeff: []cmplx128): []cmplx128Computes the real periodic sequence from the Fourier coefficients, converting the frequency spectrum in coeff into a time series, in place and returning it. This transform is unnormalized; a call to CoefficientsRadix2 followed by a call of SequenceRadix2 will multiply the input sequence by the length of the sequence.
It does not allocate, requiring that FFT twiddle factors be calculated lazily. For performance reasons, this is done by successive multiplication, so numerical accuracies can accumulate for large inputs. If accuracy is needed, the RFFT or FFT types should be used.
If the length of coeff is not an integer power of 2, it will panic.
PadRadix2
fn PadRadix2(mut x: []cmplx128): []cmplx128Returns the values in x in a slice that is an integer power of 2 long. If x already has an integer power of 2 length it is returned unaltered.
TrimRadix2
#disable boundary
fn TrimRadix2(mut x: []cmplx128): (even: []cmplx128, remains: []cmplx128)Returns the largest slice of x that is has an integer power of 2 length, and a slice holding the remaining elements.
FFT
struct FFT {
// NOTE: contains filtered hidden or unexported fields
}Implements Fast Fourier Transform and its inverse for complex sequences.
New
fn New(n: int): &FFTReturns an FFT initialized for work on sequences of length n.
Len
fn Len(*self): intReturns the length of the acceptable input.
Reset
#disable boundary
fn Reset(mut *self, n: int)Reinitializes the FFT for work on sequences of length n.
Coefficients
#disable boundary
fn Coefficients(*self, mut dst: []cmplx128, seq: []cmplx128): []cmplx128Computes the Fourier coefficients of a complex input sequence, converting the time series in seq into the frequency spectrum, placing the result in dst and returning it. This transform is unnormalized; a call to Coefficients followed by a call of Sequence will multiply the input sequence by the length of the sequence.
If the length of seq is not FFT.Len(), Coefficients will panic. If dst is nil, a new slice is allocated and returned. If dst is not nil and the length of dst does not equal the length of seq, Coefficients will panic. It is safe to use the same slice for dst and seq.
Sequence
#disable boundary
fn Sequence(*self, mut dst: []cmplx128, coeff: []cmplx128): []cmplx128Computes the complex periodic sequence from the Fourier coefficients, converting the frequency spectrum in coeff into a time series, placing the result in dst and returning it. This transform is unnormalized; a call to Coefficients followed by a call of Sequence will multiply the input sequence by the length of the sequence.
If the length of coeff is not FFT.Len(), Sequence will panic. If dst is nil, a new slice is allocated and returned. If dst is not nil and the length of dst does not equal the length of coeff, Sequence will panic. It is safe to use the same slice for dst and coeff.
Freq
fn Freq(*self, i: int): f64Returns the relative frequency center for coefficient i. Freq will panic if i is negative or greater than or equal to FFT.Len().
ShiftIdx
fn ShiftIdx(*self, i: int): intReturns a shifted index into a slice of coefficients returned by the FFT so that indexing into the coefficients places the zero frequency component at the center of the spectrum. ShiftIdx will panic if i is negative or greater than or equal to FFT.Len().
UnshiftIdx
fn UnshiftIdx(*self, i: int): intReturns inverse of ShiftIdx. UnshiftIdx will panic if i is negative or greater than or equal to FFT.Len().
RFFT
struct RFFT {
// NOTE: contains filtered hidden or unexported fields
}Fast Fourier Transform and its inverse for real sequences.
New
fn New(n: int): &RFFTReturns an RFFT initialized for work on sequences of length n.
Len
fn Len(*self): intReturns the length of the acceptable input.
Reset
#disable boundary
fn Reset(mut *self, n: int)Reinitializes the RFFT for work on sequences of length n.
Coefficients
#disable boundary
fn Coefficients(*self, mut dst: []cmplx128, seq: []f64): []cmplx128Computes the Fourier coefficients of the input sequence, converting the time series in seq into the frequency spectrum, placing the result in dst and returning it. This transform is unnormalized; a call to Coefficients followed by a call of Sequence will multiply the input sequence by the length of the sequence.
If the length of seq is not RFFT.Len(), Coefficients will panic. If dst is nil, a new slice is allocated and returned. If dst is not nil and the length of dst does not equal RFFT.Len()/2+1, Coefficients will panic.
Sequence
#disable boundary
fn Sequence(*self, mut dst: []f64, coeff: []cmplx128): []f64Computes the real periodic sequence from the Fourier coefficients, converting the frequency spectrum in coeff into a time series, placing the result in dst and returning it. This transform is unnormalized; a call to Coefficients followed by a call of Sequence will multiply the input sequence by the length of the sequence.
If the length of coeff is not RFFT.Len()/2+1, Sequence will panic. If dst is nil, a new slice is allocated and returned. If dst is not nil and the length of dst does not equal the length of coeff, Sequence will panic.
Freq
fn Freq(*self, i: int): f64Returns the relative frequency center for coefficient i. Freq will panic if i is negative or greater than or equal to RFFT.Len().