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 ¶
- func Convolve(x, y []complex128) []complex128
- func FFT(src []complex128) []complex128
- func IFFT(value []complex128) []complex128
- func Log2[T Log2Types](v T) T
- func ReorderData(value []complex128) []complex128
- func ReverseBits[T RevBitsTypes](value T) (reverse T)
- func ReverseFirstBits[T RevBitsTypes](value, size T) (reverse T)
- func ToComplex(x []float64) []complex128
- type Log2Types
- type RevBitsTypes
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 ¶
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 ¶
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")
}