Checkpoint
This commit is contained in:
160 files changed
+76293
-387
No files matched your search
+26
-4
@@ -32,6 +32,28 @@ type Number interface {
|
||||
Integer | Float
|
||||
}
|
||||
|
||||
// Min of values.
|
||||
func Min[T Number](data []T) T {
|
||||
var min T
|
||||
for _, v := range data {
|
||||
if v < min {
|
||||
min = v
|
||||
}
|
||||
}
|
||||
return min
|
||||
}
|
||||
|
||||
// Max of values.
|
||||
func Max[T Number](data []T) T {
|
||||
var max T
|
||||
for _, v := range data {
|
||||
if v > max {
|
||||
max = v
|
||||
}
|
||||
}
|
||||
return max
|
||||
}
|
||||
|
||||
// Sum of values.
|
||||
func Sum[T Number](data []T) T {
|
||||
if len(data) == 0 {
|
||||
@@ -124,7 +146,7 @@ func MeanOffset[T Number](data []T, start, offset int) float64 {
|
||||
}
|
||||
|
||||
// RMS is the root mean square of values.
|
||||
func RMS[T Number](data []T) float64 {
|
||||
func RMS[T Number](data []T) T {
|
||||
if len(data) == 0 {
|
||||
return 0
|
||||
}
|
||||
@@ -134,11 +156,11 @@ func RMS[T Number](data []T) float64 {
|
||||
squares += float64(v) * float64(v)
|
||||
}
|
||||
|
||||
return math.Sqrt(squares / float64(len(data)))
|
||||
return T(math.Sqrt(squares / float64(len(data))))
|
||||
}
|
||||
|
||||
// RMSOffset is the root mean square of values every offset.
|
||||
func RMSOffset[T Number](data []T, start, offset int) float64 {
|
||||
func RMSOffset[T Number](data []T, start, offset int) T {
|
||||
if len(data)/offset == 0 {
|
||||
return 0
|
||||
}
|
||||
@@ -148,7 +170,7 @@ func RMSOffset[T Number](data []T, start, offset int) float64 {
|
||||
squares += float64(data[i]) * float64(data[i])
|
||||
}
|
||||
|
||||
return math.Sqrt(squares / float64(len(data)/offset))
|
||||
return T(math.Sqrt(squares / float64(len(data)/offset)))
|
||||
}
|
||||
|
||||
func Eq(a, b, epsilon float32) bool {
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"math"
|
||||
"testing"
|
||||
|
||||
"go.maze.io/harmon/internal/calc"
|
||||
"github.com/BeatGlow/beatglow/internal/calc"
|
||||
)
|
||||
|
||||
func TestMean(t *testing.T) {
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package calc
|
||||
|
||||
import "math"
|
||||
|
||||
// Hann modifies seq in place by the Hann window and returns the result.
|
||||
// See https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows
|
||||
// and https://www.recordingblogs.com/wiki/hann-window for details.
|
||||
//
|
||||
// The Hann window is a high-resolution window.
|
||||
//
|
||||
// The sequence weights are
|
||||
//
|
||||
// w[k] = 0.5*(1 - cos(2*π*k/(N-1))),
|
||||
//
|
||||
// for k=0,1,...,N-1 where N is the length of the window.
|
||||
//
|
||||
// Spectral leakage parameters: ΔF_0 = 4, ΔF_0.5 = 1.5, K = 2, ɣ_max = -31.5, β = -6.
|
||||
func Hann(dst, src []float64) {
|
||||
k := 2 * math.Pi / float64(len(src)-1)
|
||||
for i := range src {
|
||||
dst[i] = src[i] * (0.5 * (1 - math.Cos(k*float64(i))))
|
||||
}
|
||||
}
|
||||
|
||||
// Hamming modifies seq in place by the Hamming window and returns the result.
|
||||
// See https://en.wikipedia.org/wiki/Window_function#Hann_and_Hamming_windows
|
||||
// and https://www.recordingblogs.com/wiki/hamming-window for details.
|
||||
//
|
||||
// The Hamming window is a high-resolution window. Among K=2 windows it has
|
||||
// the highest ɣ_max.
|
||||
//
|
||||
// The sequence weights are
|
||||
//
|
||||
// w[k] = 25/46 - 21/46 * cos(2*π*k/(N-1)),
|
||||
//
|
||||
// for k=0,1,...,N-1 where N is the length of the window.
|
||||
//
|
||||
// Spectral leakage parameters: ΔF_0 = 4, ΔF_0.5 = 1.33, K = 2, ɣ_max = -42, β = -5.37.
|
||||
func Hamming(dst, src []float64) {
|
||||
const (
|
||||
a0 = 0.54
|
||||
a1 = 0.46
|
||||
)
|
||||
|
||||
k := 2 * math.Pi / float64(len(src)-1)
|
||||
for i := range src {
|
||||
dst[i] = src[i] * (a0 - a1*math.Cos(k*float64(i)))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user