Skip to content

Instantly share code, notes, and snippets.

@brettbuddin
Created June 4, 2019 18:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettbuddin/d15923d3180d1624997daec9067897e5 to your computer and use it in GitHub Desktop.
Save brettbuddin/d15923d3180d1624997daec9067897e5 to your computer and use it in GitHub Desktop.
Upsample via time domain.
package fourier
import "errors"
func Upsample(in []float64, scale int) ([]float64, error) {
if scale < 1 {
return nil, errors.New("scale cannot be less than one")
}
var (
l = len(in)
ln = l * scale
cin = make([]complex128, l)
cpadded = make([]complex128, ln)
padded = make([]float64, ln)
)
for i, v := range in {
cin[i] = complex(v*float64(scale), 0)
}
if err := Forward(cin); err != nil {
return nil, err
}
// Split the spectral content down the middle and copy both ends into the
// ends of a new upscaled buffer. This will leave the zeros in the center.
for i := 0; i < l/2; i++ {
cpadded[i] = cin[i]
cpadded[len(cpadded)-1-i] = cin[len(cin)-1-i]
}
if err := Inverse(cpadded); err != nil {
return nil, err
}
for i, v := range cpadded {
padded[i] = real(v)
}
return padded, nil
}
package fourier
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestResample(t *testing.T) {
for _, tt := range []struct {
src []float64
scale int
expected []float64
}{
{
src: []float64{1, 0.5, 1.0, 0.5, 1.0, 0.5, 1.0, 0.5},
scale: 1,
expected: []float64{1, 0.5, 1.0, 0.5, 1.0, 0.5, 1.0, 0.5},
},
{
src: []float64{1, 0.5, 1.0, 0.5, 1.0, 0.5, 1.0, 0.5},
scale: 2,
expected: []float64{1, 0.75, 0.5, 0.75, 1.0, 0.75, 0.5, 0.75, 1.0, 0.75, 0.5, 0.75, 1.0, 0.75, 0.5, 0.75},
},
{
src: []float64{1, 0.5, 1.0, 0.5, 1.0, 0.5, 1.0, 0.5},
scale: 8,
expected: []float64{1, 0.9809698831278217, 0.9267766952966369, 0.8456708580912724, 0.75, 0.6543291419087276, 0.5732233047033631, 0.5190301168721783, 0.5, 0.5190301168721783, 0.5732233047033631, 0.6543291419087276, 0.75, 0.8456708580912724, 0.9267766952966369, 0.9809698831278217, 1, 0.9809698831278217, 0.9267766952966369, 0.8456708580912724, 0.75, 0.6543291419087276, 0.5732233047033631, 0.5190301168721783, 0.5, 0.5190301168721783, 0.5732233047033631, 0.6543291419087276, 0.75, 0.8456708580912724, 0.9267766952966369, 0.9809698831278217, 1, 0.9809698831278217, 0.9267766952966369, 0.8456708580912724, 0.75, 0.6543291419087276, 0.5732233047033631, 0.5190301168721783, 0.5, 0.5190301168721783, 0.5732233047033631, 0.6543291419087276, 0.75, 0.8456708580912724, 0.9267766952966369, 0.9809698831278217, 1, 0.9809698831278217, 0.9267766952966369, 0.8456708580912724, 0.75, 0.6543291419087276, 0.5732233047033631, 0.5190301168721783, 0.5, 0.5190301168721783, 0.5732233047033631, 0.6543291419087276, 0.75, 0.8456708580912724, 0.9267766952966369, 0.9809698831278217},
},
} {
out, err := Upsample(tt.src, tt.scale)
assert.NoError(t, err)
assert.Equal(t, tt.expected, out)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment