gonum: gonum.org/v1/gonum/fourier Index | Examples | Files | Directories

package fourier

import "gonum.org/v1/gonum/fourier"

Package fourier provides functions to perform Discrete Fourier Transforms.

Code:

// 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), floats.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⎦

Code:

// 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, floats.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

Package Files

doc.go fourier.go quarter.go sincos.go

type CmplxFFT Uses

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

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

func NewCmplxFFT Uses

func NewCmplxFFT(n int) *CmplxFFT

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

func (*CmplxFFT) Coefficients Uses

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.

Code:

// Period is a set of samples over a given period.
period := []complex128{1, 0, 2, 0, 4, 0, 2, 0}

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

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), cmplx.Abs(coeff[i]), cmplx.Phase(coeff[i]))
}

Output:

freq=-0.5 cycles/period, magnitude=9, phase=0
freq=-0.375 cycles/period, magnitude=3, phase=3.142
freq=-0.25 cycles/period, magnitude=1, phase=0
freq=-0.125 cycles/period, magnitude=3, phase=3.142
freq=0 cycles/period, magnitude=9, phase=0
freq=0.125 cycles/period, magnitude=3, phase=3.142
freq=0.25 cycles/period, magnitude=1, phase=0
freq=0.375 cycles/period, magnitude=3, phase=3.142

func (*CmplxFFT) Freq Uses

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 Uses

func (t *CmplxFFT) Len() int

Len returns the length of the acceptable input.

func (*CmplxFFT) Reset Uses

func (t *CmplxFFT) Reset(n int)

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

func (*CmplxFFT) Sequence Uses

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

Sequence computes the complex perodic 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 Uses

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 Uses

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 Uses

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

DCT implements Discrete Cosine Transform for real sequences.

func NewDCT Uses

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 Uses

func (t *DCT) Len() int

Len returns the length of the acceptable input.

func (*DCT) Reset Uses

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 Uses

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 Uses

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

DST implements Discrete Sine Transform for real sequences.

func NewDST Uses

func NewDST(n int) *DST

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

func (*DST) Len Uses

func (t *DST) Len() int

Len returns the length of the acceptable input.

func (*DST) Reset Uses

func (t *DST) Reset(n int)

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

func (*DST) Transform Uses

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 Uses

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

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

func NewFFT Uses

func NewFFT(n int) *FFT

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

func (*FFT) Coefficients Uses

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.

Code:

// Period is a set of samples over a given period.
period := []float64{1, 0, 2, 0, 4, 0, 2, 0}

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

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

Output:

freq=0 cycles/period, magnitude=9, phase=0
freq=0.125 cycles/period, magnitude=3, phase=3.142
freq=0.25 cycles/period, magnitude=1, phase=-0
freq=0.375 cycles/period, magnitude=3, phase=3.142
freq=0.5 cycles/period, magnitude=9, phase=0

Code:

// 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=%v, mean=%v\n", maxFreq, magnitude, mean/samples)

Output:

freq=262 Hz, magnitude=17296.195519181776, mean=2.783457755654771

func (*FFT) Freq Uses

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 Uses

func (t *FFT) Len() int

Len returns the length of the acceptable input.

func (*FFT) Reset Uses

func (t *FFT) Reset(n int)

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

func (*FFT) Sequence Uses

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

Sequence computes the real perodic 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 Uses

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

QuarterWaveFFT implements Fast Fourier Transform for quarter wave data.

func NewQuarterWaveFFT Uses

func NewQuarterWaveFFT(n int) *QuarterWaveFFT

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

func (*QuarterWaveFFT) CosCoefficients Uses

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 Uses

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 Uses

func (t *QuarterWaveFFT) Len() int

Len returns the length of the acceptable input.

func (*QuarterWaveFFT) Reset Uses

func (t *QuarterWaveFFT) Reset(n int)

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

func (*QuarterWaveFFT) SinCoefficients Uses

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 Uses

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

PathSynopsis
internal/fftpackPackage fftpack implements Discrete Fourier Transform functions ported from the Fortran implementation of FFTPACK.

Package fourier imports 1 packages (graph) and is imported by 5 packages. Updated 2019-04-02. Refresh now. Tools for package owners.