go calculate execution time
package main
// calculate execution time of fft
// need to install: go get github.com/mjibson/go-dsp
// source of code: https://pkg.go.dev/github.com/mjibson/go-dsp/fft#FFT
import(
//"github.com/mjibson/go-dsp/dsputils"
//"math/cmplx"
"fmt"
"time"
"math"
"github.com/mjibson/go-dsp/fft"
)
func main(){
numSamples := int(1e6)
freq := 0.1
start := time.Now()
// Equation 3-10.
x := func(n int, freq float64) float64 {
wave0 := 10 * math.Sin(2.0 * math.Pi * float64(n) * freq / 8.0)
wave1 := 0.5 * math.Sin(2*math.Pi*float64(n) * freq /4.0+3.0*math.Pi/4.0)
return wave0 + wave1
}
// Discretize our function by sampling at 8 points.
a := make([]float64, numSamples)
for i := 0; i < numSamples; i++ {
a[i] = x(i, freq)
}
fmt.Println(time.Since(start))
_ = fft.FFTReal(a)
fmt.Println(time.Since(start))
// Print the magnitude and phase at each frequency.
//for i := 0; i < numSamples; i++ {
// r, θ := cmplx.Polar(X[i])
// θ *= 360.0 / (2 * math.Pi)
// if dsputils.Float64Equal(r, 0) {
// θ = 0 // (When the magnitude is close to 0, the angle is meaningless)
// }
// fmt.Printf("X(%d) = %.1f ∠ %.1f°\n", i, r, θ)
//}
}