Skip to content

signal #

vsl.signal

Portable signal-processing primitives that complement vsl.fft. APIs that take sample arrays use []f64 in sample order and reject empty or non-finite inputs. Window generators take an integer length instead and reject non-positive lengths; kaiser_window also rejects negative or non-finite beta.

Windows

hann_window(n, sym), hamming_window(n, sym), blackman_window(n, sym), bartlett_window(n, sym), boxcar_window(n), and kaiser_window(n, beta). sym = true uses the SciPy symmetric convention (endpoints included); sym = false is periodic, for spectral analysis. Length one returns [1.0]. The Kaiser window uses a convergent, overflow-safe exponentially scaled Bessel I0 ratio (so large beta keeps a finite centre value of 1) and beta >= 0.

Convolution and correlation

pub enum ConvolveMode { full same valid }
pub fn convolve(a []f64, b []f64, mode ConvolveMode) ![]f64
pub fn correlate(a []f64, b []f64, mode ConvolveMode) ![]f64

full has length len(a) + len(b) - 1; same returns len(a) samples centered in the full result; valid returns the overlap-only region. Convolution is a direct O(n*m) sum with no padding or normalization. Correlation reverses the second argument and then convolves.

Filtering

FilterState.new(b, a) normalizes so a[0] = 1 and holds the transposed direct-form-II delay line. step(sample) advances it by one sample; lfilter(b, a, x) filters a whole buffer from zero state. Coefficients follow the SciPy convention sum(a[k] y[n-k]) = sum(b[k] x[n-k]). Chunked step calls produce bit-identical output to one lfilter call. No filter-design routines are included yet.

import vsl.signal

b := [f64(1.0)]
a := [f64(1.0), -0.5]
filtered := signal.lfilter(b, a, [f64(1.0), 1.0, 1.0])!
println(filtered)

Haar wavelets

haar_forward(values) returns (approximation, detail) from one orthonormal level. haar_inverse(approximation, detail) reconstructs 2 * len(approximation) samples. Odd input lengths are extended by repeating the last sample, which is documented because the inverse cannot recover the original parity. Both entrypoints reject non-finite inputs. Parseval's identity holds across the transform; for odd-length inputs it applies to the extended input described above, not to the original input.

See the spectral analysis and filtering example.

fn bartlett_window #

fn bartlett_window(n int, sym bool) ![]f64

bartlett_window returns a triangular window, symmetric or periodic.

fn blackman_window #

fn blackman_window(n int, sym bool) ![]f64

blackman_window returns the Blackman window with coefficients 0.42, 0.5, 0.08.

fn boxcar_window #

fn boxcar_window(n int) ![]f64

boxcar_window returns n ones.

fn convolve #

fn convolve(a []f64, b []f64, mode ConvolveMode) ![]f64

convolve computes the direct finite convolution of a and b. full returns length len(a)+len(b)-1, same returns len(a) centered in the full result, and valid returns the overlap-only region. No padding or normalization is applied.

fn correlate #

fn correlate(a []f64, b []f64, mode ConvolveMode) ![]f64

correlate computes cross-correlation of a and b by convolving a with the reversed kernel. The mode conventions match convolve.

fn haar_forward #

fn haar_forward(values []f64) !([]f64, []f64)

haar_forward computes one level of the orthonormal discrete Haar transform. For odd-length input, the last sample is repeated before transforming, so the returned approximation and detail arrays have length ceil(n/2).

fn haar_inverse #

fn haar_inverse(approximation []f64, detail []f64) ![]f64

haar_inverse reconstructs the signal from one Haar level. The result has length 2 * len(approximation); an odd original length was extended by repeating its last sample in haar_forward.

fn hamming_window #

fn hamming_window(n int, sym bool) ![]f64

hamming_window returns the Hamming window with coefficients 0.54 and 0.46.

fn hann_window #

fn hann_window(n int, sym bool) ![]f64

hann_window returns the raised-cosine Hann window. sym=true matches SciPy's symmetric convention (zero at both ends); sym=false is periodic.

fn kaiser_window #

fn kaiser_window(n int, beta f64) ![]f64

kaiser_window returns the Kaiser window for shape parameter beta. The symmetric convention is used; n == 1 returns [1.0], and n >= 2 is symmetric.

fn lfilter #

fn lfilter(b []f64, a []f64, x []f64) ![]f64

lfilter filters an entire buffer with zero initial state. For a non-zero or streaming start, construct a FilterState and call step directly.

fn ConvolveMode.from #

fn ConvolveMode.from[W](input W) !ConvolveMode

from returns the ConvolveMode value of an integer or of a field name, or an error if there is none.

fn FilterState.new #

fn FilterState.new(b []f64, a []f64) !FilterState

FilterState.new validates and normalizes coefficients so a[0] is one.

enum ConvolveMode #

enum ConvolveMode {
	full
	same
	valid
}

ConvolveMode selects the output-length convention.

struct FilterState #

@[noinit]
struct FilterState {
pub:
	b []f64
	a []f64
mut:
	z []f64
}

FilterState holds the delay-line state of a transposed direct-form-II digital filter. Coefficients follow the SciPy convention a[0]*y[n] + ... + a[q]*y[n-q] = b[0]*x[n] + ... + b[p]*x[n-p].

fn (FilterState) step #

fn (mut state FilterState) step(sample f64) !f64

step advances the filter by one sample and returns y[n].