Documentation ¶
Overview ¶
Package dsp provides generic implementations of some DSP functionalities.
Index ¶
- Constants
- func FindNoiseFloor[T Number](psd Block[T], edgeWidth int) (T, float64)
- func Magnitude[T Number](fftValue complex128, blockSize int) T
- func MagnitudeIndB[T Number](fftValue complex128, blockSize int) T
- func PSD[T Number](fftValue complex128, blockSize int) T
- func PSDValueIndB[T Number](psdValue T, blockSize int) T
- func SignalFrequency[T, F Number](bin int, spectrum Block[T], frequencyMapping *FrequencyMapping[F]) F
- type BinLocation
- type Block
- type BoolDebouncer
- type FFT
- type FilterBlock
- type FrequencyMapping
- type Goertzel
- func (f *Goertzel) Blocksize() int
- func (f *Goertzel) Detect(buf []float32) (float64, bool, int, error)
- func (f *Goertzel) Magnitude(block FilterBlock) float64
- func (f *Goertzel) MagnitudeThreshold() float64
- func (f *Goertzel) NormalizedMagnitude(block FilterBlock) float64
- func (f *Goertzel) SetMagnitudeThreshold(threshold float64)
- func (f *Goertzel) Tick() time.Duration
- type Number
- type Peak
- type RollingHistory
- func (h *RollingHistory[T]) Get(index int) T
- func (h *RollingHistory[T]) Max(n int) T
- func (h *RollingHistory[T]) MaxAt(indexes ...int) T
- func (h *RollingHistory[T]) Mean(n int) T
- func (h *RollingHistory[T]) Min(n int) T
- func (h *RollingHistory[T]) MinAt(indexes ...int) T
- func (h *RollingHistory[T]) Put(value T)
- func (h *RollingHistory[T]) Reset()
- func (h *RollingHistory[T]) SDev(n int) float64
- func (h *RollingHistory[T]) Sum(n int) T
- func (h *RollingHistory[T]) SumAt(indexes ...int) T
- func (h *RollingHistory[T]) Variance(n int) float64
- type RollingMean
- type RollingVariance
Constants ¶
const ( DefaultBlocksizeRatio = 0.005 DefaultMagnitudeThreshold = 0.75 )
Variables ¶
This section is empty.
Functions ¶
func Magnitude ¶
func Magnitude[T Number](fftValue complex128, blockSize int) T
func MagnitudeIndB ¶
func MagnitudeIndB[T Number](fftValue complex128, blockSize int) T
func PSD ¶
func PSD[T Number](fftValue complex128, blockSize int) T
func PSDValueIndB ¶
func SignalFrequency ¶
func SignalFrequency[T, F Number](bin int, spectrum Block[T], frequencyMapping *FrequencyMapping[F]) F
Types ¶
type BinLocation ¶
type BinLocation float64
const ( BinFrom BinLocation = -0.5 BinCenter BinLocation = 0 BinTo BinLocation = 0.5 )
func PeakCenterCorrection ¶
func PeakCenterCorrection[T, F Number](bin int, spectrum Block[T]) BinLocation
type Block ¶
type Block[T Number] []T
Block represents a block of samples that are processed as one unit.
type BoolDebouncer ¶
type BoolDebouncer struct {
// contains filtered or unexported fields
}
BoolDebouncer is a debouncer for boolean signals.
func NewBoolDebouncer ¶
func NewBoolDebouncer(threshold int) *BoolDebouncer
NewBoolDeboncer returns a new debouncer for boolean signals with the given threshold.
func (*BoolDebouncer) Debounce ¶
func (d *BoolDebouncer) Debounce(rawState bool) bool
Debounce is periodically called with the raw signal state. It returns the debounced signal state. The signal must be stable for threshold calls of Debounce until a state change is propagated by Debounce.
func (*BoolDebouncer) SetThreshold ¶
func (d *BoolDebouncer) SetThreshold(threshold int)
func (*BoolDebouncer) Threshold ¶
func (d *BoolDebouncer) Threshold() int
type FFT ¶
type FFT[T Number] struct { // contains filtered or unexported fields }
func (*FFT[T]) IQToSpectrum ¶
func (f *FFT[T]) IQToSpectrum(spectrum []T, iqSamples []T, projection func(complex128, int) T)
func (*FFT[T]) IQToSpectrumAndPSD ¶
func (f *FFT[T]) IQToSpectrumAndPSD(spectrum []T, psd []T, iqSamples []T, projection func(complex128, int) T)
type FilterBlock ¶
type FilterBlock []float32
FilterBlock represents
func (FilterBlock) Max ¶
func (b FilterBlock) Max() float32
Max returns the maximum absolute sample value in this filter block
type FrequencyMapping ¶
type FrequencyMapping[F Number] struct { // contains filtered or unexported fields }
func NewFrequencyMapping ¶
func NewFrequencyMapping[F Number](sampleRate int, blockSize int, centerFrequency F) *FrequencyMapping[F]
func (*FrequencyMapping[F]) BinToFrequency ¶
func (m *FrequencyMapping[F]) BinToFrequency(bin int, location BinLocation) F
func (*FrequencyMapping[F]) FrequencyToBin ¶
func (m *FrequencyMapping[F]) FrequencyToBin(frequency F) int
func (*FrequencyMapping[F]) SetCenterFrequency ¶
func (m *FrequencyMapping[F]) SetCenterFrequency(frequency F)
func (*FrequencyMapping[F]) String ¶
func (m *FrequencyMapping[F]) String() string
type Goertzel ¶
type Goertzel struct {
// contains filtered or unexported fields
}
Goertzel filter to detect a specific pitch frequency. See also: * https://www.embedded.com/the-goertzel-algorithm/ * https://www.embedded.com/single-tone-detection-with-the-goertzel-algorithm/
func NewDefaultGoertzel ¶
NewDefaultGoertzel returns a new Goertzel filter that uses the DefaultBlocksizeRatio.
func NewGoertzel ¶
NewGoertzel returns a new Goertzel filter to detect the given pitch frequency. blocksizeRatio = blocksize / sampleRate This is also the duration in seconds that should be covered by one filter block. The blocksizeRatio is used to calculate the best fitting block size for the given pitch and sample rate.
func (*Goertzel) Detect ¶
Detect the pitch in the given buffer. Only the first blocksize samples of the given buffer are used. Returns the normalized magnitude, the detected signal state, and the number of samples taken from the buffer.
func (*Goertzel) Magnitude ¶
func (f *Goertzel) Magnitude(block FilterBlock) float64
Magnitude returns the relative magnitude of the pitch frequency in the given filter block.
func (*Goertzel) MagnitudeThreshold ¶
MagnitudeThreshold defines the threshold for the normalized magnitude to be detected as signal.
func (*Goertzel) NormalizedMagnitude ¶
func (f *Goertzel) NormalizedMagnitude(block FilterBlock) float64
NormalizedMagnitude returns the magnitude of the pitch frequency in the given feature block in relation to the current magnitude limit. The normalized magnitude must exceed the magnitude threshold to detect the signal.
func (*Goertzel) SetMagnitudeThreshold ¶
SetMagnitudeThreshold sets the magnitude threshold.
type Number ¶
type Number interface { constraints.Integer | constraints.Float }
type Peak ¶
type Peak[M, F Number] struct { From int To int FromFrequency F ToFrequency F SignalFrequency F SignalValue M SignalBin int }
Peak represents a section in a block that contains a peak. M is used to represent magnitude values in the spectrum, F is the type used to represent frequencies
func (Peak[T, F]) CenterFrequency ¶
func (p Peak[T, F]) CenterFrequency() F
CenterFrequency, based on the FromFrequency and ToFrequency fields. Those fields must be filled with meaningful values.
func (Peak[T, F]) ContainsBin ¶
ContainsBin indicates if the given bin is within this peak.
type RollingHistory ¶
type RollingHistory[T Number] struct { // contains filtered or unexported fields }
RollingHistory provides access to the last <length> values and a set of functions based on those historical values.
func NewRollingHistory ¶
func NewRollingHistory[T Number](length int) *RollingHistory[T]
NewRollingHistory returns a new RollingHistory of the given length.
func (*RollingHistory[T]) Get ¶
func (h *RollingHistory[T]) Get(index int) T
Get provides the value that was inserted <index> Put calls in the past.
func (*RollingHistory[T]) MaxAt ¶
func (h *RollingHistory[T]) MaxAt(indexes ...int) T
MaxAt returns the maximum of the values at the given indexes.
func (*RollingHistory[T]) Mean ¶
func (h *RollingHistory[T]) Mean(n int) T
Mean of the last n values.
func (*RollingHistory[T]) MinAt ¶
func (h *RollingHistory[T]) MinAt(indexes ...int) T
MinAt returns the minimum of the values at the given indexes.
func (*RollingHistory[T]) Put ¶
func (h *RollingHistory[T]) Put(value T)
Put a new value into the history.
func (*RollingHistory[T]) SDev ¶
func (h *RollingHistory[T]) SDev(n int) float64
SDev returns the standard deviation of the last n values.
func (*RollingHistory[T]) SumAt ¶
func (h *RollingHistory[T]) SumAt(indexes ...int) T
SumAt sums up the values at the given indexes.
func (*RollingHistory[T]) Variance ¶
func (h *RollingHistory[T]) Variance(n int) float64
Variance of the last n values.
type RollingMean ¶
type RollingMean[T Number] struct { // contains filtered or unexported fields }
RollingMean calculates the mean over n values.
func NewRollingMean ¶
func NewRollingMean[T Number](n int) *RollingMean[T]
NewRollingMean with size n.
func (*RollingMean[T]) Put ¶
func (v *RollingMean[T]) Put(value T) T
Put a new value into the rolling window and get the new mean back.
type RollingVariance ¶
type RollingVariance[T Number] struct { // contains filtered or unexported fields }
RollingVariance calculates the variance over n values.
func NewRollingVariance ¶
func NewRollingVariance[T Number](n int) *RollingVariance[T]
NewRollingVariance with size n.
func (*RollingVariance[T]) Get ¶
func (v *RollingVariance[T]) Get() T
Get the current variance value.
func (*RollingVariance[T]) Put ¶
func (v *RollingVariance[T]) Put(value T) T
Put a new value into the rolling window and get the new variance back.