Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Blackman-Harris window function and create go.mod file #27

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/mjibson/go-dsp

go 1.21.1
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this file would definitely need to be in a separate PR. And since this repo doesn't have any tests, I'm not inclined to do that because I'm worried about unknown breakage. Also it should be the lowest supported go version, not the highest. In summary this change would need:

  • separate PR
  • running go test in a github action
  • minimum supported go version

19 changes: 19 additions & 0 deletions window/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,22 @@ func Blackman(L int) []float64 {
}
return r
}

// BlackmanHarris returns an L-point Blackman-Harris window
// Reference: https://www.mathworks.com/help/signal/ref/blackmanharris.html
func BlackmanHarris(L int) []float64 {
r := make([]float64, L)
if L == 1 {
r[0] = 1
} else {
N := L - 1
for n := 0; n <= N; n++ {
const term0 = 0.35875
term1 := -0.48829 * math.Cos(2*math.Pi*float64(n)/float64(N))
term2 := 0.14128 * math.Cos(4*math.Pi*float64(n)/float64(N))
term3 := -0.01168 * math.Cos(6*math.Pi*float64(n)/float64(N))
r[n] = term0 + term1 + term2 + term3
}
}
return r
}
21 changes: 15 additions & 6 deletions window/window_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ import (
)

type windowTest struct {
in int
hamming []float64
hann []float64
bartlett []float64
flatTop []float64
blackman []float64
in int
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has not been fmt'd.

hamming []float64
hann []float64
bartlett []float64
flatTop []float64
blackman []float64
blackmanHarris []float64
}

var windowTests = []windowTest{
Expand All @@ -39,6 +40,7 @@ var windowTests = []windowTest{
[]float64{1},
[]float64{1},
[]float64{1},
[]float64{1},
},
{
5,
Expand All @@ -47,6 +49,7 @@ var windowTests = []windowTest{
[]float64{0, 0.5, 1, 0.5, 0},
[]float64{-0.0004210510000000013, -0.05473684000000003, 1, -0.05473684000000003, -0.0004210510000000013},
[]float64{0, 0.34, 1, 0.34, 0},
[]float64{0.00006, 0.21747, 1, 0.21747, 0.00006},
},
{
10,
Expand All @@ -55,6 +58,7 @@ var windowTests = []windowTest{
[]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},
[]float64{0, 0.0508696327, 0.258000502, 0.63, 0.951129866, 0.951129866, 0.63, 0.258000502, 0.0508696327, 0},
[]float64{0.00006, 0.01507, 0.14704, 0.52057, 0.93166, 0.93166, 0.52057, 0.14704, 0.01507, 0.00006},
},
}

Expand Down Expand Up @@ -90,5 +94,10 @@ func TestWindowFunctions(t *testing.T) {
if !dsputils.PrettyClose(o, v.blackman) {
t.Error("blackman error\ninput:", v.in, "\noutput:", o, "\nexpected:", v.blackman)
}

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