dsp/window: move complex sequence weighting onto Values

This simplifies the API and removes the NaN from Inf problem.
This commit is contained in:
Dan Kortschak
2020-09-12 19:55:02 +09:30
parent ddc5baeb4a
commit 8d26fba056
2 changed files with 7 additions and 19 deletions

View File

@@ -147,23 +147,10 @@ func (v Values) Transform(seq []float64) []float64 {
return seq return seq
} }
// ValuesComplex is an arbitrary complex window function. // TransformComplex applies the weights in the receiver to seq in place, returning
type ValuesComplex []complex128 // the result. If v is nil, TransformComplex is a no-op, otherwise the length of v
// must match the length of seq.
// NewValuesComplex returns a ValuesComplex of length n with weights corresponding func (v Values) TransformComplex(seq []complex128) []complex128 {
// 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 {
if v == nil { if v == nil {
return seq return seq
} }
@@ -171,7 +158,8 @@ func (v ValuesComplex) Transform(seq []complex128) []complex128 {
panic("window: length mismatch") panic("window: length mismatch")
} }
for i, w := range v { for i, w := range v {
seq[i] *= w sv := seq[i]
seq[i] = complex(w*real(sv), w*imag(sv))
} }
return seq return seq
} }

View File

@@ -202,7 +202,7 @@ func TestWindowsComplex(t *testing.T) {
src[i] = complex(1, 1) 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) { if !equalApprox(dst, test.want, tol) {
t.Errorf("unexpected result for lookup window function %q:\ngot:%#.6v\nwant:%#.6v", test.name, dst, test.want) t.Errorf("unexpected result for lookup window function %q:\ngot:%#.6v\nwant:%#.6v", test.name, dst, test.want)
} }