fourier

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: MIT Imports: 3 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package fourier computes forward and inverse discrete Fourier transforms over complex samples. Use equal power-of-two lengths for circular convolution with Convolve.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Convolve

func Convolve(x, y []complex128) []complex128

Convolve returns the convolution of x ∗ y, applied to the complex128 slice x.

func FFT

func FFT(src []complex128) []complex128

FFT returns the frequency spectrum of src without modifying src. Use a power-of-two input length. Calls may run concurrently.

func IFFT

func IFFT(value []complex128) []complex128

IFFT returns the Inverse Fast Fourier Transform of a given complex128 slice.

func Log2

func Log2[T Log2Types](v T) T

Log2 returns the log base 2 of v, matching the input to precomputed values to achieve the fastest performance possible.

If the input is not present, then its log2 value is computed with a fast bit-shifting technique.

from: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious

func ReorderData

func ReorderData(value []complex128) []complex128

ReorderData shuffles the input complex128 values to re-order it, setting the first half of the slice in even indexes and the last half of the slice in odd indexes.

func ReverseBits

func ReverseBits[T RevBitsTypes](value T) (reverse T)

ReverseBits returns the bits of `value` in reverse order from: http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious

func ReverseFirstBits

func ReverseFirstBits[T RevBitsTypes](value, size T) (reverse T)

ReverseFirstBits returns the first `size` bits of `value` in reverse order from: http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious

func ToComplex

func ToComplex(x []float64) []complex128

ToComplex converts a slice of float64 into a slice of complex128, where the input slice's values are set as the real part of the complex numbers

In Go, a complex number is a pair of two float numbers. This means that converting a float number to a complex number only makes it necessary to assign the (float's) value as the real part of the complex number, leaving the imaginary as zero.

This particular function leverages that by creating a float slice of twice the size as the inputs, and placing each element of the input on every second slot. Finally, it casts this float slice as a complex slice, using an unsafe approach to prioritize performance.

Types

type Log2Types

type Log2Types interface {
	uint | uint16 | uint32 | uint64 | int | int16 | int32 | int64
}

Log2Types is a type constraint interface to scope the allowed types to call the Log2 function.

Example
// A 1024-frame FFT uses ten radix-2 stages.
if stages := Log2(uint32(1024)); stages != 10 {
	panic("expected ten radix-2 stages")
}

type RevBitsTypes

type RevBitsTypes interface {
	uint | uint16 | uint32 | uint64 | int | int16 | int32 | int64
}

RevBitsTypes is a type constraint interface to scope the allowed types to call the ReverseBits function.

Example
// Reverse the four bits of an index in a 16-slot FFT.
if index := ReverseFirstBits(uint16(0b0011), 4); index != 0b1100 {
	panic("expected the reversed index")
}

Jump to

Keyboard shortcuts

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