packages

signal::conv

Convolution and correlation, 1-d and 2-d, direct or by FFT. Output sizes and alignment follow SciPy's scipy.signal.convolve.

Enums

ModeSource

pub enum Mode

Which part of the full convolution to return.

nabi
use signal::conv::{convolve, Mode};
use core::array::Vector;

let a: Vector<f64> = [1, 2, 3];
let v: Vector<f64> = [0, 1, 0.5];
println(try convolve(a, v)); // [0.0, 1.0, 2.5, 4.0, 1.5]
println(try convolve(a, v, mode: Mode::Same)); // [1.0, 2.5, 4.0]
println(try convolve(a, v, mode: Mode::Valid)); // [2.5]
  • Every overlap: n + m - 1 points.

  • The size of the first input, centred on the full result.

  • Only where the inputs overlap fully: |n - m| + 1 points.

MethodSource

pub enum Method

How to compute: Auto picks the faster of the two by size.

  • Direct§

    Source

    The sum by its definition, O(n m); exact to rounding.

  • By real FFTs, O((n + m) log(n + m)); error about eps * log2(n + m) * max|a| * max|v| * min(n, m).

Functions

convolveSource

pub fn convolve<A: RealSamples, V: RealSamples>(a: A, v: V, mode: Mode = Mode::Full, method: Method = Method::Auto) throws (SignalError) -> Vector<f64>

The convolution (a * v)[k] = sum_i a[i] v[k - i], cut to mode. Method::Auto (the default) sums directly when one input is short and uses FFTs otherwise.

nabi
use signal::conv::convolve;
use core::array::Vector;

let a: Vector<f64> = [1, 1];
println(try convolve(a, a)); // [1.0, 2.0, 1.0]

correlateSource

pub fn correlate<A: RealSamples, V: RealSamples>(a: A, v: V, mode: Mode = Mode::Full, method: Method = Method::Auto) throws (SignalError) -> Vector<f64>

The cross-correlation c[k] = sum_i a[i + k] v[i], as SciPy's correlate: convolve(a, v reversed), so in Mode::Full lag 0 is at index v.len() - 1.

nabi
use signal::conv::{correlate, Mode};
use core::array::Vector;

let a: Vector<f64> = [0, 1, 2, 0];
let v: Vector<f64> = [1, 2];
println(try correlate(a, v)); // [0.0, 2.0, 5.0, 2.0, 0.0]
println(try correlate(a, v, mode: Mode::Valid)); // [2.0, 5.0, 2.0]

correlation_lagsSource

pub fn correlation_lags(n: usize, m: usize, mode: Mode = Mode::Full) -> Vector<i64>

The lags of each correlate output point for inputs of lengths n and m in mode (as SciPy's correlation_lags).

nabi
println(signal::conv::correlation_lags(4, 2)); // [-1, 0, 1, 2, 3]

convolve2dSource

pub fn convolve2d(a: Matrix<f64>, k: Matrix<f64>, mode: Mode = Mode::Full, method: Method = Method::Auto) throws (SignalError) -> Matrix<f64>

The 2-d convolution of a and kernel k, cut to mode along both axes (as SciPy's convolve2d with zero boundaries).

nabi
use signal::conv::convolve2d;
use core::array::Matrix;

let a: Matrix<f64> = [[1, 2], [3, 4]];
let k: Matrix<f64> = [[1, 1]];
println(try convolve2d(a, k)); // [[1.0, 3.0, 2.0], [3.0, 7.0, 4.0]]

correlate2dSource

pub fn correlate2d(a: Matrix<f64>, k: Matrix<f64>, mode: Mode = Mode::Full, method: Method = Method::Auto) throws (SignalError) -> Matrix<f64>

The 2-d cross-correlation: convolve2d with k flipped on both axes.

nabi
use signal::conv::{correlate2d, Mode};
use core::array::Matrix;

let a: Matrix<f64> = [[1, 2], [3, 4]];
println(try correlate2d(a, a, mode: Mode::Valid)); // [[30.0]]