numir.signal
Signal processing package
-
Declaration
pure nothrow @nogc @safe auto
blackman
(size_tn
, doublea0
= 0.42, doublea1
= 0.5, doublea2
= 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_tn
, doublea
= 0.5, doubleb
= 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
See Also
https://ccrma.stanford.edu/~jos/sasp/Generalized_Hamming_Window_Family.html https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.hann.html
Examples
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)(Xsxs
, size_twidth
, size_tstride
);Split (window and
stride
) time frames for FFT or convolutionsParameters
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
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)(Xsxs
, size_tnperseg
, size_tnoverlap
) if (isSlice!Xs && (DimensionCount!Xs == 1));
autostft
(alias windowFun = hann, Xs)(Xsxs
, size_tnperseg
= 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 segmentReturn Value
comlex 2d slice (nframes, nfreqs=
nperseg
) -
Declaration
auto
istft
(alias windowFun = hann, Xs)(Xsxs
, size_tnoverlap
) if (isSlice!Xs && (DimensionCount!Xs == 2));
autoistft
(alias windowFun = hann, Xs)(Xsxs
) 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
healthimport 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));