This project demonstrates how to perform a Discrete Fourier Transform (DFT) using the Gonum DSP Fourier package in Go. It generates a composite waveform composed of multiple sine waves, applies a Hanning window to reduce spectral leakage, computes the FFT, and detects the main frequency components.
This example showcases:
- Generating a composite signal (sum of multiple sine waves).
- Applying a Hanning window to smooth discontinuities at signal edges.
- Computing the Fast Fourier Transform (FFT) using Gonum’s
fourierpackage. - Identifying the dominant frequencies and their magnitudes.
The program reconstructs the frequencies and amplitudes that make up a mixed signal.

Source: https://www.3blue1brown.com/lessons/fourier-transforms
- Go 1.18+
- Gonum
wave := GenerateCompositeWave(freqs, amplitudes, sampleRate, duration)ApplyHanningWindow(wave)fft := fourier.NewFFT(fftSize)
spectrum := fft.Coefficients(nil, paddedWave)Magnitude is calculated from the complex coefficients and scaled by signal length and window gain.
// Compute magnitude spectrum using original wave length for amplitude scaling
windowGain := 0.5
mag := make([]float64, fftSize/2)
for i := 0; i < fftSize/2; i++ {
mag[i] = cmplx.Abs(spectrum[i]) * 2 / float64(len(wave)) / windowGain
}peaks := FindMainPeaks(mag, freqRes, neighborhoodHz, threshold)| Parameter | Description | Example Value |
|---|---|---|
sampleRate |
Sampling rate in Hz | 1024 |
duration |
Signal duration in seconds | 15.0 |
freqs |
Frequencies of the sine components (Hz) | [50, 120, 300] |
amplitudes |
Amplitudes of each sine wave | [1.0, 0.5, 0.8] |
neighborhoodHz |
Range for filtering side lobes (Hz) | 3.0 |
threshold |
Minimum magnitude for peak detection | 0.05 |
$ go run examples/synthetic/dft_synthetic.go
Detected main frequencies:
Frequency: 50.0 Hz, Magnitude: 1.000
Frequency: 120.0 Hz, Magnitude: 0.500
Frequency: 300.0 Hz, Magnitude: 0.800
Or for an audio file:
$ go run examples/audio_file/dft_audio_file.go \
-input my_audio_file.mp3 \
-duration 1 \
-mmt 0.001 \
-start 0