fourier

package
v0.15.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 16, 2024 License: BSD-3-Clause Imports: 4 Imported by: 35

Documentation

Overview

Package fourier provides functions to perform Discrete Fourier Transforms.

Example (CmplxFFT2)
package main

import (
	"fmt"
	"math/cmplx"

	"gonum.org/v1/gonum/dsp/fourier"
	"gonum.org/v1/gonum/floats/scalar"
	"gonum.org/v1/gonum/mat"
)

func main() {
	// Image is a set of diagonal lines.
	image := mat.NewDense(11, 11, []float64{
		0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
		0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
		1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
		0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
		0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
		1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
		0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
		0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
		1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
		0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
		0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
	})

	// Make appropriately sized complex FFT.
	// Rows and columns are the same, so the same
	// CmplxFFT can be used for both axes.
	r, c := image.Dims()
	cfft := fourier.NewCmplxFFT(r)

	// Perform the first axis transform.
	rows := make([]complex128, r*c)
	for i := 0; i < r; i++ {
		row := rows[c*i : c*(i+1)]
		for j, v := range image.RawRowView(i) {
			row[j] = complex(v, 0)
		}
		cfft.Coefficients(row, row)
	}

	// Perform the second axis transform, storing
	// the result in freqs.
	freqs := mat.NewDense(c, c, nil)
	column := make([]complex128, r)
	for j := 0; j < c; j++ {
		for i := 0; i < r; i++ {
			column[i] = rows[i*c+j]
		}
		cfft.Coefficients(column, column)
		for i, v := range column {
			// Center the frequencies.
			freqs.Set(cfft.UnshiftIdx(i), cfft.UnshiftIdx(j), scalar.Round(cmplx.Abs(v), 1))
		}
	}

	fmt.Printf("%v\n", mat.Formatted(freqs))

}
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)
package main

import (
	"fmt"
	"math/cmplx"

	"gonum.org/v1/gonum/dsp/fourier"
	"gonum.org/v1/gonum/floats/scalar"
	"gonum.org/v1/gonum/mat"
)

func main() {
	// This example shows how to perform a 2D fourier transform
	// on an image. The transform identifies the lines present
	// in the image.

	// Image is a set of diagonal lines.
	image := mat.NewDense(11, 11, []float64{
		0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
		0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
		1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
		0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
		0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
		1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
		0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
		0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
		1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0,
		0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
		0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1,
	})

	// Make appropriately sized real and complex FFT types.
	r, c := image.Dims()
	fft := fourier.NewFFT(c)
	cfft := fourier.NewCmplxFFT(r)

	// Only c/2+1 coefficients will be returned for
	// the real FFT.
	c = c/2 + 1

	// Perform the first axis transform.
	rows := make([]complex128, r*c)
	for i := 0; i < r; i++ {
		fft.Coefficients(rows[c*i:c*(i+1)], image.RawRowView(i))
	}

	// Perform the second axis transform, storing
	// the result in freqs.
	freqs := mat.NewDense(c, c, nil)
	column := make([]complex128, r)
	for j := 0; j < c; j++ {
		for i := 0; i < r; i++ {
			column[i] = rows[i*c+j]
		}
		cfft.Coefficients(column, column)
		for i, v := range column[:c] {
			freqs.Set(i, j, scalar.Round(cmplx.Abs(v), 1))
		}
	}

	fmt.Printf("%v\n", mat.Formatted(freqs))

}
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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CoefficientsRadix2 added in v0.9.0

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 added in v0.9.0

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 added in v0.9.0

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 added in v0.9.0

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 added in v0.9.0

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 added in v0.9.0

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 added in v0.9.0

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 added in v0.9.0

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

func NewCmplxFFT(n int) *CmplxFFT

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
package main

import (
	"fmt"
	"math/cmplx"

	"gonum.org/v1/gonum/dsp/fourier"
)

func main() {
	// Samples is a set of samples over a given period.
	samples := []complex128{1, 0, 2, 0, 4, 0, 2, 0}
	period := float64(len(samples))

	// Initialize a complex FFT and perform the analysis.
	fft := fourier.NewCmplxFFT(len(samples))
	coeff := fft.Coefficients(nil, samples)

	for i := range coeff {
		// Center the spectrum.
		i = fft.ShiftIdx(i)

		fmt.Printf("freq=%v cycles/period, magnitude=%v, phase=%.4g\n",
			fft.Freq(i)*period, cmplx.Abs(coeff[i]), cmplx.Phase(coeff[i]))
	}

}
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

func (t *CmplxFFT) Freq(i int) float64

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) Len

func (t *CmplxFFT) Len() int

Len returns the length of the acceptable input.

func (*CmplxFFT) Reset

func (t *CmplxFFT) Reset(n int)

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

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

func (t *CmplxFFT) ShiftIdx(i int) int

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

func (t *CmplxFFT) UnshiftIdx(i int) int

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

func NewDCT(n int) *DCT

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

func (*DCT) Len

func (t *DCT) Len() int

Len returns the length of the acceptable input.

func (*DCT) Reset

func (t *DCT) Reset(n int)

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

func (*DCT) Transform

func (t *DCT) Transform(dst, src []float64) []float64

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 NewDST

func NewDST(n int) *DST

NewDST returns a DST initialized for work on sequences of length n.

func (*DST) Len

func (t *DST) Len() int

Len returns the length of the acceptable input.

func (*DST) Reset

func (t *DST) Reset(n int)

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

func (*DST) Transform

func (t *DST) Transform(dst, src []float64) []float64

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 NewFFT

func NewFFT(n int) *FFT

NewFFT returns an FFT initialized for work on sequences of length n.

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
package main

import (
	"fmt"
	"math/cmplx"

	"gonum.org/v1/gonum/dsp/fourier"
)

func main() {
	// Samples is a set of samples over a given period.
	samples := []float64{1, 0, 2, 0, 4, 0, 2, 0}
	period := float64(len(samples))

	// Initialize an FFT and perform the analysis.
	fft := fourier.NewFFT(len(samples))
	coeff := fft.Coefficients(nil, samples)

	for i, c := range coeff {
		fmt.Printf("freq=%v cycles/period, magnitude=%v, phase=%.4g\n",
			fft.Freq(i)*period, cmplx.Abs(c), cmplx.Phase(c))
	}

}
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
Example (Tone)
package main

import (
	"fmt"
	"math"
	"math/cmplx"

	"gonum.org/v1/gonum/dsp/fourier"
)

func main() {
	// Tone is a set of samples over a second of a pure Middle C.
	const (
		mC      = 261.625565 // Hz
		samples = 44100
	)
	tone := make([]float64, samples)
	for i := range tone {
		tone[i] = math.Sin(mC * 2 * math.Pi * float64(i) / samples)
	}

	// Initialize an FFT and perform the analysis.
	fft := fourier.NewFFT(samples)
	coeff := fft.Coefficients(nil, tone)

	var maxFreq, magnitude, mean float64
	for i, c := range coeff {
		m := cmplx.Abs(c)
		mean += m
		if m > magnitude {
			magnitude = m
			maxFreq = fft.Freq(i) * samples
		}
	}
	fmt.Printf("freq=%v Hz, magnitude=%.0f, mean=%.4f\n", maxFreq, magnitude, mean/samples)

}
Output:


freq=262 Hz, magnitude=17296, mean=2.7835

func (*FFT) Freq

func (t *FFT) Freq(i int) float64

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) Len

func (t *FFT) Len() int

Len returns the length of the acceptable input.

func (*FFT) Reset

func (t *FFT) Reset(n int)

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

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.

Directories

Path Synopsis
internal
fftpack
Package fftpack implements Discrete Fourier Transform functions ported from the Fortran implementation of FFTPACK.
Package fftpack implements Discrete Fourier Transform functions ported from the Fortran implementation of FFTPACK.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL