Skip to content

Commit

Permalink
dsp/window: move complex sequence weighting onto Values
Browse files Browse the repository at this point in the history
This simplifies the API and removes the NaN from Inf problem.
  • Loading branch information
kortschak committed Sep 15, 2020
1 parent ddc5bae commit 8d26fba
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
24 changes: 6 additions & 18 deletions dsp/window/window_parametric.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,31 +147,19 @@ func (v Values) Transform(seq []float64) []float64 {
return seq
}

// ValuesComplex is an arbitrary complex window function.
type ValuesComplex []complex128

// NewValuesComplex returns a ValuesComplex of length n with weights corresponding
// to the provided window function.
func NewValuesComplex(window func([]complex128) []complex128, n int) ValuesComplex {
v := make(ValuesComplex, n)
for i := range v {
v[i] = 1
}
return window(v)
}

// Transform applies the weights in the receiver to seq in place, returning the
// result. If v is nil, Transform is a no-op, otherwise the length of v must
// match the length of seq.
func (v ValuesComplex) Transform(seq []complex128) []complex128 {
// TransformComplex applies the weights in the receiver to seq in place, returning
// the result. If v is nil, TransformComplex is a no-op, otherwise the length of v
// must match the length of seq.
func (v Values) TransformComplex(seq []complex128) []complex128 {
if v == nil {
return seq
}
if len(v) != len(seq) {
panic("window: length mismatch")
}
for i, w := range v {
seq[i] *= w
sv := seq[i]
seq[i] = complex(w*real(sv), w*imag(sv))
}
return seq
}
2 changes: 1 addition & 1 deletion dsp/window/window_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func TestWindowsComplex(t *testing.T) {
src[i] = complex(1, 1)
}

dst = NewValuesComplex(test.fnCmplx, len(src)).Transform(src)
dst = NewValues(test.fn, len(src)).TransformComplex(src)
if !equalApprox(dst, test.want, tol) {
t.Errorf("unexpected result for lookup window function %q:\ngot:%#.6v\nwant:%#.6v", test.name, dst, test.want)
}
Expand Down

0 comments on commit 8d26fba

Please sign in to comment.