Fast Fourier Transform Implementation in Go
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Fast Fourier Transform runs in O(n log n) time and O(n) space, in Go as in every other language.
- ✓Go has complex64 and complex128 as builtin types with the complex, real and imag builtins, plus math/cmplx (Abs, Exp, Rect, Sqrt) - but no transform anywhere in the standard library.
- ✓Follow gonum's dst convention: accept a destination slice, allow nil, return it, so the caller owns allocation.
- ✓The Go trap: Division on complex128 is not a few float instructions: the compiler emits a call to the runtime's complex division helper, which runs Smith's algorithm to avoid intermediate overflow.
- ✓The gc compiler does not auto-vectorise, so a pure-Go butterfly is scalar; gonum's speed comes from a tight allocation-free loop, not SIMD.
What Fast Fourier Transform actually is
Fast Fourier Transform in Go: what you actually reach for
Go implementation
package main
import (
"fmt"
"math"
"math/cmplx"
"gonum.org/v1/gonum/dsp/fourier"
)
func main() {
const n = 1024
const fs = 44100.0
sig := make([]float64, n)
for i := range sig {
sig[i] = math.Sin(2 * math.Pi * 440 * float64(i) / fs)
}
fft := fourier.NewFFT(n) // twiddles built once: keep and reuse
coeff := fft.Coefficients(nil, sig) // len(coeff) == n/2+1 for real input
best, k := 0.0, 0
for i, c := range coeff {
if m := cmplx.Abs(c); m > best {
best, k = m, i
}
}
fmt.Printf("peak %.1f Hz\n", float64(k)*fs/float64(n))
inv := 1 / float64(n)
for i, c := range coeff { // multiply; never c / complex(n, 0)
coeff[i] = complex(real(c)*inv, imag(c)*inv)
}
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Go-specific trap
Performance in Go, beyond the O(n log n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Go type or library should I use for Fast Fourier Transform?
Go has complex64 and complex128 as builtin types with the complex, real and imag builtins, plus math/cmplx (Abs, Exp, Rect, Sqrt) - but no transform anywhere in the standard library. The ecosystem answer is gonum.org/v1/gonum/dsp/fourier: fourier.NewFFT(n) precomputes the twiddle tables, Coefficients(dst, seq []float64) []complex128 runs the real-input forward transform, Sequence goes back, and NewCmplxFFT covers complex input. Confirm the package's scaling convention with a round trip before trusting absolute magnitudes.
What is the most common Go mistake when implementing Fast Fourier Transform?
Division on complex128 is not a few float instructions: the compiler emits a call to the runtime's complex division helper, which runs Smith's algorithm to avoid intermediate overflow. So the innocent normalisation loop c / complex(float64(n), 0) is a function call per element and can cost more than the transform it follows. Precompute inv := 1 / float64(n) and build the result with complex(real(c)*inv, imag(c)*inv). Nothing in a profile points at the '/' character - it shows up as time inside runtime, which is where people stop looking.
How does Fast Fourier Transform actually perform in Go?
The gc compiler does not auto-vectorise, so a pure-Go butterfly is scalar; gonum's speed comes from a tight allocation-free loop, not SIMD. A []complex128 contains no pointers, so the garbage collector never scans it - large spectra are cheap to hold, unlike slices of structs carrying pointer fields. Bounds-check elimination is worth the effort: reslice once to a length the compiler can see and index off that. A slice the transform allocates and returns escapes to the heap - which is exactly why the dst convention exists: hand it a buffer you already own and escape analysis has nothing to promote. Time complexity is O(n log n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialBefore paying for GitHub Copilot, check its student and free-tier options on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.