-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtvd1d_test.go
63 lines (57 loc) · 1.44 KB
/
tvd1d_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
51
52
53
54
55
56
57
58
59
60
61
62
63
package tvd_test
import (
"image/color"
"math/rand"
"testing"
"github.com/soypat/tvd"
"gonum.org/v1/plot"
"gonum.org/v1/plot/font"
"gonum.org/v1/plot/plotter"
)
func TestDenoisingWithGonumGraphVisualization(t *testing.T) {
// Generate noisy data
const (
width, height = 19 * font.Centimeter, 10 * font.Centimeter
xstart, xmax = 0, 10
div = 10000
lambda = 32
noiseAmp = 4
)
randf := rand.New(rand.NewSource(1)).Float64
fn := func(x float64) (y float64) {
switch {
case x < xmax/6:
y = 4
case x < 2*xmax/3:
y = 2
default:
y = x
}
return y + noiseAmp*randf()/2
}
var noisy plotter.XYs
noiseSlice := make([]float64, div)
for i := 0; i < div; i++ {
x := xstart + xmax*float64(i)/float64(div)
noiseSlice[i] = fn(x)
noisy = append(noisy, plotter.XY{X: x, Y: noiseSlice[i]})
}
denoiseSlice := tvd.Denoise1D(noiseSlice, lambda)
var denoised plotter.XYs
for i := 0; i < div; i++ {
x := xstart + xmax*float64(i)/float64(div)
denoised = append(denoised, plotter.XY{X: x, Y: denoiseSlice[i]})
}
p := plot.New()
p.Title.Text = "Total variation denoising"
p.X.Label.Text = "x"
p.Y.Label.Text = "y"
plttr1, _ := plotter.NewLine(noisy)
plttr2, _ := plotter.NewLine(denoised)
plttr2.Color = color.RGBA{R: 255, A: 255}
plttr1.Color = color.RGBA{B: 255, A: 255}
p.Add(plttr1, plttr2)
p.Legend.Add("Noisy", plttr1)
p.Legend.Add("Denoised", plttr2)
p.Save(width, height, "tvd1d_test.png")
}