spafe.utils.preprocessing#
Description : Preprocessing utils implementation.
Copyright (c) 2019-2024 Ayoub Malek. This source code is licensed under the terms of the BSD 3-Clause License. For a copy, see <https://github.com/SuperKogito/spafe/blob/master/LICENSE>.
- class spafe.utils.preprocessing.SlidingWindow(win_len: float = 0.025, win_hop: float = 0.01, win_type: typing_extensions.Literal[hanning, bartlet, kaiser, blackman, hamming] = 'hamming')[source]#
Bases:
object
Sliding widow class.
- Parameters
- win_type: typing_extensions.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
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
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: typing_extensions.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
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