## Documentation ¶

### Overview ¶

Package fourier provides functions to perform Discrete Fourier Transforms.

## Example (CmplxFFT2) ¶

Output: ⎡ 1.6 6.8 3.8 1.7 1.2 1.1 1.1 1.4 2.6 3.9 1.1⎤ ⎢ 6.8 27.5 14.1 5.9 4 3.2 3 3 3.9 3.2 3.9⎥ ⎢ 3.8 14.1 6.8 2.8 1.8 1.4 1.2 1.1 1.4 3.9 2.6⎥ ⎢ 1.7 5.9 2.8 1.1 0.7 0.5 0.5 0.5 1.1 3 1.4⎥ ⎢ 1.2 4 1.8 0.7 0.5 0.4 0.4 0.5 1.2 3 1.1⎥ ⎢ 1.1 3.2 1.4 0.5 0.4 40 0.4 0.5 1.4 3.2 1.1⎥ ⎢ 1.1 3 1.2 0.5 0.4 0.4 0.5 0.7 1.8 4 1.2⎥ ⎢ 1.4 3 1.1 0.5 0.5 0.5 0.7 1.1 2.8 5.9 1.7⎥ ⎢ 2.6 3.9 1.4 1.1 1.2 1.4 1.8 2.8 6.8 14.1 3.8⎥ ⎢ 3.9 3.2 3.9 3 3 3.2 4 5.9 14.1 27.5 6.8⎥ ⎣ 1.1 3.9 2.6 1.4 1.1 1.1 1.2 1.7 3.8 6.8 1.6⎦

## Example (FFT2) ¶

Output: ⎡ 40 0.4 0.5 1.4 3.2 1.1⎤ ⎢ 0.4 0.5 0.7 1.8 4 1.2⎥ ⎢ 0.5 0.7 1.1 2.8 5.9 1.7⎥ ⎢ 1.4 1.8 2.8 6.8 14.1 3.8⎥ ⎢ 3.2 4 5.9 14.1 27.5 6.8⎥ ⎣ 1.1 1.2 1.7 3.8 6.8 1.6⎦

### Index ¶

- func CoefficientsRadix2(seq []complex128) []complex128
- func CoefficientsRadix4(seq []complex128) []complex128
- func PadRadix2(x []complex128) []complex128
- func PadRadix4(x []complex128) []complex128
- func SequenceRadix2(coeff []complex128) []complex128
- func SequenceRadix4(coeff []complex128) []complex128
- func TrimRadix2(x []complex128) (even, remains []complex128)
- func TrimRadix4(x []complex128) (even, remains []complex128)
- type CmplxFFT
- func (t *CmplxFFT) Coefficients(dst, seq []complex128) []complex128
- func (t *CmplxFFT) Freq(i int) float64
- func (t *CmplxFFT) Len() int
- func (t *CmplxFFT) Reset(n int)
- func (t *CmplxFFT) Sequence(dst, coeff []complex128) []complex128
- func (t *CmplxFFT) ShiftIdx(i int) int
- func (t *CmplxFFT) UnshiftIdx(i int) int

- type DCT
- type DST
- type FFT
- type QuarterWaveFFT
- func (t *QuarterWaveFFT) CosCoefficients(dst, seq []float64) []float64
- func (t *QuarterWaveFFT) CosSequence(dst, coeff []float64) []float64
- func (t *QuarterWaveFFT) Len() int
- func (t *QuarterWaveFFT) Reset(n int)
- func (t *QuarterWaveFFT) SinCoefficients(dst, seq []float64) []float64
- func (t *QuarterWaveFFT) SinSequence(dst, coeff []float64) []float64

#### Examples ¶

### Constants ¶

### Variables ¶

### Functions ¶

#### func CoefficientsRadix2 ¶

func CoefficientsRadix2(seq []complex128) []complex128

CoefficientsRadix2 computes 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.

CoefficientsRadix2 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 FFT or CmplxFFT types should be used.

If the length of seq is not an integer power of 2, CoefficientsRadix2 will panic.

#### func CoefficientsRadix4 ¶

func CoefficientsRadix4(seq []complex128) []complex128

CoefficientsRadix4 computes 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 CoefficientsRadix4 followed by a call of SequenceRadix4 will multiply the input sequence by the length of the sequence.

CoefficientsRadix4 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 FFT or CmplxFFT types should be used.

If the length of seq is not an integer power of 4, CoefficientsRadix4 will panic.

#### func PadRadix2 ¶

func PadRadix2(x []complex128) []complex128

PadRadix2 returns 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.

#### func PadRadix4 ¶

func PadRadix4(x []complex128) []complex128

PadRadix4 returns the values in x in a slice that is an integer power of 4 long. If x already has an integer power of 4 length it is returned unaltered.

#### func SequenceRadix2 ¶

func SequenceRadix2(coeff []complex128) []complex128

SequenceRadix2 computes 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.

SequenceRadix2 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 FFT or CmplxFFT types should be used.

If the length of coeff is not an integer power of 2, SequenceRadix2 will panic.

#### func SequenceRadix4 ¶

func SequenceRadix4(coeff []complex128) []complex128

SequenceRadix4 computes 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 CoefficientsRadix4 followed by a call of SequenceRadix4 will multiply the input sequence by the length of the sequence.

SequenceRadix4 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 FFT or CmplxFFT types should be used.

If the length of coeff is not an integer power of 4, SequenceRadix4 will panic.

#### func TrimRadix2 ¶

func TrimRadix2(x []complex128) (even, remains []complex128)

TrimRadix2 returns the largest slice of x that is has an integer power of 2 length, and a slice holding the remaining elements.

#### func TrimRadix4 ¶

func TrimRadix4(x []complex128) (even, remains []complex128)

TrimRadix4 returns the largest slice of x that is has an integer power of 4 length, and a slice holding the remaining elements.

### Types ¶

#### type CmplxFFT ¶

```
type CmplxFFT struct {
// contains filtered or unexported fields
}
```

CmplxFFT implements Fast Fourier Transform and its inverse for complex sequences.

#### func NewCmplxFFT ¶

NewCmplxFFT returns an CmplxFFT initialized for work on sequences of length n.

#### func (*CmplxFFT) Coefficients ¶

func (t *CmplxFFT) Coefficients(dst, seq []complex128) []complex128

Coefficients computes 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 t.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.

## Example ¶

Output: freq=-4 cycles/period, magnitude=9, phase=0 freq=-3 cycles/period, magnitude=3, phase=3.142 freq=-2 cycles/period, magnitude=1, phase=0 freq=-1 cycles/period, magnitude=3, phase=3.142 freq=0 cycles/period, magnitude=9, phase=0 freq=1 cycles/period, magnitude=3, phase=3.142 freq=2 cycles/period, magnitude=1, phase=0 freq=3 cycles/period, magnitude=3, phase=3.142

#### func (*CmplxFFT) Freq ¶

Freq returns the relative frequency center for coefficient i. Freq will panic if i is negative or greater than or equal to t.Len().

#### func (*CmplxFFT) Sequence ¶

func (t *CmplxFFT) Sequence(dst, coeff []complex128) []complex128

Sequence computes 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 t.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.

#### func (*CmplxFFT) ShiftIdx ¶

ShiftIdx returns a shifted index into a slice of coefficients returned by the CmplxFFT 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 t.Len().

#### func (*CmplxFFT) UnshiftIdx ¶

UnshiftIdx returns inverse of ShiftIdx. UnshiftIdx will panic if i is negative or greater than or equal to t.Len().

#### type DCT ¶

```
type DCT struct {
// contains filtered or unexported fields
}
```

DCT implements Discrete Cosine Transform for real sequences.

#### func NewDCT ¶

NewDCT returns a DCT initialized for work on sequences of length n. NewDCT will panic is n is not greater than 1.

#### func (*DCT) Reset ¶

Reset reinitializes the DCT for work on sequences of length n. Reset will panic is n is not greater than 1.

#### func (*DCT) Transform ¶

Transform computes the Discrete Fourier Cosine Transform of the input data, src, placing the result in dst and returning it. This transform is unnormalized; a call to Transform followed by another call to Transform will multiply the input sequence by 2*(n-1), where n is the length of the sequence.

If the length of src is not t.Len(), Transform 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 t.Len(), FFT will panic. It is safe to use the same slice for dst and src.

#### type DST ¶

```
type DST struct {
// contains filtered or unexported fields
}
```

DST implements Discrete Sine Transform for real sequences.

#### func (*DST) Transform ¶

Transform computes the Discrete Fourier Sine Transform of the input data, src, placing the result in dst and returning it. This transform is unnormalized; a call to Transform followed by another call to Transform will multiply the input sequence by 2*(n-1), where n is the length of the sequence.

If the length of src is not t.Len(), Transform 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 t.Len(), FFT will panic. It is safe to use the same slice for dst and src.

#### type FFT ¶

```
type FFT struct {
// contains filtered or unexported fields
}
```

FFT implements Fast Fourier Transform and its inverse for real sequences.

#### func (*FFT) Coefficients ¶

func (t *FFT) Coefficients(dst []complex128, seq []float64) []complex128

Coefficients computes 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 t.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 t.Len()/2+1, Coefficients will panic.

## Example ¶

Output: freq=0 cycles/period, magnitude=9, phase=0 freq=1 cycles/period, magnitude=3, phase=3.142 freq=2 cycles/period, magnitude=1, phase=-0 freq=3 cycles/period, magnitude=3, phase=3.142 freq=4 cycles/period, magnitude=9, phase=0

#### func (*FFT) Freq ¶

Freq returns the relative frequency center for coefficient i. Freq will panic if i is negative or greater than or equal to t.Len().

#### func (*FFT) Sequence ¶

func (t *FFT) Sequence(dst []float64, coeff []complex128) []float64

Sequence computes 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 t.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.

#### type QuarterWaveFFT ¶

```
type QuarterWaveFFT struct {
// contains filtered or unexported fields
}
```

QuarterWaveFFT implements Fast Fourier Transform for quarter wave data.

#### func NewQuarterWaveFFT ¶

func NewQuarterWaveFFT(n int) *QuarterWaveFFT

NewQuarterWaveFFT returns a QuarterWaveFFT initialized for work on sequences of length n.

#### func (*QuarterWaveFFT) CosCoefficients ¶

func (t *QuarterWaveFFT) CosCoefficients(dst, seq []float64) []float64

CosCoefficients computes the Fast Fourier Transform of quarter wave data for the input sequence, seq, placing the cosine series coefficients in dst and returning it. This transform is unnormalized; a call to CosCoefficients followed by a call to CosSequence will multiply the input sequence by 4*n, where n is the length of the sequence.

If the length of seq is not t.Len(), CosCoefficients 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 t.Len(), CosCoefficients will panic. It is safe to use the same slice for dst and seq.

#### func (*QuarterWaveFFT) CosSequence ¶

func (t *QuarterWaveFFT) CosSequence(dst, coeff []float64) []float64

CosSequence computes the Inverse Fast Fourier Transform of quarter wave data for the input cosine series coefficients, coeff, placing the sequence data in dst and returning it. This transform is unnormalized; a call to CosSequence followed by a call to CosCoefficients will multiply the input sequence by 4*n, where n is the length of the sequence.

If the length of seq is not t.Len(), CosSequence 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 t.Len(), CosSequence will panic. It is safe to use the same slice for dst and seq.

#### func (*QuarterWaveFFT) Len ¶

func (t *QuarterWaveFFT) Len() int

Len returns the length of the acceptable input.

#### func (*QuarterWaveFFT) Reset ¶

func (t *QuarterWaveFFT) Reset(n int)

Reset reinitializes the QuarterWaveFFT for work on sequences of length n.

#### func (*QuarterWaveFFT) SinCoefficients ¶

func (t *QuarterWaveFFT) SinCoefficients(dst, seq []float64) []float64

SinCoefficients computes the Fast Fourier Transform of quarter wave data for the input sequence, seq, placing the sine series coefficients in dst and returning it. This transform is unnormalized; a call to SinCoefficients followed by a call to SinSequence will multiply the input sequence by 4*n, where n is the length of the sequence.

If the length of seq is not t.Len(), SinCoefficients 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 t.Len(), SinCoefficients will panic. It is safe to use the same slice for dst and seq.

#### func (*QuarterWaveFFT) SinSequence ¶

func (t *QuarterWaveFFT) SinSequence(dst, coeff []float64) []float64

SinSequence computes the Inverse Fast Fourier Transform of quarter wave data for the input sine series coefficients, coeff, placing the sequence data in dst and returning it. This transform is unnormalized; a call to SinSequence followed by a call to SinCoefficients will multiply the input sequence by 4*n, where n is the length of the sequence.

If the length of seq is not t.Len(), SinSequence 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 t.Len(), SinSequence will panic. It is safe to use the same slice for dst and seq.