spafe.utils.preprocessing#

class spafe.utils.preprocessing.SlidingWindow(win_len: float = 0.025, win_hop: float = 0.01, win_type: Literal['hanning', 'bartlet', 'kaiser', 'blackman', 'hamming'] = 'hamming')[source]#

Bases: object

Sliding widow class.

Parameters
  • win_len (float) – window length in sec. (Default is 0.025).

  • win_hop (float) – step between successive windows in sec. (Default is 0.01).

  • win_type (float) – window type to apply for the windowing. (Default is β€œhamming”).

win_hop: float = 0.01#
win_len: float = 0.025#
win_type: Literal['hanning', 'bartlet', 'kaiser', 'blackman', 'hamming'] = 'hamming'#
spafe.utils.preprocessing.framing(sig: ndarray, fs: int = 16000, win_len: float = 0.025, win_hop: float = 0.01) Tuple[ndarray, int][source]#

transform a signal into a series of overlapping frames (= Frame blocking) as described in [Malek-framing-blog].

Parameters
  • sig (numpy.ndarray) – a mono audio signal (Nx1) from which to compute features.

  • fs (int) – the sampling frequency of the signal we are working with. (Default is 16000).

  • win_len (float) – window length in sec. (Default is 0.025).

  • win_hop (float) – step between successive windows in sec. (Default is 0.01).

Returns

  • (numpy.ndarray) : array of frames.

  • (int) : frame length.

Return type

(tuple)

Note

Uses the stride trick to accelerate the processing.

References

Malek-framing-blog

: Malek A., Signal framing, 25.01.2022, https://superkogito.github.io/blog/2020/01/25/signal_framing.html

spafe.utils.preprocessing.pre_emphasis(sig: ndarray, pre_emph_coeff: float = 0.97) ndarray[source]#

perform preemphasis on the input signal.

Parameters
  • sig (numpy.ndarray) – input signal.

  • coeff (float) – preemphasis coefficient. 0 is no filter. (Default is 0.97).

Returns

pre-empahsised signal.

Return type

(numpy.ndarray)

Note

\[y[t] = x[t] - \alpha \times x[t-1]\]
spafe.utils.preprocessing.stride_trick(a: ndarray, stride_length: int, stride_step: int) ndarray[source]#

apply framing using the stride trick from numpy.

Parameters
  • a (numpy.ndarray) – signal array.

  • stride_length (int) – length of the stride.

  • stride_step (int) – stride step.

Returns

blocked/framed array.

Return type

(numpy.ndarray)

Note

You can refer to numpy documentation of this stride trick here: https://numpy.org/doc/stable/reference/generated/numpy.lib.stride_tricks.sliding_window_view.html

spafe.utils.preprocessing.windowing(frames: ndarray, frame_len: int, win_type: Literal['hanning', 'bartlet', 'kaiser', 'blackman', 'hamming'] = 'hamming') ndarray[source]#

generate and apply a window function to avoid spectral leakage [Malek-windowing-blog].

Parameters
  • frames (numpy.ndarray) – array including the overlapping frames.

  • frame_len (int) – frame length.

  • win_type (str) – type of window to use. (Default is β€œhamming”).

Returns

windowed frames.

Return type

(numpy.ndarray)

References

Malek-windowing-blog

: Malek, A. Specctral leakage, 2022.03.13, https://superkogito.github.io/blog/2020/03/13/spectral_leakage_and_windowing.html

spafe.utils.preprocessing.zero_handling(x: ndarray) ndarray[source]#

handle the issue with zero values if they are exposed to become an argument for any log function.

Parameters

x (numpy.ndarray) – input vector.

Returns

vector with zeros substituted with epsilon values.

Return type

(numpy.ndarray)