dsp

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 25, 2026 License: BSD-2-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package dsp provides digital signal processing utilities for the Opus codec.

Index

Constants

View Source
const (
	WindowHann = iota
	WindowHamming
	WindowBlackman
	WindowSine
	WindowVorbis
)

Window types

View Source
const Pi = math.Pi

Pi is the mathematical constant π

Variables

This section is empty.

Functions

func Abs

func Abs(x float64) float64

Abs returns the absolute value of a float64

func ApplyWindow

func ApplyWindow(signal []float64, window []float64)

ApplyWindow multiplies the input signal by a window function in-place.

func BitReverse

func BitReverse(n, bits int) int

BitReverse reverses the bits of n for the given number of bits

func Clamp

func Clamp(val, min, max int) int

Clamp restricts a value to a given range

func ClampFloat

func ClampFloat(val, min, max float64) float64

ClampFloat restricts a float64 value to a given range

func Cos

func Cos(x float64) float64

Cos returns the cosine of x (in radians)

func Dot

func Dot(a, b []float64) float64

Dot computes the dot product of two float64 slices

func Energy

func Energy(x []float64) float64

Energy computes the energy (sum of squares) of a signal

func IsPowerOf2

func IsPowerOf2(n int) bool

IsPowerOf2 checks if n is a power of 2

func Log2

func Log2(n int) int

Log2 returns the base-2 logarithm of n (for power of 2)

func Max

func Max(a, b int) int

Max returns the maximum of two integers

func MaxFloat

func MaxFloat(a, b float64) float64

MaxFloat returns the maximum of two float64 values

func Min

func Min(a, b int) int

Min returns the minimum of two integers

func MinFloat

func MinFloat(a, b float64) float64

MinFloat returns the minimum of two float64 values

func NextPowerOf2

func NextPowerOf2(n int) int

NextPowerOf2 returns the next power of 2 >= n

func Normalize

func Normalize(x []float64)

Normalize normalizes a slice to have maximum absolute value of 1.0

func OverlapAdd

func OverlapAdd(output []float64, input []float64, offset int)

OverlapAdd performs overlap-add operation for windowed frames.

func RMS

func RMS(x []float64) float64

RMS computes the root mean square of a signal

func RealIFFT

func RealIFFT(input []Complex, outputSize int) ([]float64, error)

RealIFFT performs inverse FFT to produce real-valued output.

func Sin

func Sin(x float64) float64

Sin returns the sine of x (in radians)

func Window

func Window(windowType, length int) []float64

Window generates a window function of the specified type and length.

func WindowedOverlapAdd

func WindowedOverlapAdd(output []float64, input []float64, window []float64, offset int)

WindowedOverlapAdd applies window and then performs overlap-add.

Types

type CELTMode

type CELTMode struct {
	N       int       // frame size (e.g. 960)
	Overlap int       // overlap size (e.g. 120)
	Window  []float64 // synthesis window of length Overlap (rising ramp, 0→1)
}

CELTMode holds parameters for the CELT MDCT with small-overlap model.

func NewCELTMode

func NewCELTMode(N, overlap int, window []float32) *CELTMode

NewCELTMode creates a CELT mode. window is the overlap-region window (length = overlap).

func (*CELTMode) CLTMDCTBackward

func (m *CELTMode) CLTMDCTBackward(X []float64, carry []float64) []float64

CLTMDCTBackward performs the CELT inverse transform and TDAC mirror/window step matching libopus clt_mdct_backward. carry holds the ov/2 future samples preserved from the previous call in libopus' decode_mem layout.

func (*CELTMode) CLTMDCTForward added in v1.1.0

func (m *CELTMode) CLTMDCTForward(in []float64) []float64

CLTMDCTForward performs the CELT forward MDCT, the analysis counterpart of CLTMDCTBackward. It is a faithful float port of libopus clt_mdct_forward_c (celt/mdct.c) for the non-subdivided (stride=1) case, using the same trig convention as IMDCTRaw (cos(2π(i+0.125)/(2N))).

Input `in` is the analysis buffer of length N+Overlap: the first Overlap samples are the windowed transition from the previous frame and the remaining N samples are the current frame (libopus celt_encoder compute_mdcts passes in+b*N over a buffer of stride B*N+overlap). Output is N MDCT coefficients matching the layout consumed by CLTMDCTBackward.

scale is the post-FFT scale factor making CLTMDCTBackward(CLTMDCTForward(x)) reconstruct x through overlap-add; it pairs with the "raw" (unnormalised) IMDCTRaw used by the backward path.

func (*CELTMode) IMDCT

func (m *CELTMode) IMDCT(X []float64) []float64

IMDCT computes the N-point CELT inverse MDCT and returns N raw (unwindowed) time-domain samples. Windowing is deferred to InverseOverlapAdd.

Formula (RFC 6716 §5.5.2):

Y[n] = (2/N) * sum_{k=0}^{N-1} X[k] * cos(π/N * (k+1/2) * (n + N/2 + 1/2))
     = (2/N) * Re( exp(iπ(2n+N+1)/(4N)) * DFT_{2N}(a')[n] )
where a'[k] = X[k] * exp(iπk(N+1)/(2N))  (zero-padded to 2N)

func (*CELTMode) IMDCTRaw

func (m *CELTMode) IMDCTRaw(X []float64) []float64

IMDCTRaw computes the raw libopus clt_mdct_backward buffer before the final TDAC mirror/window step. Unlike IMDCT, this is not normalised by 2/N and the samples are in libopus' internal half-overlap-shifted order.

func (*CELTMode) InverseOverlapAdd

func (m *CELTMode) InverseOverlapAdd(y []float64, tail []float64) []float64

InverseOverlapAdd is the MDCT-IV overlap-add currently used by CELT synthesis.

type Complex

type Complex struct {
	Real, Imag float64
}

Complex represents a complex number for FFT operations

func AnyFFT

func AnyFFT(x []Complex) []Complex

AnyFFT computes the DFT of x using Bluestein's chirp-Z algorithm. Works for any length N (not restricted to powers of 2).

func AnyIFFT

func AnyIFFT(x []Complex) []Complex

AnyIFFT computes the inverse DFT of x using AnyFFT.

func FFT

func FFT(input []Complex) ([]Complex, error)

FFT performs a Cooley-Tukey FFT on the input data. The input size must be a power of 2. This implementation is based on the classic radix-2 decimation-in-time algorithm.

func IFFT

func IFFT(input []Complex) ([]Complex, error)

IFFT performs an inverse FFT.

func RealFFT

func RealFFT(input []float64) ([]Complex, error)

RealFFT performs FFT on real-valued input, exploiting symmetry. Returns only the first n/2+1 complex values (the rest are conjugate symmetric).

func (Complex) Abs

func (c Complex) Abs() float64

Abs returns the magnitude of the complex number

func (Complex) Add

func (c Complex) Add(other Complex) Complex

Add adds two complex numbers

func (Complex) Conj

func (c Complex) Conj() Complex

Conj returns the complex conjugate

func (Complex) Mul

func (c Complex) Mul(other Complex) Complex

Mul multiplies two complex numbers

func (Complex) MulScalar

func (c Complex) MulScalar(s float64) Complex

MulScalar multiplies by a real scalar

func (Complex) Sub

func (c Complex) Sub(other Complex) Complex

Sub subtracts another complex number

type FFTConfig

type FFTConfig struct {
	// contains filtered or unexported fields
}

FFTConfig holds precomputed twiddle factors for efficient repeated FFT operations.

func NewFFTConfig

func NewFFTConfig(size int) (*FFTConfig, error)

NewFFTConfig creates a new FFT configuration with precomputed twiddle factors.

func (*FFTConfig) Execute

func (cfg *FFTConfig) Execute(input []Complex) ([]Complex, error)

Execute performs FFT using precomputed twiddle factors.

func (*FFTConfig) ExecuteInPlace added in v1.2.0

func (cfg *FFTConfig) ExecuteInPlace(data []Complex) error

ExecuteInPlace performs the configured FFT directly in data.

func (*FFTConfig) ExecuteInverse

func (cfg *FFTConfig) ExecuteInverse(input []Complex) ([]Complex, error)

ExecuteInverse performs inverse FFT using precomputed twiddle factors.

type MDCT

type MDCT struct {
	// contains filtered or unexported fields
}

MDCT represents a Modified Discrete Cosine Transform configuration. MDCT is used extensively in audio coding, particularly in CELT.

func NewMDCT

func NewMDCT(size int) (*MDCT, error)

NewMDCT creates a new MDCT configuration. size is the number of output coefficients (input is 2*size samples). For non-power-of-2 sizes (e.g. 960), a direct cosine computation is used.

func (*MDCT) Forward

func (m *MDCT) Forward(input []float64) ([]float64, error)

Forward performs the forward MDCT transform. Input: 2*N samples, Output: N coefficients.

func (*MDCT) ForwardOverlap

func (m *MDCT) ForwardOverlap(input []float64, overlap []float64) ([]float64, error)

ForwardOverlap performs forward MDCT with proper overlap handling for streaming.

func (*MDCT) Inverse

func (m *MDCT) Inverse(input []float64) ([]float64, error)

Inverse performs the inverse MDCT transform (IMDCT). Input: N coefficients, Output: 2*N samples (before overlap-add).

func (*MDCT) InverseOverlap

func (m *MDCT) InverseOverlap(coeffs []float64, overlap []float64) ([]float64, error)

InverseOverlap performs inverse MDCT with proper overlap-add for streaming.

func (*MDCT) Size

func (m *MDCT) Size() int

Size returns the MDCT output size (number of coefficients)

Jump to

Keyboard shortcuts

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