signal

package module
v0.0.0-...-b47e3bf Latest Latest
Warning

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

Go to latest
Published: May 21, 2023 License: BSD-3-Clause Imports: 1 Imported by: 9

README

Package signal

Package signal provides types and functions to aid in signal processing.

Documentation

Overview

Package signal provides types and functions to aid in signal processing.

Index

Constants

View Source
const DefaultSize = 4096

Variables

This section is empty.

Functions

func ExpDecayFunc

func ExpDecayFunc(t float64) float64

func SawtoothFunc

func SawtoothFunc(t float64) float64

SawtoothFunc is the continuous signal of a sawtooth wave.

func SineFunc

func SineFunc(t float64) float64

SineFunc is the continuous signal of a sine wave.

func SquareFunc

func SquareFunc(t float64) float64

SquareFunc is the continuous signal of a square wave.

func TriangleFunc

func TriangleFunc(t float64) float64

TriangleFunc is the continuous signal of a triangle wave.

Types

type Continuous

type Continuous func(t float64) float64

Continuous represents a continuous-time signal.

Effectively, many things may be represented as continuous. A function returning math.Sin(2*math.Pi*t) samples a sine wave while Discrete.Interpolate returns an interpolated value from its table, with both satisfying this type.

func (Continuous) Sample

func (fn Continuous) Sample(src Continuous, interval, phase float64) float64

Sample satisfies Sampler interface.

The side effect of fn sampling src can not be known, so this returns phase+interval.

type Discrete

type Discrete []float64

Discrete represents a discrete-time signal.

If building a lookup table, let sampling interval equal the recipricol of len(Discrete).

const n = 1024
sig := make(Discrete, n)
sig.Sample(SineFunc, 1/n, 0)

If samples are intended to be played back in sequence, provide normalized frequency against output sample rate; e.g. to sample four seconds of 440Hz sine wave at 44.1kHz

r := 44100.0
t := 4.0
out := make(Discrete, int(r*t)) // allocate four seconds of space
out.Sample(SineFunc, 440/r, 0)  // sample 440Hz sine wave

To play these samples over four second period, use an oscillator as clock.

osc := snd.NewOscil(out, 1/t, nil) // package dasa.cc/snd

TODO document sampling at different rates.

func ExpDecay

func ExpDecay() Discrete

ExpDecay returns a discrete sample of ExpDecayFunc.

func Sawtooth

func Sawtooth() Discrete

Sawtooth returns a discrete sample of SawtoothFunc.

func SawtoothSynthesis

func SawtoothSynthesis(n int) Discrete

SawtoothSynthesis adds all partial harmonics belonging to [2..n], creating a sinusoidal wave that is the inverse of a sawtooth.

func Sine

func Sine() Discrete

Sine returns a discrete sample of SineFunc.

func Square

func Square() Discrete

Square returns a discrete sample of SquareFunc.

func SquareSynthesis

func SquareSynthesis(n int) Discrete

SquareSynthesis adds odd partial harmonics belonging to [3..n], creating a sinusoidal wave.

func Triangle

func Triangle() Discrete

Triangle returns a discrete sample of TriangleFunc.

func (Discrete) AdditiveInverse

func (sig Discrete) AdditiveInverse()

AdditiveInverse sets each sample to -x.

func (Discrete) AdditiveSynthesis

func (sig Discrete) AdditiveSynthesis(fd Discrete, pth int)

AdditiveSynthesis adds the fundamental, fd, for the partial harmonic, pth, to sig.

func (Discrete) At

func (sig Discrete) At(t float64) float64

At uses the fractional component of t to return the sample at the truncated index.

func (Discrete) Index

func (sig Discrete) Index(i int) float64

func (Discrete) Interp

func (sig Discrete) Interp(t float64) float64

Interp uses the fractional component of t to return an interpolated sample.

TODO currently does linear interpolation...

func (Discrete) Multiply

func (sig Discrete) Multiply(xs Discrete)

func (Discrete) MultiplyScalar

func (sig Discrete) MultiplyScalar(x float64)

func (Discrete) Normalize

func (sig Discrete) Normalize()

Normalize alters sig so values belong to [-1..1].

func (Discrete) NormalizeRange

func (sig Discrete) NormalizeRange(s, e float64)

NormalizeRange alters sig so values belong to [s..e].

Calling this method for values that already occupy [s..e] will degrade values further due to round-off error.

func (Discrete) Reverse

func (sig Discrete) Reverse()

Reverse sig in place so the first element becomes the last and the last element becomes the first.

func (Discrete) Sample

func (sig Discrete) Sample(src Continuous, interval, phase float64) float64

Sample satisfies Sampler interface.

func (Discrete) UnitInverse

func (sig Discrete) UnitInverse()

UnitInverse sets each sample to 1 - x.

type Float

type Float interface {
	~float32 | ~float64
}

type Sampler

type Sampler interface {
	// Sample reads values from src at phase by interval and returns next phase to sample.
	//
	// The side effect resulting from sampling is implementation specific.
	Sample(src Continuous, interval, phase float64) float64
}

Jump to

Keyboard shortcuts

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