Checkpoint

This commit is contained in:
maze authored and maze committed 2026-10-29 17:29:41 +01:00
1 parent 85a9ec684c
commit c5d6137fab
160 files changed
+76293 -387

No files matched your search

+26 -4
View File
@@ -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 {
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"math"
"testing"
"go.maze.io/harmon/internal/calc"
"github.com/BeatGlow/beatglow/internal/calc"
)
func TestMean(t *testing.T) {
+49
View File
@@ -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)))
}
}
+26
View File
@@ -0,0 +1,26 @@
package fourier
import "unsafe"
// ToComplex converts a slice of float64 into a slice of complex128, where the input
// slice's values are set as the real part of the complex numbers
//
// In Go, a complex number is a pair of two float numbers. This means that converting a
// float number to a complex number only makes it necessary to assign the (float's) value as
// the real part of the complex number, leaving the imaginary as zero.
//
// This particular function leverages that by creating a float slice of twice the size as the inputs, and
// placing each element of the input on every second slot. Finally, it casts this float slice as a complex slice,
// using an unsafe approach to prioritize performance.
func ToComplex(x []float64) []complex128 {
var (
length = len(x)
out = make([]float64, length*2)
)
for i, j := 0, 0; i < len(x); i, j = i+1, (i+1)*2 {
out[j] = x[i]
}
return (*(*[]complex128)(unsafe.Pointer(&out)))[:length]
}
+147
View File
@@ -0,0 +1,147 @@
package fourier
import (
"math"
"github.com/BeatGlow/BeatGlow/internal/dsp/fourier/window"
)
const (
tau = 2 * math.Pi
// DefaultMagnitudeThreshold describes the default value where a certain
// frequency is strong enough to be considered relevant to the spectrum filter.
DefaultMagnitudeThreshold = 10
)
// FrequencyPower denotes a single frequency and its magnitude in a Fast
// Fourier Transform of a signal.
type FrequencyPower struct {
Freq int
Mag float64
}
// Apply applies a Fast Fourier Transform (FFT) on a slice of float64 `data`,
// with sample rate `sampleRate`. It returns a slice of FrequencyPower.
func Apply(sampleRate int, data []float64, w window.Window) []FrequencyPower {
var (
ln = len(data)
magnitudes = make([]FrequencyPower, 0, (ln/2)-1)
)
// apply a window function to the values
if w != nil && len(w) == ln {
w.Apply(data)
}
// apply a fast Fourier transform on the data; exclude index 0, no 0Hz-freq results
spectrum := FFT(ToComplex(data))
for i := 1; i < ln/2; i++ {
freqReal := real(spectrum[i])
freqImag := imag(spectrum[i])
// map the magnitude for each frequency bin to the corresponding value in the map
// using math.Sqrt(re*re + im*im) is faster than using math.Hypot(re, im)
// see fft_test.go for details
magnitudes = append(
magnitudes,
FrequencyPower{
Freq: i * sampleRate / ln,
Mag: math.Sqrt(freqReal*freqReal + freqImag*freqImag),
},
)
}
return magnitudes
}
// FFT applies a Fast Fourier Transform to the input slice of complex128 values, to
// retrieve the frequency spectrum of a digital signal.
func FFT(value []complex128) []complex128 {
var (
valueLen = len(value)
factors = GetRadix2Factors(valueLen)
temp = make([]complex128, valueLen) // temp
)
value = ReorderData(value)
// stage increases by a power of two
for stage := 2; stage <= valueLen; stage <<= 1 {
var (
blocks = valueLen / stage
stage2Value = stage / 2
)
// iterate through each item in the batch, increasing by the stage value
for batchIdx := 0; batchIdx < valueLen; batchIdx += stage {
if stage == 2 { // "first stage" scenario
var (
reorderIdx = value[batchIdx]
reorderNext = value[batchIdx+1]
)
temp[batchIdx] = reorderIdx + reorderNext
temp[batchIdx+1] = reorderIdx - reorderNext
continue
}
for iter := 0; iter < stage2Value; iter++ {
var (
idx = iter + batchIdx
idx2 = idx + stage2Value
reorderIdx = value[idx]
factorized = value[idx2] * factors[blocks*iter]
)
temp[idx] = reorderIdx + factorized
temp[idx2] = reorderIdx - factorized
}
}
value, temp = temp, value
}
return value
}
// IFFT returns the Inverse Fast Fourier Transform of a given complex128 slice.
func IFFT(value []complex128) []complex128 {
var (
ln = len(value)
output = make([]complex128, ln)
factor = complex(float64(ln), 0)
)
// Reverse inputs, which is calculated with modulo factor, hence value[0] as an outlier
output[0] = value[0]
for i, j := 1, ln-1; i < ln; i, j = i+1, j-1 {
output[i] = value[j]
}
output = FFT(output)
for i := range output {
output[i] /= factor
}
return output
}
// Convolve returns the convolution of x ∗ y, applied to the complex128 slice x.
func Convolve(x, y []complex128) []complex128 {
if len(x) != len(y) {
return nil
}
x = FFT(x)
y = FFT(y)
for i := 0; i < len(x); i++ {
x[i] *= y[i]
}
return IFFT(x)
}
+58
View File
@@ -0,0 +1,58 @@
package fourier
// Log2Types is a type constraint interface to scope the allowed types to
// call the Log2 function.
type Log2Types interface {
uint | uint16 | uint32 | uint64 | int | int16 | int32 | int64
}
// Log2 returns the log base 2 of v, matching the input to precomputed
// values to achieve the fastest performance possible.
//
// If the input is not present, then its log2 value is computed with a fast
// bit-shifting technique.
//
// from: http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious
func Log2[T Log2Types](v T) T {
switch v {
case 0, 1:
return 0
case 2:
return 1
case 4:
return 2
case 8:
return 3
case 16:
return 4
case 32:
return 5
case 64:
return 6
case 128:
return 7
case 256:
return 8
case 512:
return 9
case 1024:
return 10
case 2048:
return 11
case 4096:
return 12
case 8192:
return 13
default:
return log2(v)
}
}
func log2[T Log2Types](v T) T {
var r T
for v >>= 1; v != 0; v >>= 1 {
r++
}
return r
}
+45
View File
@@ -0,0 +1,45 @@
package fourier
import "math"
const (
firstCur = 2048
firstPrev = 1024
)
// GetRadix2Factors is temporarily public, could become private at a later point.
func GetRadix2Factors(inputLen int) []complex128 {
if factors, ok := radix2Factors[inputLen]; ok {
return factors
}
for factor, prev := firstCur, firstPrev; factor <= inputLen; factor, prev = factor<<1, factor {
if _, ok := radix2Factors[factor]; !ok {
radix2Factors[factor] = make([]complex128, factor)
for n, j := 0, 0; n < factor; n, j = n+2, j+1 {
radix2Factors[factor][n] = radix2Factors[prev][j]
}
for n := 1; n < factor; n += 2 {
v := -tau / float64(factor) * float64(n)
radix2Factors[factor][n] = complex(
math.Cos(v), math.Sin(v),
)
}
}
}
return radix2Factors[inputLen]
}
var radix2Factors = map[int][]complex128{
4: {1 + 0i, 0 - 1i, -1 + 0i, 0 + 1i},
8: {1 + 0i, 0.7071067811865476 - 0.7071067811865475i, 0 - 1i, -0.7071067811865475 - 0.7071067811865476i, -1 + 0i, -0.7071067811865477 + 0.7071067811865475i, 0 + 1i, 0.7071067811865475 + 0.7071067811865477i},
16: {1 + 0i, 0.9238795325112867 - 0.3826834323650898i, 0.7071067811865476 - 0.7071067811865475i, 0.38268343236508984 - 0.9238795325112867i, 0 - 1i, -0.3826834323650897 - 0.9238795325112867i, -0.7071067811865475 - 0.7071067811865476i, -0.9238795325112867 - 0.38268343236508984i, -1 + 0i, -0.9238795325112868 + 0.38268343236508967i, -0.7071067811865477 + 0.7071067811865475i, -0.38268343236509034 + 0.9238795325112865i, 0 + 1i, 0.38268343236509 + 0.9238795325112866i, 0.7071067811865475 + 0.7071067811865477i, 0.9238795325112865 + 0.3826834323650904i},
32: {1 + 0i, 0.9807852804032304 - 0.19509032201612825i, 0.9238795325112867 - 0.3826834323650898i, 0.8314696123025453 - 0.5555702330196022i, 0.7071067811865476 - 0.7071067811865475i, 0.5555702330196023 - 0.8314696123025452i, 0.38268343236508984 - 0.9238795325112867i, 0.1950903220161283 - 0.9807852804032304i, 0 - 1i, -0.1950903220161282 - 0.9807852804032304i, -0.3826834323650897 - 0.9238795325112867i, -0.555570233019602 - 0.8314696123025453i, -0.7071067811865475 - 0.7071067811865476i, -0.8314696123025453 - 0.5555702330196021i, -0.9238795325112867 - 0.38268343236508984i, -0.9807852804032303 - 0.19509032201612858i, -1 + 0i, -0.9807852804032304 + 0.19509032201612836i, -0.9238795325112868 + 0.38268343236508967i, -0.8314696123025453 + 0.555570233019602i, -0.7071067811865477 + 0.7071067811865475i, -0.5555702330196022 + 0.8314696123025453i, -0.38268343236509034 + 0.9238795325112865i, -0.19509032201612866 + 0.9807852804032303i, 0 + 1i, 0.19509032201612828 + 0.9807852804032304i, 0.38268343236509 + 0.9238795325112866i, 0.5555702330196018 + 0.8314696123025455i, 0.7071067811865475 + 0.7071067811865477i, 0.8314696123025453 + 0.5555702330196022i, 0.9238795325112865 + 0.3826834323650904i, 0.9807852804032303 + 0.19509032201612872i},
64: {1 + 0i, 0.9951847266721968 - 0.0980171403295606i, 0.9807852804032304 - 0.19509032201612825i, 0.9569403357322088 - 0.29028467725446233i, 0.9238795325112867 - 0.3826834323650898i, 0.881921264348355 - 0.47139673682599764i, 0.8314696123025453 - 0.5555702330196022i, 0.773010453362737 - 0.6343932841636455i, 0.7071067811865476 - 0.7071067811865475i, 0.6343932841636456 - 0.7730104533627369i, 0.5555702330196023 - 0.8314696123025452i, 0.4713967368259978 - 0.8819212643483549i, 0.38268343236508984 - 0.9238795325112867i, 0.29028467725446233 - 0.9569403357322089i, 0.1950903220161283 - 0.9807852804032304i, 0.09801714032956077 - 0.9951847266721968i, 0 - 1i, -0.09801714032956066 - 0.9951847266721968i, -0.1950903220161282 - 0.9807852804032304i, -0.2902846772544622 - 0.9569403357322089i, -0.3826834323650897 - 0.9238795325112867i, -0.4713967368259977 - 0.8819212643483549i, -0.555570233019602 - 0.8314696123025453i, -0.6343932841636453 - 0.7730104533627371i, -0.7071067811865475 - 0.7071067811865476i, -0.773010453362737 - 0.6343932841636455i, -0.8314696123025453 - 0.5555702330196021i, -0.8819212643483549 - 0.4713967368259978i, -0.9238795325112867 - 0.38268343236508984i, -0.9569403357322088 - 0.29028467725446233i, -0.9807852804032303 - 0.19509032201612858i, -0.9951847266721968 - 0.09801714032956084i, -1 + 0i, -0.9951847266721968 + 0.09801714032956059i, -0.9807852804032304 + 0.19509032201612836i, -0.9569403357322089 + 0.29028467725446216i, -0.9238795325112868 + 0.38268343236508967i, -0.881921264348355 + 0.47139673682599764i, -0.8314696123025453 + 0.555570233019602i, -0.7730104533627371 + 0.6343932841636453i, -0.7071067811865477 + 0.7071067811865475i, -0.6343932841636459 + 0.7730104533627367i, -0.5555702330196022 + 0.8314696123025453i, -0.47139673682599786 + 0.8819212643483548i, -0.38268343236509034 + 0.9238795325112865i, -0.2902846772544624 + 0.9569403357322088i, -0.19509032201612866 + 0.9807852804032303i, -0.09801714032956045 + 0.9951847266721968i, 0 + 1i, 0.09801714032956009 + 0.9951847266721969i, 0.19509032201612828 + 0.9807852804032304i, 0.2902846772544621 + 0.9569403357322089i, 0.38268343236509 + 0.9238795325112866i, 0.4713967368259976 + 0.881921264348355i, 0.5555702330196018 + 0.8314696123025455i, 0.6343932841636456 + 0.7730104533627369i, 0.7071067811865475 + 0.7071067811865477i, 0.7730104533627367 + 0.6343932841636459i, 0.8314696123025453 + 0.5555702330196022i, 0.8819212643483548 + 0.4713967368259979i, 0.9238795325112865 + 0.3826834323650904i, 0.9569403357322088 + 0.29028467725446244i, 0.9807852804032303 + 0.19509032201612872i, 0.9951847266721968 + 0.09801714032956052i},
128: {1 + 0i, 0.9987954562051724 - 0.049067674327418015i, 0.9951847266721968 - 0.0980171403295606i, 0.989176509964781 - 0.14673047445536175i, 0.9807852804032304 - 0.19509032201612825i, 0.9700312531945441 - 0.24298017990326387i, 0.9569403357322088 - 0.29028467725446233i, 0.9415440651830208 - 0.33688985339222005i, 0.9238795325112867 - 0.3826834323650898i, 0.9039892931234433 - 0.4275550934302821i, 0.881921264348355 - 0.47139673682599764i, 0.8577286100002721 - 0.5141027441932217i, 0.8314696123025453 - 0.5555702330196022i, 0.8032075314806449 - 0.5956993044924334i, 0.773010453362737 - 0.6343932841636455i, 0.7409511253549591 - 0.6715589548470183i, 0.7071067811865476 - 0.7071067811865475i, 0.6715589548470184 - 0.7409511253549591i, 0.6343932841636456 - 0.7730104533627369i, 0.5956993044924335 - 0.8032075314806448i, 0.5555702330196023 - 0.8314696123025452i, 0.5141027441932218 - 0.8577286100002721i, 0.4713967368259978 - 0.8819212643483549i, 0.4275550934302822 - 0.9039892931234433i, 0.38268343236508984 - 0.9238795325112867i, 0.33688985339222005 - 0.9415440651830208i, 0.29028467725446233 - 0.9569403357322089i, 0.24298017990326398 - 0.970031253194544i, 0.1950903220161283 - 0.9807852804032304i, 0.14673047445536175 - 0.989176509964781i, 0.09801714032956077 - 0.9951847266721968i, 0.04906767432741813 - 0.9987954562051724i, 0 - 1i, -0.04906767432741801 - 0.9987954562051724i, -0.09801714032956066 - 0.9951847266721968i, -0.14673047445536164 - 0.989176509964781i, -0.1950903220161282 - 0.9807852804032304i, -0.24298017990326387 - 0.9700312531945441i, -0.2902846772544622 - 0.9569403357322089i, -0.33688985339221994 - 0.9415440651830209i, -0.3826834323650897 - 0.9238795325112867i, -0.42755509343028186 - 0.9039892931234434i, -0.4713967368259977 - 0.8819212643483549i, -0.5141027441932216 - 0.8577286100002722i, -0.555570233019602 - 0.8314696123025453i, -0.5956993044924332 - 0.8032075314806449i, -0.6343932841636453 - 0.7730104533627371i, -0.6715589548470184 - 0.7409511253549591i, -0.7071067811865475 - 0.7071067811865476i, -0.7409511253549589 - 0.6715589548470186i, -0.773010453362737 - 0.6343932841636455i, -0.8032075314806448 - 0.5956993044924335i, -0.8314696123025453 - 0.5555702330196021i, -0.8577286100002721 - 0.5141027441932218i, -0.8819212643483549 - 0.4713967368259978i, -0.9039892931234434 - 0.42755509343028203i, -0.9238795325112867 - 0.38268343236508984i, -0.9415440651830207 - 0.3368898533922203i, -0.9569403357322088 - 0.29028467725446233i, -0.970031253194544 - 0.24298017990326404i, -0.9807852804032303 - 0.19509032201612858i, -0.989176509964781 - 0.1467304744553618i, -0.9951847266721968 - 0.09801714032956084i, -0.9987954562051724 - 0.04906767432741797i, -1 + 0i, -0.9987954562051724 + 0.049067674327417724i, -0.9951847266721968 + 0.09801714032956059i, -0.989176509964781 + 0.14673047445536158i, -0.9807852804032304 + 0.19509032201612836i, -0.9700312531945441 + 0.24298017990326382i, -0.9569403357322089 + 0.29028467725446216i, -0.9415440651830208 + 0.3368898533922201i, -0.9238795325112868 + 0.38268343236508967i, -0.9039892931234436 + 0.4275550934302818i, -0.881921264348355 + 0.47139673682599764i, -0.8577286100002722 + 0.5141027441932216i, -0.8314696123025453 + 0.555570233019602i, -0.8032075314806449 + 0.5956993044924332i, -0.7730104533627371 + 0.6343932841636453i, -0.7409511253549591 + 0.6715589548470184i, -0.7071067811865477 + 0.7071067811865475i, -0.6715589548470187 + 0.7409511253549589i, -0.6343932841636459 + 0.7730104533627367i, -0.5956993044924332 + 0.803207531480645i, -0.5555702330196022 + 0.8314696123025453i, -0.5141027441932219 + 0.857728610000272i, -0.47139673682599786 + 0.8819212643483548i, -0.4275550934302825 + 0.9039892931234432i, -0.38268343236509034 + 0.9238795325112865i, -0.33688985339221994 + 0.9415440651830209i, -0.2902846772544624 + 0.9569403357322088i, -0.24298017990326412 + 0.970031253194544i, -0.19509032201612866 + 0.9807852804032303i, -0.14673047445536233 + 0.9891765099647809i, -0.09801714032956045 + 0.9951847266721968i, -0.04906767432741803 + 0.9987954562051724i, 0 + 1i, 0.04906767432741767 + 0.9987954562051724i, 0.09801714032956009 + 0.9951847266721969i, 0.14673047445536194 + 0.9891765099647809i, 0.19509032201612828 + 0.9807852804032304i, 0.24298017990326376 + 0.9700312531945441i, 0.2902846772544621 + 0.9569403357322089i, 0.3368898533922196 + 0.941544065183021i, 0.38268343236509 + 0.9238795325112866i, 0.4275550934302822 + 0.9039892931234433i, 0.4713967368259976 + 0.881921264348355i, 0.5141027441932214 + 0.8577286100002722i, 0.5555702330196018 + 0.8314696123025455i, 0.5956993044924328 + 0.8032075314806453i, 0.6343932841636456 + 0.7730104533627369i, 0.6715589548470183 + 0.7409511253549591i, 0.7071067811865475 + 0.7071067811865477i, 0.7409511253549589 + 0.6715589548470187i, 0.7730104533627367 + 0.6343932841636459i, 0.803207531480645 + 0.5956993044924332i, 0.8314696123025453 + 0.5555702330196022i, 0.857728610000272 + 0.5141027441932219i, 0.8819212643483548 + 0.4713967368259979i, 0.9039892931234431 + 0.427555093430282Line truncated
256: {1 + 0i, 0.9996988186962041 - 0.024541228522912288i, 0.9987954562051724 - 0.049067674327418015i, 0.9972904566786902 - 0.07356456359966743i, 0.9951847266721968 - 0.0980171403295606i, 0.9924795345987101 - 0.1224106751992162i, 0.989176509964781 - 0.14673047445536175i, 0.9852776423889412 - 0.17096188876030122i, 0.9807852804032304 - 0.19509032201612825i, 0.9757021300385286 - 0.2191012401568698i, 0.9700312531945441 - 0.24298017990326387i, 0.9637760657954398 - 0.26671275747489837i, 0.9569403357322088 - 0.29028467725446233i, 0.9495281805930367 - 0.3136817403988915i, 0.9415440651830208 - 0.33688985339222005i, 0.9329927988347388 - 0.3598950365349881i, 0.9238795325112867 - 0.3826834323650898i, 0.9142097557035307 - 0.40524131400498986i, 0.9039892931234433 - 0.4275550934302821i, 0.8932243011955153 - 0.44961132965460654i, 0.881921264348355 - 0.47139673682599764i, 0.8700869911087113 - 0.49289819222978404i, 0.8577286100002721 - 0.5141027441932217i, 0.8448535652497071 - 0.5349976198870972i, 0.8314696123025453 - 0.5555702330196022i, 0.8175848131515837 - 0.5758081914178453i, 0.8032075314806449 - 0.5956993044924334i, 0.7883464276266063 - 0.6152315905806268i, 0.773010453362737 - 0.6343932841636455i, 0.7572088465064846 - 0.6531728429537768i, 0.7409511253549591 - 0.6715589548470183i, 0.724247082951467 - 0.6895405447370669i, 0.7071067811865476 - 0.7071067811865475i, 0.6895405447370669 - 0.7242470829514668i, 0.6715589548470184 - 0.7409511253549591i, 0.6531728429537769 - 0.7572088465064845i, 0.6343932841636456 - 0.7730104533627369i, 0.6152315905806269 - 0.7883464276266062i, 0.5956993044924335 - 0.8032075314806448i, 0.5758081914178454 - 0.8175848131515836i, 0.5555702330196023 - 0.8314696123025452i, 0.5349976198870974 - 0.844853565249707i, 0.5141027441932218 - 0.8577286100002721i, 0.49289819222978415 - 0.8700869911087113i, 0.4713967368259978 - 0.8819212643483549i, 0.4496113296546066 - 0.8932243011955153i, 0.4275550934302822 - 0.9039892931234433i, 0.4052413140049898 - 0.9142097557035307i, 0.38268343236508984 - 0.9238795325112867i, 0.3598950365349883 - 0.9329927988347387i, 0.33688985339222005 - 0.9415440651830208i, 0.3136817403988915 - 0.9495281805930367i, 0.29028467725446233 - 0.9569403357322089i, 0.2667127574748984 - 0.9637760657954398i, 0.24298017990326398 - 0.970031253194544i, 0.21910124015686977 - 0.9757021300385286i, 0.1950903220161283 - 0.9807852804032304i, 0.17096188876030136 - 0.9852776423889412i, 0.14673047445536175 - 0.989176509964781i, 0.12241067519921628 - 0.9924795345987101i, 0.09801714032956077 - 0.9951847266721968i, 0.07356456359966745 - 0.9972904566786902i, 0.04906767432741813 - 0.9987954562051724i, 0.024541228522912267 - 0.9996988186962041i, 0 - 1i, -0.024541228522912142 - 0.9996988186962041i, -0.04906767432741801 - 0.9987954562051724i, -0.07356456359966734 - 0.9972904566786902i, -0.09801714032956066 - 0.9951847266721968i, -0.12241067519921617 - 0.9924795345987101i, -0.14673047445536164 - 0.989176509964781i, -0.17096188876030124 - 0.9852776423889412i, -0.1950903220161282 - 0.9807852804032304i, -0.21910124015686966 - 0.9757021300385286i, -0.24298017990326387 - 0.9700312531945441i, -0.2667127574748983 - 0.9637760657954398i, -0.2902846772544622 - 0.9569403357322089i, -0.31368174039889146 - 0.9495281805930367i, -0.33688985339221994 - 0.9415440651830209i, -0.35989503653498817 - 0.9329927988347388i, -0.3826834323650897 - 0.9238795325112867i, -0.40524131400498975 - 0.9142097557035308i, -0.42755509343028186 - 0.9039892931234434i, -0.4496113296546067 - 0.8932243011955153i, -0.4713967368259977 - 0.8819212643483549i, -0.4928981922297839 - 0.8700869911087115i, -0.5141027441932216 - 0.8577286100002722i, -0.5349976198870969 - 0.8448535652497072i, -0.555570233019602 - 0.8314696123025453i, -0.5758081914178453 - 0.8175848131515837i, -0.5956993044924332 - 0.8032075314806449i, -0.6152315905806267 - 0.7883464276266063i, -0.6343932841636453 - 0.7730104533627371i, -0.6531728429537765 - 0.7572088465064848i, -0.6715589548470184 - 0.7409511253549591i, -0.6895405447370669 - 0.724247082951467i, -0.7071067811865475 - 0.7071067811865476i, -0.7242470829514668 - 0.689540544737067i, -0.7409511253549589 - 0.6715589548470186i, -0.7572088465064847 - 0.6531728429537766i, -0.773010453362737 - 0.6343932841636455i, -0.7883464276266062 - 0.6152315905806269i, -0.8032075314806448 - 0.5956993044924335i, -0.8175848131515835 - 0.5758081914178454i, -0.8314696123025453 - 0.5555702330196021i, -0.8448535652497071 - 0.5349976198870972i, -0.8577286100002721 - 0.5141027441932218i, -0.8700869911087113 - 0.49289819222978415i, -0.8819212643483549 - 0.4713967368259978i, -0.8932243011955152 - 0.4496113296546069i, -0.9039892931234434 - 0.42755509343028203i, -0.9142097557035307 - 0.40524131400498986i, -0.9238795325112867 - 0.38268343236508984i, -0.9329927988347387 - 0.35989503653498833i, -0.9415440651830207 - 0.3368898533922203i, -0.9495281805930367 - 0.3136817403988914i, -0.9569403357322088 - 0.29028467725446233i, -0.9637760657954398 - 0.2667127574748985i, -0.97003125Line truncated
512: {1 + 0i, 0.9999247018391446 - 0.012271538285719925i, 0.9996988186962041 - 0.024541228522912288i, 0.9993223845883494 - 0.03680722294135883i, 0.9987954562051724 - 0.049067674327418015i, 0.9981181129001492 - 0.06132073630220858i, 0.9972904566786902 - 0.07356456359966743i, 0.996312612182778 - 0.0857973123444399i, 0.9951847266721968 - 0.0980171403295606i, 0.9939069700023561 - 0.11022220729388306i, 0.9924795345987101 - 0.1224106751992162i, 0.99090263542778 - 0.13458070850712617i, 0.989176509964781 - 0.14673047445536175i, 0.9873014181578584 - 0.15885814333386145i, 0.9852776423889412 - 0.17096188876030122i, 0.9831054874312164 - 0.18303988795514095i, 0.9807852804032304 - 0.19509032201612825i, 0.9783173707196277 - 0.20711137619221856i, 0.9757021300385286 - 0.2191012401568698i, 0.9729399522055602 - 0.2310581082806711i, 0.9700312531945441 - 0.24298017990326387i, 0.9669764710448521 - 0.25486565960451457i, 0.9637760657954398 - 0.26671275747489837i, 0.9604305194155658 - 0.27851968938505306i, 0.9569403357322088 - 0.29028467725446233i, 0.9533060403541938 - 0.3020059493192281i, 0.9495281805930367 - 0.3136817403988915i, 0.9456073253805213 - 0.3253102921622629i, 0.9415440651830208 - 0.33688985339222005i, 0.937339011912575 - 0.34841868024943456i, 0.9329927988347388 - 0.3598950365349881i, 0.9285060804732156 - 0.37131719395183754i, 0.9238795325112867 - 0.3826834323650898i, 0.9191138516900577 - 0.3939920400610481i, 0.9142097557035307 - 0.40524131400498986i, 0.9091679830905225 - 0.41642956009763715i, 0.9039892931234433 - 0.4275550934302821i, 0.8986744656939538 - 0.43861623853852766i, 0.8932243011955153 - 0.44961132965460654i, 0.8876396204028539 - 0.46053871095824i, 0.881921264348355 - 0.47139673682599764i, 0.8760700941954066 - 0.4821837720791227i, 0.8700869911087113 - 0.49289819222978404i, 0.8639728561215867 - 0.5035383837257176i, 0.8577286100002721 - 0.5141027441932217i, 0.8513551931052652 - 0.524589682678469i, 0.8448535652497071 - 0.5349976198870972i, 0.8382247055548381 - 0.5453249884220465i, 0.8314696123025453 - 0.5555702330196022i, 0.8245893027850253 - 0.5657318107836131i, 0.8175848131515837 - 0.5758081914178453i, 0.8104571982525948 - 0.5857978574564389i, 0.8032075314806449 - 0.5956993044924334i, 0.7958369046088836 - 0.6055110414043255i, 0.7883464276266063 - 0.6152315905806268i, 0.7807372285720945 - 0.6248594881423863i, 0.773010453362737 - 0.6343932841636455i, 0.765167265622459 - 0.6438315428897914i, 0.7572088465064846 - 0.6531728429537768i, 0.7491363945234594 - 0.6624157775901718i, 0.7409511253549591 - 0.6715589548470183i, 0.7326542716724129 - 0.680600997795453i, 0.724247082951467 - 0.6895405447370669i, 0.7157308252838186 - 0.6983762494089729i, 0.7071067811865476 - 0.7071067811865475i, 0.6983762494089729 - 0.7157308252838186i, 0.6895405447370669 - 0.7242470829514668i, 0.6806009977954531 - 0.7326542716724128i, 0.6715589548470184 - 0.7409511253549591i, 0.6624157775901718 - 0.7491363945234593i, 0.6531728429537769 - 0.7572088465064845i, 0.6438315428897915 - 0.765167265622459i, 0.6343932841636456 - 0.7730104533627369i, 0.6248594881423865 - 0.7807372285720944i, 0.6152315905806269 - 0.7883464276266062i, 0.6055110414043255 - 0.7958369046088835i, 0.5956993044924335 - 0.8032075314806448i, 0.585797857456439 - 0.8104571982525947i, 0.5758081914178454 - 0.8175848131515836i, 0.5657318107836132 - 0.8245893027850253i, 0.5555702330196023 - 0.8314696123025452i, 0.5453249884220466 - 0.838224705554838i, 0.5349976198870974 - 0.844853565249707i, 0.524589682678469 - 0.8513551931052652i, 0.5141027441932218 - 0.8577286100002721i, 0.5035383837257176 - 0.8639728561215866i, 0.49289819222978415 - 0.8700869911087113i, 0.4821837720791229 - 0.8760700941954065i, 0.4713967368259978 - 0.8819212643483549i, 0.46053871095824 - 0.8876396204028539i, 0.4496113296546066 - 0.8932243011955153i, 0.43861623853852766 - 0.8986744656939538i, 0.4275550934302822 - 0.9039892931234433i, 0.4164295600976373 - 0.9091679830905224i, 0.4052413140049898 - 0.9142097557035307i, 0.3939920400610481 - 0.9191138516900577i, 0.38268343236508984 - 0.9238795325112867i, 0.3713171939518376 - 0.9285060804732156i, 0.3598950365349883 - 0.9329927988347387i, 0.3484186802494345 - 0.937339011912575i, 0.33688985339222005 - 0.9415440651830208i, 0.325310292162263 - 0.9456073253805213i, 0.3136817403988915 - 0.9495281805930367i, 0.3020059493192282 - 0.9533060403541938i, 0.29028467725446233 - 0.9569403357322089i, 0.27851968938505306 - 0.9604305194155658i, 0.2667127574748984 - 0.9637760657954398i, 0.2548656596045146 - 0.9669764710448521i, 0.24298017990326398 - 0.970031253194544i, 0.23105810828067125 - 0.9729399522055602i, 0.21910124015686977 - 0.9757021300385286i, 0.20711137619221856 - 0.9783173707196277i, 0.1950903220161283 - 0.9807852804032304i, 0.18303988795514103 - 0.9831054874312164i, 0.17096188876030136 - 0.9852776423889412i, 0.1588581433338614 - 0.9873014181578584i, 0.14673047445536175 - 0.989176509964781i, 0.13458070850712622 - 0.99090263542778i, 0.12241067519921628 - 0.9924795345987101i, 0.11022Line truncated
1024: {1 + 0i, 0.9999811752826011 - 0.006135884649154475i, 0.9999247018391446 - 0.012271538285719925i, 0.9998305817958234 - 0.01840672990580482i, 0.9996988186962041 - 0.024541228522912288i, 0.9995294175010931 - 0.030674803176636626i, 0.9993223845883494 - 0.03680722294135883i, 0.9990777277526454 - 0.04293825693494082i, 0.9987954562051724 - 0.049067674327418015i, 0.9984755805732948 - 0.05519524434968994i, 0.9981181129001492 - 0.06132073630220858i, 0.9977230666441916 - 0.06744391956366405i, 0.9972904566786902 - 0.07356456359966743i, 0.9968202992911658 - 0.07968243797143013i, 0.996312612182778 - 0.0857973123444399i, 0.9957674144676598 - 0.09190895649713272i, 0.9951847266721968 - 0.0980171403295606i, 0.9945645707342554 - 0.10412163387205459i, 0.9939069700023561 - 0.11022220729388306i, 0.9932119492347945 - 0.11631863091190475i, 0.9924795345987101 - 0.1224106751992162i, 0.9917097536690995 - 0.12849811079379317i, 0.99090263542778 - 0.13458070850712617i, 0.9900582102622971 - 0.1406582393328492i, 0.989176509964781 - 0.14673047445536175i, 0.9882575677307495 - 0.15279718525844344i, 0.9873014181578584 - 0.15885814333386145i, 0.9863080972445987 - 0.16491312048996992i, 0.9852776423889412 - 0.17096188876030122i, 0.984210092386929 - 0.17700422041214875i, 0.9831054874312164 - 0.18303988795514095i, 0.9819638691095552 - 0.1890686641498062i, 0.9807852804032304 - 0.19509032201612825i, 0.9795697656854406 - 0.2011046348420919i, 0.9783173707196277 - 0.20711137619221856i, 0.9770281426577543 - 0.21311031991609136i, 0.9757021300385286 - 0.2191012401568698i, 0.9743393827855759 - 0.22508391135979283i, 0.9729399522055602 - 0.2310581082806711i, 0.9715038909862518 - 0.2370236059943672i, 0.9700312531945441 - 0.24298017990326387i, 0.9685220942744173 - 0.24892760574572015i, 0.9669764710448521 - 0.25486565960451457i, 0.9653944416976894 - 0.2607941179152755i, 0.9637760657954398 - 0.26671275747489837i, 0.9621214042690416 - 0.272621355449949i, 0.9604305194155658 - 0.27851968938505306i, 0.9587034748958715 - 0.2844075372112719i, 0.9569403357322088 - 0.29028467725446233i, 0.9551411683057708 - 0.2961508882436238i, 0.9533060403541938 - 0.3020059493192281i, 0.9514350209690083 - 0.30784964004153487i, 0.9495281805930367 - 0.3136817403988915i, 0.9475855910177412 - 0.3195020308160157i, 0.9456073253805213 - 0.3253102921622629i, 0.9435934581619604 - 0.33110630575987643i, 0.9415440651830208 - 0.33688985339222005i, 0.9394592236021899 - 0.3426607173119944i, 0.937339011912575 - 0.34841868024943456i, 0.9351835099389476 - 0.35416352542049034i, 0.9329927988347388 - 0.3598950365349881i, 0.9307669610789837 - 0.36561299780477385i, 0.9285060804732156 - 0.37131719395183754i, 0.9262102421383114 - 0.37700741021641826i, 0.9238795325112867 - 0.3826834323650898i, 0.921514039342042 - 0.38834504669882625i, 0.9191138516900577 - 0.3939920400610481i, 0.9166790599210427 - 0.3996241998456468i, 0.9142097557035307 - 0.40524131400498986i, 0.9117060320054299 - 0.4108431710579039i, 0.9091679830905225 - 0.41642956009763715i, 0.9065957045149153 - 0.4220002707997997i, 0.9039892931234433 - 0.4275550934302821i, 0.901348847046022 - 0.43309381885315196i, 0.8986744656939538 - 0.43861623853852766i, 0.8959662497561852 - 0.4441221445704292i, 0.8932243011955153 - 0.44961132965460654i, 0.8904487232447579 - 0.45508358712634384i, 0.8876396204028539 - 0.46053871095824i, 0.8847970984309378 - 0.4659764957679662i, 0.881921264348355 - 0.47139673682599764i, 0.8790122264286335 - 0.4767992300633221i, 0.8760700941954066 - 0.4821837720791227i, 0.8730949784182901 - 0.487550160148436i, 0.8700869911087113 - 0.49289819222978404i, 0.8670462455156926 - 0.49822766697278187i, 0.8639728561215867 - 0.5035383837257176i, 0.8608669386377673 - 0.508830142543107i, 0.8577286100002721 - 0.5141027441932217i, 0.8545579883654005 - 0.5193559901655896i, 0.8513551931052652 - 0.524589682678469i, 0.8481203448032972 - 0.5298036246862946i, 0.8448535652497071 - 0.5349976198870972i, 0.8415549774368984 - 0.5401714727298929i, 0.8382247055548381 - 0.5453249884220465i, 0.8348628749863801 - 0.5504579729366048i, 0.8314696123025453 - 0.5555702330196022i, 0.8280450452577557 - 0.560661576197336i, 0.8245893027850253 - 0.5657318107836131i, 0.8211025149911046 - 0.5707807458869673i, 0.8175848131515837 - 0.5758081914178453i, 0.8140363297059484 - 0.5808139580957645i, 0.8104571982525948 - 0.5857978574564389i, 0.8068475535437993 - 0.5907597018588742i, 0.8032075314806449 - 0.5956993044924334i, 0.799537269107905 - 0.600616479383869i, 0.7958369046088836 - 0.6055110414043255i, 0.7921065773002124 - 0.6103828062763095i, 0.7883464276266063 - 0.6152315905806268i, 0.7845565971555752 - 0.6200572117632891i, 0.7807372285720945 - 0.6248594881423863i, 0.7768884656732324 - 0.629638238914927i, 0.773010453362737 - 0.6343932841636455i, 0.7691033376455796 - 0.6391244448637757i, 0.765167265622459 - 0.6438315428897914i, 0.7612023854842618 - 0.6485144010221124i, 0.7572088465064846 - 0.6531728429537768i, 0.7531867990436125 - 0.6578066932970786i, 0.7491363945234594 - 0.66241577Line truncated
}
+46
View File
@@ -0,0 +1,46 @@
package fourier
// RevBitsTypes is a type constraint interface to scope the allowed types to
// call the ReverseBits function.
type RevBitsTypes interface {
uint | uint16 | uint32 | uint64 | int | int16 | int32 | int64
}
// ReverseFirstBits returns the first `size` bits of `value` in reverse order
// from: http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious
func ReverseFirstBits[T RevBitsTypes](value, size T) (reverse T) {
for ; value > 0; value, size = value>>1, size-1 {
reverse <<= 1 // left shift
reverse |= value & 1 // add next bit
}
return reverse << size
}
// ReverseBits returns the bits of `value` in reverse order
// from: http://graphics.stanford.edu/~seander/bithacks.html#BitReverseObvious
func ReverseBits[T RevBitsTypes](value T) (reverse T) {
for ; value > 0; value >>= 1 {
reverse <<= 1 // left shift
reverse |= value & 1 // add next bit
}
return reverse
}
// ReorderData shuffles the input complex128 values to re-order it,
// setting the first half of the slice in even indexes and the last half
// of the slice in odd indexes.
func ReorderData(value []complex128) []complex128 {
var (
ln = len(value)
reorder = make([]complex128, ln)
sizeLog = Log2(ln)
)
for i := range value {
reorder[ReverseFirstBits(i, sizeLog)] = value[i]
}
return reorder
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+54
View File
@@ -0,0 +1,54 @@
package window
import (
"math"
)
const (
minBlockSize = 4
tau = math.Pi * 2
twoTau = math.Pi * 4
)
// GeneratorFunc is a type of function that generates a Window based on an input size.
type GeneratorFunc func(int) Window
// Generate calls the GeneratorFunc itself, with the input int `size`.
func (w GeneratorFunc) Generate(size int) Window {
return w(size)
}
// Window is a custom type for a slice of float64, that repesents a Window that shapes
// input signal.
type Window []float64
// Apply will go through the input slice of float64 values, and apply the window to it by
// multiplying the value by the ratio in the respective element in the Window.
func (w Window) Apply(value []float64) {
for idx := range value {
value[idx] *= w[idx]
}
}
//nolint:gochecknoglobals // immutable slice that contains supported block sizes
var blockSizes = []int{8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192}
// New creates a new Window from the input GeneratorFunc and size
//
// The size should be a power of two supported in the fft package's BlockSize.
//
// In case the input size `int` is not a power of two, it will fallback to the next
// available block size, with a series of bit-shifting operations on the input size
// to derive the closest element in the (private) blockSizes slice of ints.
func New(w GeneratorFunc, size int) Window {
var (
count int
blockSize = size >> minBlockSize
)
for i := blockSize; i > 0; i >>= 1 {
count++
}
return w(blockSizes[count])
}
+38
View File
@@ -0,0 +1,38 @@
package safe
import (
"image"
"sync/atomic"
)
type Point struct {
X, Y atomic.Int64
}
func (p *Point) Load() image.Point {
return image.Pt(
int(p.X.Load()),
int(p.Y.Load()),
)
}
func (p *Point) Store(point image.Point) {
p.X.Store(int64(point.X))
p.Y.Store(int64(point.Y))
}
type Rectangle struct {
Min, Max Point
}
func (r *Rectangle) Load() image.Rectangle {
return image.Rectangle{
Min: r.Min.Load(),
Max: r.Max.Load(),
}
}
func (r *Rectangle) Store(rect image.Rectangle) {
r.Min.Store(rect.Min)
r.Max.Store(rect.Max)
}
+1 -1
View File
@@ -3,7 +3,7 @@ package util_test
import (
"testing"
"go.maze.io/harmon/internal/util"
"github.com/BeatGlow/beatglow/internal/util"
)
func TestMakeAddr(t *testing.T) {