-
Notifications
You must be signed in to change notification settings - Fork 1
/
plot_test.go
50 lines (46 loc) · 1.17 KB
/
plot_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package biquad_test
import (
"math"
"testing"
"github.com/soypat/biquad"
"gonum.org/v1/plot"
"gonum.org/v1/plot/font"
"gonum.org/v1/plot/plotter"
)
func TestPlot(t *testing.T) {
const (
pi = math.Pi
// working frequency. The frequency that matters (we wish to keep it)
f0 = 2.
fnoise = 4.
fs = 100. // Sampling frequency.
N = 100 // amount of sample points
ts = 1 / fs // sampling period
)
// We generate waveform data composed of a "working" frequency and a "noise" frequency
data := make([]float64, N)
for i := 0; i < N; i++ {
t := float64(i) * ts
data[i] = math.Sin(2*pi*f0*t) + math.Sin(2*pi*fnoise*t)
}
signal := biquad.MakeSignal(fs, data)
lp, err := biquad.NewLowPass(fs, f0, 1)
if err != nil {
panic(err)
}
filtered, err := lp.Filter(signal)
if err != nil {
panic(err)
}
p := plot.New()
ls, _ := plotter.NewLine(signal)
lf, _ := plotter.NewLine(filtered)
lf.Dashes = []font.Length{0.1 * font.Centimeter, 0.1 * font.Centimeter}
p.Add(ls, lf)
p.Legend.Add("original signal", ls)
p.Legend.Add("filtered signal", lf)
err = p.Save(30*font.Centimeter, 15*font.Centimeter, "out.png")
if err != nil {
panic(err)
}
}