I'm sure this is my fault, but I can't see what I'm doing wrong.
Given the following simple program, when the input is a 440Hz sine wave, I expect to see a single peak in the frequency domain. Giving the same input to an online FFT calculator, the expected output is obtained.
Can you point out my mistake, or is go-dsp
doing something strange?
The Program
package main
import (
"bufio"
"fmt"
"github.com/mjibson/go-dsp/fft"
"github.com/mjibson/go-dsp/window"
"math"
"os"
"strconv"
)
func main() {
file, err := os.Open(os.Args[1])
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(file)
values := make([]float64, 0)
for scanner.Scan() {
line := scanner.Text()
value, e := strconv.ParseFloat(line, 64)
if e != nil {
panic(e)
}
values = append(values, value)
}
fftComplex := doFft(values)
fftReal := realize(fftComplex)
for _, freq := range fftReal {
fmt.Println(freq)
}
}
func doFft(data []float64) []complex128 {
window.Apply(data, window.Hamming)
return fft.FFTReal(data)
}
func realize(data []complex128) []float64 {
realVals := make([]float64, len(data))
for i, comp := range data {
rel := math.Pow(real(comp), 2)
img := math.Pow(imag(comp), 2)
realVals[i] = math.Sqrt(rel + img)
}
return realVals
}
It's run like:
go run fft.go awave.dat > awave-dsp.fft
The Input
Data is here, it's 440Hz sine. Plotted it looks like:
![screen shot 2014-03-09 at 3 53 19 pm](https://camo.githubusercontent.com/ec64094326ea11451c6bd5d101383b0362e00b40fe64f4a452cfad9ee76b1647/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f3139333432312f323336383937302f31653834636636322d613763362d313165332d383765642d6661376364393132663630322e706e67)
gnuplot -e "plot 'awave.dat'; pause -1"
The Output
Data is here. Plotted it looks like:
![screen shot 2014-03-09 at 3 54 35 pm](https://camo.githubusercontent.com/31fab0282d0d37613aa9bddcad9863fccb48e987dd421cd1d7adcbf88f5d6bf6/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f3139333432312f323336383937322f33316534353930362d613763362d313165332d396230662d3961623634663834653739382e706e67)
The x-scale here isn't Hz, it's just the bin numbers from FFTReal
.
gnuplot -e "plot 'awave-dsp.fft'; pause -1"
Expected Output
Using the same input, and the online FFT mentioned above, I get the expected output:
![screen shot 2014-03-09 at 3 54 54 pm](https://camo.githubusercontent.com/49ba00b53898951add7774f8b688b98f78c09680a3129ae2e996d4f3b5a574a7/68747470733a2f2f662e636c6f75642e6769746875622e636f6d2f6173736574732f3139333432312f323336383937352f35306234646630652d613763362d313165332d393534612d3164316334656239653666352e706e67)