Skip to content

Commit

Permalink
Add flat top window
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalkuo authored and maddyblue committed Jan 4, 2017
1 parent 5b3d8c8 commit 49dba83
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
36 changes: 36 additions & 0 deletions window/window.go
Expand Up @@ -96,3 +96,39 @@ func Bartlett(L int) []float64 {

return r
}

// FlatTop returns an L-point flat top window.
// Reference: http://www.mathworks.com/help/signal/ref/flattopwin.html
func FlatTop(L int) []float64 {
const (
alpha0 = float64(0.21557895)
alpha1 = float64(0.41663158)
alpha2 = float64(0.277263158)
alpha3 = float64(0.083578947)
alpha4 = float64(0.006947368)
)

r := make([]float64, L)

if L == 1 {
r[0] = 1
return r
}

N := L - 1
coef := 2 * math.Pi / float64(N)

for n := 0; n <= N; n++ {
factor := float64(n) * coef

term0 := alpha0
term1 := alpha1 * math.Cos(factor)
term2 := alpha2 * math.Cos(2*factor)
term3 := alpha3 * math.Cos(3*factor)
term4 := alpha4 * math.Cos(4*factor)

r[n] = term0 - term1 + term2 - term3 + term4
}

return r
}
9 changes: 9 additions & 0 deletions window/window_test.go
Expand Up @@ -27,6 +27,7 @@ type windowTest struct {
hamming []float64
hann []float64
bartlett []float64
flatTop []float64
}

var windowTests = []windowTest{
Expand All @@ -35,18 +36,21 @@ var windowTests = []windowTest{
[]float64{1},
[]float64{1},
[]float64{1},
[]float64{1},
},
{
5,
[]float64{0.08, 0.54, 1, 0.54, 0.08},
[]float64{0, 0.5, 1, 0.5, 0},
[]float64{0, 0.5, 1, 0.5, 0},
[]float64{-0.0004210510000000013, -0.05473684000000003, 1, -0.05473684000000003, -0.0004210510000000013},
},
{
10,
[]float64{0.08, 0.18761956, 0.46012184, 0.77, 0.97225861, 0.97225861, 0.77, 0.46012184, 0.18761956, 0.08},
[]float64{0, 0.116977778440511, 0.413175911166535, 0.75, 0.969846310392954, 0.969846310392954, 0.75, 0.413175911166535, 0.116977778440511, 0},
[]float64{0, 0.222222222222222, 0.444444444444444, 0.666666666666667, 0.888888888888889, 0.888888888888889, 0.666666666666667, 0.444444444444444, 0.222222222222222, 0},
[]float64{-0.000421051000000, -0.020172031509486, -0.070199042063189, 0.198210530000000, 0.862476344072674, 0.862476344072674, 0.198210530000000, -0.070199042063189, -0.020172031509486, -0.000421051000000},
},
}

Expand All @@ -72,5 +76,10 @@ func TestWindowFunctions(t *testing.T) {
if !dsputils.PrettyClose(o, v.hamming) {
t.Error("apply error\noutput:", o, "\nexpected:", v.hamming)
}

o = FlatTop(v.in)
if !dsputils.PrettyClose(o, v.flatTop) {
t.Error("flatTop error\ninput:", v.in, "\noutput:", o, "\nexpected:", v.flatTop)
}
}
}

0 comments on commit 49dba83

Please sign in to comment.