Created
December 15, 2016 23:59
-
-
Save lleveque/b27d9de6f390f53e185fde543583cae6 to your computer and use it in GitHub Desktop.
Redoing the Mathworks fft exercise : https://fr.mathworks.com/help/matlab/ref/fft.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "github.com/mjibson/go-dsp/fft" | |
import "math" | |
import "fmt" | |
import "math/rand" | |
import "math/cmplx" | |
func main() { | |
Fs := 1000.0 // Sampling frequency | |
T := 1/Fs // Sampling period | |
L := 1000 // Length of signal | |
var t, S, X, P2, P1, f []float64 | |
var Y []complex128 | |
// build signal+noise | |
for i := 0; i < L; i++ { | |
t = append(t, float64(i)*T) | |
S = append(S, 0.7*math.Sin(2*math.Pi*50*t[i]) + math.Sin(2*math.Pi*120*t[i])) | |
X = append(X, S[i] + rand.Float64()) | |
} | |
Y = fft.FFTReal(X) | |
// go back to real and take weight into account | |
for _, e := range(Y) { | |
P2 = append(P2, cmplx.Abs(e)/float64(L)) | |
} | |
// fold the FFT | |
for i, e := range(P2[0:L/2]) { | |
if i == 0 { | |
P1 = append(P1, e) | |
} else { | |
P1 = append(P1, 2*e) | |
} | |
} | |
// build frequencies | |
for i := 0; i<L/2; i++ { | |
f = append(f, Fs * float64(i) / float64(L)) | |
} | |
for i, e := range(f) { | |
fmt.Println(e, P1[i]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment