dsp

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package dsp provides generic implementations of some DSP functionalities.

Index

Constants

View Source
const (
	DefaultBlocksizeRatio     = 0.005
	DefaultMagnitudeThreshold = 0.75
)

Variables

This section is empty.

Functions

func FindNoiseFloor

func FindNoiseFloor[T Number](psd Block[T], edgeWidth int) (T, float64)

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 PSDValueIndB[T Number](psdValue T, blockSize int) T

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.

func (Block[T]) Max

func (b Block[T]) Max(from, to int) (T, int)

Max imum value in the given section of this block.

func (Block[T]) Mean

func (b Block[T]) Mean(from, to int) T

Mean of the values in the given section of this block.

func (Block[T]) Size

func (b Block[T]) Size() int

Size returns the blocksize.

func (Block[T]) Subblock

func (b Block[T]) Subblock(from, to int) Block[T]

Subblock returns the given section of this block.

func (Block[T]) Sum

func (b Block[T]) Sum(from, to int) T

Sum of the values in the given section of this block.

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 NewFFT

func NewFFT[T Number]() *FFT[T]

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

func NewDefaultGoertzel(pitch float64, sampleRate int) *Goertzel

NewDefaultGoertzel returns a new Goertzel filter that uses the DefaultBlocksizeRatio.

func NewGoertzel

func NewGoertzel(pitch float64, sampleRate int, blocksizeRatio float64) *Goertzel

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) Blocksize

func (f *Goertzel) Blocksize() int

Blocksize used for the given pitch and sample rate.

func (*Goertzel) Detect

func (f *Goertzel) Detect(buf []float32) (float64, bool, int, error)

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

func (f *Goertzel) MagnitudeThreshold() float64

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

func (f *Goertzel) SetMagnitudeThreshold(threshold float64)

SetMagnitudeThreshold sets the magnitude threshold.

func (*Goertzel) Tick

func (f *Goertzel) Tick() time.Duration

Tick returns the duration of one filter block.

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 FindPeaks

func FindPeaks[T, F Number](peaks []Peak[T, F], spectrum Block[T], cumulationSize int, threshold T, frequencyMapping *FrequencyMapping[F]) []Peak[T, F]

func (Peak[T, F]) Center

func (p Peak[T, F]) Center() int

Center index

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

func (p Peak[T, F]) ContainsBin(bin int) bool

ContainsBin indicates if the given bin is within this peak.

func (Peak[T, F]) Width

func (p Peak[T, F]) Width() int

Width in bins.

func (Peak[T, F]) WidthHz

func (p Peak[T, F]) WidthHz() F

WidthHz in Hz, based on the FromFrequency and ToFrequency fields. Those fiels must be filled with meaningful values.

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]) Max

func (h *RollingHistory[T]) Max(n int) T

Max of the last n values.

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]) Min

func (h *RollingHistory[T]) Min(n int) T

Min 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]) Reset

func (h *RollingHistory[T]) Reset()

Reset the rolling 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]) Sum

func (h *RollingHistory[T]) Sum(n int) T

Sum up 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]) Get

func (v *RollingMean[T]) Get() T

Get the current mean value.

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.

func (*RollingMean[T]) Reset

func (v *RollingMean[T]) Reset()

Reset the rolling window.

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.

func (*RollingVariance[T]) Reset

func (v *RollingVariance[T]) Reset()

Reset the rolling window.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL