numir.signal

Signal processing package

  • Declaration

    pure nothrow @nogc @safe auto blackman(size_t n, double a0 = 0.42, double a1 = 0.5, double a2 = 0.08);

    Classic Blackman window slice generator

    Parameters

    size_t n

    length of window

    double a0

    window parameter

    double a1

    window parameter

    double a2

    window parameter

    Return Value

    window weight slice

  • Declaration

    pure nothrow @nogc @safe auto hann(size_t n, double a = 0.5, double b = 0.25);

    Hann window slice generator

    Parameters

    size_t n

    length of window

    double a

    window parameter

    double b

    window parameter

    Return Value

    window weight slice

    Examples

    1. import numir.signal; import std.meta : AliasSeq; import mir.ndslice : maxIndex; import std.math : approxEqual; // test windows are symmetry and peaked at median auto n = 11; static foreach (wfun; AliasSeq!(blackman, hann)) {{ auto w = wfun(n); assert(w.maxIndex[0] == n / 2); foreach (i; 0 .. n / 2 - 1) { assert(w[i].approxEqual(w[$ - 1 - i])); } }}

  • Declaration

    pure nothrow @nogc @safe auto splitFrames(Xs)(Xs xs, size_t width, size_t stride);

    Split (window and stride) time frames for FFT or convolutions

    Parameters

    Xs xs

    input slice

    size_t width

    length of each segment

    size_t stride

    the number of skipped frames between the head of split slices

    Return Value

    slice of split (windowed and strided) slices

    Examples

    1. import numir.signal; import mir.ndslice.topology : iota; static immutable ys = [[0,1,2], [2,3,4]]; assert(iota(6).splitFrames(3, 2) == ys);

  • Declaration

    auto stft(alias windowFun = hann, Xs)(Xs xs, size_t nperseg, size_t noverlap) if (isSlice!Xs && (DimensionCount!Xs == 1));
    auto stft(alias windowFun = hann, Xs)(Xs xs, size_t nperseg = 256) if (isSlice!Xs && (DimensionCount!Xs == 1));

    Computes the short time Fourier transform

    Parameters

    Xs xs

    input 1d slice with the shape (ntimes,)

    size_t nperseg

    (default 256) short-time frame width for each FFT segment

    size_t noverlap

    (default nperseg / 2) short-time frame overlapped length for each FFT segment

    Return Value

    comlex 2d slice (nframes, nfreqs=nperseg)

  • Declaration

    auto istft(alias windowFun = hann, Xs)(Xs xs, size_t noverlap) if (isSlice!Xs && (DimensionCount!Xs == 2));
    auto istft(alias windowFun = hann, Xs)(Xs xs) if (isSlice!Xs && (DimensionCount!Xs == 2));

    Computes the inverse short time Fourier transform

    Parameters

    Xs xs

    input 2d complex slice with the shape (ntimes, nfreq)

    size_t noverlap

    (default nperseg / 2) short-time frame overlapped length for each FFT segment Returns 1d real slice with the shape (ntimes,)

    Examples

    test stft-istft health

    1. import std.complex; import std.stdio; import mir.ndslice : map, sliced; import numir.testing : approxEqual; // first 16 samples from https://raw.githubusercontent.com/ShigekiKarita/torch-nmf-ss-toy/master/test10k.wav auto xs = [-10, 15, 106, -1, -655, -1553, -1501, -522, -106, 831, 1250, 381, 1096, 2302, 2686, 2427].sliced; // need larger overlaps to revert well auto ys = stft(xs, 8, 7); auto ixs = istft(ys, 7).map!(c => c.re); assert(approxEqual(ixs, xs));