Added README; cleaned up the repo

This commit is contained in:
maze authored and maze committed 2026-10-29 17:30:03 +01:00
1 parent f9bc5bb3b8
commit dae2fbc97b
33 files changed
+270 -9330

No files matched your search

-26
View File
@@ -1,26 +0,0 @@
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
@@ -1,147 +0,0 @@
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
@@ -1,58 +0,0 @@
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
@@ -1,45 +0,0 @@
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
@@ -1,46 +0,0 @@
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
@@ -1,54 +0,0 @@
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])
}
-348
View File
@@ -1,348 +0,0 @@
// Copyright 2021 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package spidev wraps the Linux spidev driver and performs low-level SPI
// operations.
package spidev
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"math"
"os"
"runtime"
"unsafe"
"golang.org/x/sys/unix"
)
// See Linux "include/uapi/linux/spi/spidev.h" and
// "Documentation/spi/spidev.rst"
// Various ioctl numbers.
const (
iocRdMode = 0x80016b01
iocWrMode = 0x40016b01
iocRdLSBFirst = 0x80016b02
iocWrLSBFirst = 0x40016b02
iocRdBitsPerWord = 0x80016b03
iocWrBitsPerWord = 0x40016b03
iocRdMaxSpeedHz = 0x80046b04
iocWrMaxSpeedHz = 0x40046b04
iocRdMode32 = 0x80046b05
iocWrMode32 = 0x40046b05
)
// Constants used by iocMessage function.
const (
// iocMessage0 is the length of a message of 0 length. Use the
// iocMessage(n) function for an iocMessage of length n.
iocMessage0 = 0x40006b00
sizeBits = 14
sizeShift = 16
sizeMask = ((1 << sizeBits) - 1) << sizeShift
)
// maxTransferSize is the maximum size of a transfer. This is limited by the
// `length uint32` in the iocTransfer struct.
var maxTransferSize = math.MaxInt32
// iocMessage is an ioctl number for n Transfers. Since the ioctl number
// contains the size of the message, it is not a constant.
func iocMessage(n int) uint32 {
size := uint32(n * binary.Size(iocTransfer{}))
if n < 0 || size > (1<<sizeBits) {
return iocMessage(0)
}
return iocMessage0 | (size << sizeShift)
}
// Mode is a bitset of the current SPI mode bits.
type Mode uint32
const (
// CPHA determines clock phase sampling (1=trailing edge).
CPHA Mode = 1 << iota
// CPOL determines clock polarity (1=idle high).
CPOL
// CS_HIGH determines chip select polarity (1=idle high).
CS_HIGH
// LSB_FIRST determines whether least significant bit comes first in a
// word (1=LSB first).
LSB_FIRST
// THREE_WIRE determines whether the bus operates in three wire mode
// (1=three wire).
THREE_WIRE
// LOOP determines whether the device should loop (1=loop enabled).
LOOP
// NO_CS determines whether to disable chip select (1=no chip select).
NO_CS
// READY determins ready mode bit.
READY
// TX_DUAL determines whether to transmit in dual mode.
TX_DUAL
// TX_QUAD determines whether to transmit in quad mode.
TX_QUAD
// RX_DUAL determines whether to receive in dual mode.
RX_DUAL
// RX_QUAD determines whether to receive in quad mode.
RX_QUAD
)
// iocTransfer is the data type used by the iocMessage ioctl. Multiple such
// transfers may be chained together in a single ioctl call.
type iocTransfer struct {
// txBuf contains the userspace address of data to send. If this is 0,
// then zeros are shifted onto the SPI bus.
txBuf uint64
// rxBuf contains the userspace address of data to receive. If this is
// 0, data received is ignored.
rxBuf uint64
// length is the length of max{transfer, txBuf, rxBuf} in bytes.
length uint32
speedHz uint32
delayUSecs uint16
bitsPerWord uint8
csChange uint8
txNBits uint8
rxNBits uint8
wordDelayUSecs uint8
pad uint8
}
// Transfer is the data and options for a single SPI transfer. Note that a SPI
// transfer is full-duplex meaning data is shifted out of Tx and shifted into
// Rx on the same clock cycle.
type Transfer struct {
// Tx contains a slice sent over the SPI bus.
Tx []byte
// Rx contains a slice received from the SPI bus.
Rx []byte
// The following temporarily override the SPI mode. They only apply to
// the current transfer.
// SpeedHz sets speed in Hz (optional).
SpeedHz uint32
// DelayUSecs is how long to delay before the next transfer (optional).
DelayUSecs uint16
// BitsPerWord is the device's wordsize (optional).
BitsPerWord uint8
// CSChange controls whether the CS (Chip Select) signal will be
// de-asserted at the end of the transfer.
CSChange bool
// TxNbits controls single, dual or quad mode (optional).
TxNBits uint8
// RxNbits controls single, dual or quad mode (optional).
RxNBits uint8
// WordDelayUSecs is the delay between words (optional).
WordDelayUSecs uint8
}
func (t *Transfer) String() string {
var x [8]byte
n, _ := io.ReadAtLeast(bytes.NewBuffer(t.Tx), x[:], len(x))
return fmt.Sprintf("%#02x...[:%d](%#02x)", x[:n], len(t.Tx), x[0])
}
// ErrTxOverflow is returned if the Transfer buffer is too large.
type ErrTxOverflow struct {
TxLen, TxMax int
}
// Error implements the error interface.
func (e ErrTxOverflow) Error() string {
return fmt.Sprintf("SPI tx buffer overflow (%d > %d)", e.TxLen, e.TxMax)
}
// ErrRxOverflow is returned if the Transfer buffer is too large.
type ErrRxOverflow struct {
RxLen, RxMax int
}
// Error implements the error interface.
func (e ErrRxOverflow) Error() string {
return fmt.Sprintf("SPI rx buffer overflow (%d > %d)", e.RxLen, e.RxMax)
}
// ErrBufferMismatch is returned if the rx and tx buffers do not have equal
// length.
type ErrBufferMismatch struct {
TxLen, RxLen int
}
// Error implements the error interface.
func (e ErrBufferMismatch) Error() string {
return fmt.Sprintf("SPI tx and rx buffers of unequal length (%d != %d)", e.TxLen, e.RxLen)
}
// SPI wraps the Linux device driver and provides low-level SPI operations.
type SPI struct {
f *os.File
// Used for mocking.
syscall func(trap, a1, a2 uintptr, a3 unsafe.Pointer) (r1, r2 uintptr, err unix.Errno)
// logger allows logging
logger func(string, ...any)
}
type opt func(s *SPI) error
// WithLogger returns an opt which can be used in Open to add
// a logger. A common usage would be:
// spidev.Open("/dev/spidev0.0", WithLogger(log.Printf))
func WithLogger(l func(string, ...any)) opt {
return func(s *SPI) error {
s.logger = l
return nil
}
}
// safe tries to set "safe" settings for initial SPI operation.
// However, settings may not succeed, for $REASONS$.
// Hardware is highly variable.
// If there is an error, log it, and continue.
func (s *SPI) safe() {
if err := s.SetSpeedHz(500000); err != nil {
s.logger("warning only: set speed to %d HZ err %v", 500000, err)
}
}
// Open opens a new SPI device. dev is a filename such as "/dev/spidev0.0".
// Remember to call Close() once done.
func Open(dev string, opts ...opt) (*SPI, error) {
f, err := os.OpenFile(dev, os.O_RDWR, 0)
if err != nil {
return nil, err
}
s := &SPI{
f: f,
logger: func(string, ...any) {}, // log.Printf,
//logger: log.Printf,
// a3 must be an unsafe.Pointer instead of a uintptr, otherwise
// we cannot mock out in the test without creating a race
// condition. See `go doc unsafe.Pointer`.
syscall: func(trap, a1, a2 uintptr, a3 unsafe.Pointer) (r1, r2 uintptr, err unix.Errno) {
return unix.Syscall(trap, a1, a2, uintptr(a3))
},
}
for _, o := range opts {
if err := o(s); err != nil {
return nil, err
}
}
s.safe()
return s, nil
}
// Close closes the SPI device.
func (s *SPI) Close() error {
return s.f.Close()
}
// Transfer performs multiple SPI reads and/or writes in a single function.
// See the Transfer struct for details.
func (s *SPI) Transfer(transfers []Transfer) error {
// Convert []Transfer to []iocTransfer.
it := make([]iocTransfer, len(transfers))
for i, t := range transfers {
s.logger("%d:%s", i, t.String())
it[i] = iocTransfer{
speedHz: t.SpeedHz,
delayUSecs: t.DelayUSecs,
bitsPerWord: t.BitsPerWord,
txNBits: t.TxNBits,
rxNBits: t.RxNBits,
wordDelayUSecs: t.WordDelayUSecs,
}
if len(t.Tx) > maxTransferSize {
return ErrTxOverflow{len(t.Tx), maxTransferSize}
}
if len(t.Rx) > maxTransferSize {
return ErrRxOverflow{len(t.Rx), maxTransferSize}
}
if len(t.Tx) != 0 && len(t.Rx) != 0 && len(t.Tx) != len(t.Rx) {
return ErrBufferMismatch{len(t.Tx), len(t.Rx)}
}
if len(t.Tx) != 0 {
txBuf := &transfers[i].Tx[0]
it[i].txBuf = uint64(uintptr(unsafe.Pointer(txBuf)))
it[i].length = uint32(len(t.Tx))
// Defer garbage collection until after the syscall.
defer runtime.KeepAlive(txBuf)
}
if len(t.Rx) != 0 {
rxBuf := &transfers[i].Rx[0]
it[i].rxBuf = uint64(uintptr(unsafe.Pointer(rxBuf)))
it[i].length = uint32(len(t.Rx))
// Defer garbage collection until after the syscall.
defer runtime.KeepAlive(rxBuf)
}
if transfers[i].CSChange {
it[i].csChange = 1
}
}
if _, _, err := s.syscall(unix.SYS_IOCTL, s.f.Fd(), uintptr(iocMessage(len(transfers))), unsafe.Pointer(&it[0])); err != 0 {
return os.NewSyscallError("ioctl(SPI_IOC_MESSAGE)", err)
}
return nil
}
// Mode returns the Mode bitset.
func (s *SPI) Mode() (Mode, error) {
var m Mode
if _, _, err := s.syscall(unix.SYS_IOCTL, s.f.Fd(), iocRdMode32, unsafe.Pointer(&m)); err != 0 {
return 0, os.NewSyscallError("ioctl(SPI_IOC_RD_MODE32)", err)
}
return m, nil
}
// SetMode sets the Mode bitset.
func (s *SPI) SetMode(m Mode) error {
if _, _, err := s.syscall(unix.SYS_IOCTL, s.f.Fd(), iocWrMode32, unsafe.Pointer(&m)); err != 0 {
return os.NewSyscallError("ioctl(SPI_IOC_WR_MODE32)", err)
}
return nil
}
// BitsPerWord sets the number of bits per word. Myy understanding is this is
// only useful if there is a word delay.
func (s *SPI) BitsPerWord() (uint8, error) {
var bpw uint8
if _, _, err := s.syscall(unix.SYS_IOCTL, s.f.Fd(), iocRdBitsPerWord, unsafe.Pointer(&bpw)); err != 0 {
return bpw, os.NewSyscallError("ioctl(SPI_IOC_RD_BITS_PER_WORD)", err)
}
return bpw, nil
}
// SetBitsPerWord sets the number of bits per word.
func (s *SPI) SetBitsPerWord(bpw uint8) error {
if _, _, err := s.syscall(unix.SYS_IOCTL, s.f.Fd(), iocWrBitsPerWord, unsafe.Pointer(&bpw)); err != 0 {
return os.NewSyscallError("ioctl(SPI_IOC_WR_BITS_PER_WORD)", err)
}
return nil
}
// SpeedHz gets the transfer speed.
func (s *SPI) SpeedHz() (uint32, error) {
var hz uint32
if _, _, err := s.syscall(unix.SYS_IOCTL, s.f.Fd(), iocRdMaxSpeedHz, unsafe.Pointer(&hz)); err != 0 {
return hz, os.NewSyscallError("ioctl(SPI_IOC_RD_MAX_SPEED_HZ)", err)
}
return hz, nil
}
// SetSpeedHz sets the transfer speed.
func (s *SPI) SetSpeedHz(hz uint32) error {
if _, _, err := s.syscall(unix.SYS_IOCTL, s.f.Fd(), iocWrMaxSpeedHz, unsafe.Pointer(&hz)); err != 0 {
return os.NewSyscallError("ioctl(SPI_IOC_WR_MAX_SPEED_HZ)", err)
}
return nil
}