goft

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2021 License: MIT Imports: 4 Imported by: 1

README

goft

v0.1.0 Test coverage Go Report Card
Go Reference license

Fourier Transformation support for Go language

Install

$ go get github.com/yut-kt/goft

Usage

import (
    "fmt"
    "math"
	
    "github.com/yut-kt/goft"
)

func ExampleDFT() {
    testSamples := []float64{0, 1, 0, -1}

    // DFT Any Numeric Slice
    f, err := goft.DFT(testSamples)
    if err != nil {
        panic(err)
    }
}

See goft_test.go for detailed Usage

License

goft is released under the MIT License.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Amplitude

func Amplitude(c []complex128) []float64

Amplitude is a function mainly to calc calculate the amplitude of the FFT result.

Example
package main

import (
	"fmt"

	"github.com/yut-kt/goft"
)

func main() {
	testSamples := []float64{0, 1, 0, -1}

	// DFT
	f, err := goft.DFT(testSamples)
	if err != nil {
		panic(err)
	}

	fmt.Println(goft.Amplitude(f))
}
Output:

[0 2 2.449293598294703e-16 2]

func DFT

func DFT(f interface{}) ([]complex128, error)

DFT is a function to Discrete Fourier Transform.

Example
package main

import (
	"fmt"

	"github.com/yut-kt/goft"
)

func main() {
	testSamples := []float64{0, 1, 0, -1}

	// DFT
	f, err := goft.DFT(testSamples)
	if err != nil {
		panic(err)
	}
	fmt.Println(f)
}
Output:

[(0+0i) (2.449293598294703e-16-2i) (0+2.449293598294703e-16i) (-7.347880794884109e-16+2i)]

func FFT

func FFT(f interface{}) ([]complex128, error)

FFT is a function to Fast Fourier Transformation.

Example
package main

import (
	"fmt"

	"github.com/yut-kt/goft"
)

func main() {
	testSamples := []float64{0, 1, 0, -1}

	// FFT
	f, err := goft.FFT(testSamples)
	if err != nil {
		panic(err)
	}
	fmt.Println(f)
}
Output:

[(0+0i) (2.449293598294703e-16-2i) (0+0i) (-4.898587196589406e-16+2i)]

func IDFT

func IDFT(F interface{}) ([]complex128, error)

IDFT is a function to Inverse Discrete Fourier Transform.

Example
package main

import (
	"fmt"
	"math"

	"github.com/yut-kt/goft"
)

const EPSILON float64 = 0.00000001

func main() {
	testSamples := []float64{0, 1, 0, -1}

	// DFT
	f, err := goft.DFT(testSamples)
	if err != nil {
		panic(err)
	}

	// IDFT
	F, err := goft.IDFT(f)
	if err != nil {
		panic(err)
	}
	fmt.Println(F)

	// float64 approximate check
	isEqual := true
	for i, v := range F {
		isEqual = isEqual && math.Abs(testSamples[i]-real(v)) < EPSILON
		isEqual = isEqual && math.Abs(imag(v)) < EPSILON
	}
	fmt.Println(isEqual)
}
Output:

[(-1.2246467991473515e-16+5.551115123125783e-17i) (1+6.123233995736757e-17i) (1.4997597826618535e-32+1.1102230246251565e-16i) (-1+6.123233995736757e-17i)]
true

func IFFT

func IFFT(F interface{}) ([]complex128, error)

IFFT is a function to Inverse Fast Fourier Transform.

Example
package main

import (
	"fmt"
	"math"

	"github.com/yut-kt/goft"
)

const EPSILON float64 = 0.00000001

func main() {
	testSamples := []float64{0, 1, 0, -1}

	// FFT
	f, err := goft.FFT(testSamples)
	if err != nil {
		panic(err)
	}

	// IFFT
	F, err := goft.IFFT(f)
	if err != nil {
		panic(err)
	}
	fmt.Println(F)

	// float64 approximate check
	isEqual := true
	for i, v := range F {
		isEqual = isEqual && math.Abs(testSamples[i]-real(v)) < EPSILON
		isEqual = isEqual && math.Abs(imag(v)) < EPSILON
	}
	fmt.Println(isEqual)
}
Output:

[(-6.123233995736757e-17+0i) (1+6.123233995736757e-17i) (6.123233995736757e-17-7.498798913309268e-33i) (-1+6.123233995736757e-17i)]
true

Types

type Type

type Type int
const (
	CaseFFT Type = iota
	CaseIFFT
)

Jump to

Keyboard shortcuts

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